Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/XMLChartTheme.java @ 304:a16d337c5678
The style of charts can be configured with ChartThemes using XML configuration files.
gnv-artifacts/trunk@362 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 23 Nov 2009 17:01:28 +0000 |
parents | |
children | 22a6493e8460 |
comparison
equal
deleted
inserted
replaced
303:9ba2cf8cad8d | 304:a16d337c5678 |
---|---|
1 package de.intevation.gnv.chart; | |
2 | |
3 import java.awt.Font; | |
4 import java.awt.Color; | |
5 import java.lang.NumberFormatException; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 | |
9 import org.jfree.chart.StandardChartTheme; | |
10 import org.jfree.chart.plot.XYPlot; | |
11 import org.jfree.ui.RectangleInsets; | |
12 import org.jfree.util.UnitType; | |
13 | |
14 | |
15 import org.w3c.dom.Document; | |
16 | |
17 import de.intevation.artifactdatabase.Config; | |
18 | |
19 /** | |
20 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de> | |
21 */ | |
22 public class XMLChartTheme | |
23 extends StandardChartTheme | |
24 { | |
25 private static final Color DEFAULT_COLOR = Color.BLACK; | |
26 | |
27 private Logger log = Logger.getLogger(XMLChartTheme.class); | |
28 | |
29 protected boolean domainCrosshairVisible; | |
30 protected boolean rangeCrosshairVisible; | |
31 | |
32 | |
33 public XMLChartTheme(String name) { | |
34 super(name); | |
35 } | |
36 | |
37 | |
38 public void setDomainCrosshairVisible(boolean visible) { | |
39 this.domainCrosshairVisible = visible; | |
40 } | |
41 | |
42 | |
43 public boolean getDomainCrosshairVisible() { | |
44 return domainCrosshairVisible; | |
45 } | |
46 | |
47 | |
48 public boolean getRangeCrosshairVisible() { | |
49 return rangeCrosshairVisible; | |
50 } | |
51 | |
52 | |
53 public void setRangeCrosshairVisible(boolean visible) { | |
54 this.rangeCrosshairVisible = visible; | |
55 } | |
56 | |
57 | |
58 public void applyXMLConfiguration(Document document) { | |
59 log.debug("create XMLChartTheme"); | |
60 | |
61 init(document); | |
62 } | |
63 | |
64 | |
65 private void init(Document document) { | |
66 log.debug("init XMLChartTheme parameters"); | |
67 | |
68 initChartParameters(document); | |
69 initTitleParameters(document); | |
70 initSubtitleParameters(document); | |
71 initPlotParameters(document); | |
72 initAxisParameters(document); | |
73 initLegendParameters(document); | |
74 } | |
75 | |
76 | |
77 private void initTitleParameters(Document document) { | |
78 log.debug("init title parameters."); | |
79 | |
80 int size = getInt(document, "theme/title/font/size/@value"); | |
81 String type = getString(document, "theme/title/font/type/@value"); | |
82 boolean bold = getBool(document, "theme/title/font/bold/@value"); | |
83 | |
84 setExtraLargeFont(createFont(type, size, bold)); | |
85 | |
86 String color = getString(document, "theme/title/font/color/@value"); | |
87 Color c = decodeColor(color); | |
88 if (c != null) | |
89 setTitlePaint(c); | |
90 } | |
91 | |
92 | |
93 private void initSubtitleParameters(Document document) { | |
94 log.debug("init title parameters."); | |
95 | |
96 int size = getInt(document, "theme/subtitle/font/size/@value"); | |
97 String type = getString(document, "theme/subtitle/font/type/@value"); | |
98 boolean bold = getBool(document, "theme/subtitle/font/bold/@value"); | |
99 | |
100 setLargeFont(createFont(type, size, bold)); | |
101 | |
102 String col = getString(document, "theme/subtitle/font/color/@value"); | |
103 setSubtitlePaint(Color.decode(col)); | |
104 } | |
105 | |
106 | |
107 private void initChartParameters(Document document) { | |
108 log.debug("init chart parameters."); | |
109 | |
110 String bg = getString(document, "theme/chart/background/color/@value"); | |
111 Color c = decodeColor(bg); | |
112 if (c != null) | |
113 setChartBackgroundPaint(c); | |
114 } | |
115 | |
116 | |
117 private void initPlotParameters(Document document) { | |
118 log.debug("init plot parameters."); | |
119 | |
120 String tmp = null; | |
121 tmp = getString(document, "theme/plot/background/color/@value"); | |
122 Color c = decodeColor(tmp); | |
123 if (c != null) | |
124 setPlotBackgroundPaint(c); | |
125 | |
126 tmp = getString(document, "theme/plot/outline/color/@value"); | |
127 c = decodeColor(tmp); | |
128 if (c != null) | |
129 setPlotOutlinePaint(c); | |
130 | |
131 tmp = getString(document, "theme/plot/domaingridline/color/@value"); | |
132 c = decodeColor(tmp); | |
133 if (c != null) | |
134 setDomainGridlinePaint(c); | |
135 | |
136 tmp = getString(document, "theme/plot/rangegridline/color/@value"); | |
137 c = decodeColor(tmp); | |
138 if (c != null) | |
139 setRangeGridlinePaint(c); | |
140 | |
141 boolean rangeCrosshairVisible = getBool( | |
142 document, "theme/plot/rangecrosshair/visible/@value"); | |
143 setRangeCrosshairVisible(rangeCrosshairVisible); | |
144 | |
145 boolean domainCrosshairVisible = getBool( | |
146 document, "theme/plot/domaincrosshair/visible/@value"); | |
147 setDomainCrosshairVisible(domainCrosshairVisible); | |
148 | |
149 int top = getInt(document, "theme/plot/offset/top/@value"); | |
150 int bottom = getInt(document, "theme/plot/offset/bottom/@value"); | |
151 int left = getInt(document, "theme/plot/offset/left/@value"); | |
152 int right = getInt(document, "theme/plot/offset/right/@value"); | |
153 setAxisOffset(new RectangleInsets( | |
154 UnitType.RELATIVE, | |
155 top, left, bottom, right) | |
156 ); | |
157 } | |
158 | |
159 | |
160 private void initAxisParameters(Document document) { | |
161 log.debug("init axis parameters."); | |
162 | |
163 String tmp = null; | |
164 tmp = getString(document, "theme/axis/label/color/@value"); | |
165 Color c = decodeColor(tmp); | |
166 if (c != null) | |
167 setAxisLabelPaint(c); | |
168 | |
169 tmp = getString(document, "theme/axis/ticklabel/color/@value"); | |
170 c = decodeColor(tmp); | |
171 if (c != null) | |
172 setTickLabelPaint(c); | |
173 } | |
174 | |
175 | |
176 private void initLegendParameters(Document document) { | |
177 log.debug("init legend parameters."); | |
178 | |
179 String tmp = null; | |
180 tmp = getString(document, "theme/legend/font/color/@value"); | |
181 Color c = decodeColor(tmp); | |
182 if (c != null) | |
183 setLegendItemPaint(c); | |
184 | |
185 tmp = getString(document, "theme/legend/background/color/@value"); | |
186 c = decodeColor(tmp); | |
187 if (c != null) | |
188 setLegendBackgroundPaint(c); | |
189 } | |
190 | |
191 | |
192 private static String getString(Document document, String xpath) { | |
193 return Config.getStringXPath(document, xpath); | |
194 } | |
195 | |
196 | |
197 private static int getInt(Document document, String xpath) { | |
198 String tmp = getString(document, xpath); | |
199 | |
200 if (tmp != null) | |
201 return Integer.parseInt(tmp); | |
202 else | |
203 return 0; | |
204 } | |
205 | |
206 | |
207 private static boolean getBool(Document document, String xpath) { | |
208 String tmp = getString(document, xpath); | |
209 | |
210 if (tmp != null) | |
211 return Boolean.parseBoolean(tmp); | |
212 else | |
213 return false; | |
214 } | |
215 | |
216 | |
217 protected Color decodeColor(String color) { | |
218 try { | |
219 if (color == null) | |
220 return null; | |
221 | |
222 return Color.decode(color); | |
223 } | |
224 catch (NumberFormatException nfe) { | |
225 log.warn("Error while parsing color: " + color, nfe); | |
226 } | |
227 | |
228 return null; | |
229 } | |
230 | |
231 | |
232 protected Font createFont(String type, int size, boolean bold) { | |
233 Font font = null; | |
234 if (bold) | |
235 font = new Font(type, Font.BOLD, size); | |
236 else | |
237 font = new Font(type, Font.PLAIN, size); | |
238 | |
239 return font; | |
240 } | |
241 | |
242 | |
243 protected void applyToXYPlot(XYPlot plot) { | |
244 log.debug("apply theme parameter to XYPlot"); | |
245 | |
246 super.applyToXYPlot(plot); | |
247 plot.setDomainCrosshairVisible(this.domainCrosshairVisible); | |
248 plot.setRangeCrosshairVisible(this.rangeCrosshairVisible); | |
249 } | |
250 } |