comparison flys-artifacts/src/main/java/de/intevation/flys/jfree/XYStyle.java @ 3468:f37e7e8907cb

merged flys-artifacts/2.8.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:39 +0200
parents 0b9b2a0c4e64
children
comparison
equal deleted inserted replaced
3387:5ffad8bde8ad 3468:f37e7e8907cb
1 package de.intevation.flys.jfree;
2
3 import de.intevation.flys.utils.ThemeUtil;
4
5 import java.awt.BasicStroke;
6 import java.awt.Color;
7 import java.awt.geom.Ellipse2D;
8
9 import org.apache.log4j.Logger;
10 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
11 import org.w3c.dom.Document;
12
13
14 /**
15 * Utility to apply theme-settings to a renderer.
16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
17 */
18 public class XYStyle implements Style {
19
20 private static Logger logger = Logger.getLogger(XYStyle.class);
21
22 protected Document theme;
23
24 protected XYLineAndShapeRenderer renderer;
25
26
27 public XYStyle(Document theme) {
28 this.theme = theme;
29 this.renderer = null;
30 }
31
32
33 /**
34 * Applies line color, size and type attributes to renderer, also
35 * whether to draw lines and/or points.
36 */
37 @Override
38 public XYLineAndShapeRenderer applyTheme(XYLineAndShapeRenderer r, int idx){
39 this.renderer = r;
40 applyLineColor(r, idx);
41 applyLineSize(r, idx);
42 applyLineType(r, idx);
43 applyShowLine(r, idx);
44 applyShowPoints(r, idx);
45 applyPointSize(r, idx);
46 applyPointColor(r, idx);
47 applyShowMinimum(r, idx);
48 applyShowMaximum(r, idx);
49
50 // Line label styles
51 applyShowLineLabel(r, idx);
52 applyShowLineLabelBG(r, idx);
53 applyLineLabelFont(r, idx);
54 applyLineLabelColor(r, idx);
55 applyLineLabelBGColor(r, idx);
56
57 // Point label styles
58 // TODO: Currently point label are annotations and are not drawn this way
59 /*
60 applyShowPointLabelBG(r, idx);
61 applyLinePointFont(r, idx);
62 applyLinePointColor(r, idx);
63 applyLinePointBGColor(r, idx);*/
64
65 return r;
66 }
67
68
69 /** Set line color to renderer. */
70 protected void applyLineColor(XYLineAndShapeRenderer r, int idx) {
71 Color c = ThemeUtil.parseLineColorField(theme);
72 if(c != null) {
73 logger.debug("applyLineColor " + c.toString());
74 r.setSeriesPaint(idx, c);
75 }
76 else {
77 logger.warn("applyLineColor: color is null - malformed linecolor field?");
78 }
79 }
80
81
82 /** Tells the renderer whether or not to add a label to a line. */
83 protected void applyShowLineLabel(XYLineAndShapeRenderer r, int idx) {
84 if (!(r instanceof EnhancedLineAndShapeRenderer)) {
85 return;
86 }
87 boolean showLabelLine = ThemeUtil.parseShowLineLabel(theme);
88 boolean anyLabel = showLabelLine || ThemeUtil.parseShowWidth(theme) ||
89 ThemeUtil.parseShowLevel(theme) ||
90 ThemeUtil.parseShowMiddleHeight(theme);
91 ((EnhancedLineAndShapeRenderer)r).setShowLineLabel(anyLabel, idx);
92 }
93
94
95 /** Tells the renderer whether or not to fill the bg of a lines label. */
96 protected void applyShowLineLabelBG(XYLineAndShapeRenderer r, int idx) {
97 if (!(r instanceof EnhancedLineAndShapeRenderer)) {
98 return;
99 }
100 boolean showLabelLine = ThemeUtil.parseLabelShowBackground(theme);
101 ((EnhancedLineAndShapeRenderer)r).setShowLineLabelBG(idx, showLabelLine);
102 }
103
104 /** Tell the renderer which font (and -size and -style) to use for
105 * linelabels. */
106 protected void applyLineLabelFont(XYLineAndShapeRenderer r, int idx) {
107 if (!(r instanceof EnhancedLineAndShapeRenderer)) {
108 return;
109 }
110 ((EnhancedLineAndShapeRenderer)r).setLineLabelFont(
111 ThemeUtil.parseTextFont(theme), idx);
112 }
113
114 /** Tell the renderer which color to use for
115 * linelabels. */
116 protected void applyLineLabelColor(XYLineAndShapeRenderer r, int idx) {
117 if (!(r instanceof EnhancedLineAndShapeRenderer)) {
118 return;
119 }
120 ((EnhancedLineAndShapeRenderer)r).setLineLabelTextColor(
121 idx, ThemeUtil.parseTextColor(theme));
122 }
123
124 /** Tell the renderer which color to use for bg of
125 * linelabels. */
126 protected void applyLineLabelBGColor(XYLineAndShapeRenderer r, int idx) {
127 if (!(r instanceof EnhancedLineAndShapeRenderer)) {
128 return;
129 }
130 ((EnhancedLineAndShapeRenderer)r).setLineLabelBGColor(idx,
131 ThemeUtil.parseTextBackground(theme));
132 }
133
134 /** Set stroke of series. */
135 protected void applyLineSize(XYLineAndShapeRenderer r, int idx) {
136 int size = ThemeUtil.parseLineWidth(theme);
137 r.setSeriesStroke(
138 idx,
139 new BasicStroke(size));
140 }
141
142
143 /** Set stroke strength of series. */
144 protected void applyLineType(XYLineAndShapeRenderer r, int idx) {
145 int size = ThemeUtil.parseLineWidth(theme);
146 float[] dashes = ThemeUtil.parseLineStyle(theme);
147
148 // Do not apply the dashed style.
149 if (dashes.length <= 1) {
150 return;
151 }
152
153 r.setSeriesStroke(
154 idx,
155 new BasicStroke(size,
156 BasicStroke.CAP_BUTT,
157 BasicStroke.JOIN_ROUND,
158 1.0f,
159 dashes,
160 0.0f));
161 }
162
163
164 protected void applyPointSize(XYLineAndShapeRenderer r, int idx) {
165 int size = ThemeUtil.parsePointWidth(theme);
166 int dim = 2 * size;
167
168 r.setSeriesShape(idx, new Ellipse2D.Double(-size, -size, dim, dim));
169 }
170
171
172 protected void applyPointColor(XYLineAndShapeRenderer r, int idx) {
173 Color c = ThemeUtil.parsePointColor(theme);
174
175 if (c != null) {
176 r.setSeriesFillPaint(idx, c);
177 r.setUseFillPaint(true);
178 r.setDrawOutlines(false);
179 }
180 }
181
182
183 /**
184 * Sets form and visibility of points.
185 */
186 protected void applyShowPoints(XYLineAndShapeRenderer r, int idx) {
187 boolean show = ThemeUtil.parseShowPoints(theme);
188
189 r.setSeriesShapesVisible(idx, show);
190 r.setDrawOutlines(true);
191 }
192
193
194 protected void applyShowLine(XYLineAndShapeRenderer r, int idx) {
195 boolean show = ThemeUtil.parseShowLine(theme);
196 r.setSeriesLinesVisible(idx, show);
197 }
198
199
200 protected void applyShowMinimum(XYLineAndShapeRenderer r, int idx) {
201 if (!(r instanceof EnhancedLineAndShapeRenderer)) {
202 return;
203 }
204
205 boolean visible = ThemeUtil.parseShowMinimum(theme);
206
207 EnhancedLineAndShapeRenderer er = (EnhancedLineAndShapeRenderer) r;
208 er.setIsMinimumShapeVisisble(idx, visible);
209 }
210
211
212 protected void applyShowMaximum(XYLineAndShapeRenderer r, int idx) {
213 if (!(r instanceof EnhancedLineAndShapeRenderer)) {
214 return;
215 }
216
217 boolean visible = ThemeUtil.parseShowMaximum(theme);
218
219 EnhancedLineAndShapeRenderer er = (EnhancedLineAndShapeRenderer) r;
220 er.setIsMaximumShapeVisible(idx, visible);
221 }
222
223
224 @Override
225 public XYLineAndShapeRenderer getRenderer() {
226 return this.renderer;
227 }
228 }
229 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org