comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/QWSeriesCollection.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents 0cf647fe2a96
children 04d090d06d3f
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.services;
2
3 import de.intevation.flys.artifacts.model.fixings.QW;
4
5 import de.intevation.flys.java2d.ShapeUtils;
6
7 import de.intevation.flys.jfree.ShapeRenderer;
8
9 import java.awt.BasicStroke;
10 import java.awt.Color;
11 import java.awt.Paint;
12 import java.awt.Shape;
13
14 import java.awt.geom.Rectangle2D;
15
16 import java.text.DateFormat;
17 import java.text.SimpleDateFormat;
18
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.jfree.chart.LegendItem;
26 import org.jfree.chart.LegendItemCollection;
27
28 import org.jfree.chart.labels.XYItemLabelGenerator;
29
30 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
31
32 import org.jfree.data.xy.XYDataset;
33 import org.jfree.data.xy.XYSeries;
34 import org.jfree.data.xy.XYSeriesCollection;
35
36 public class QWSeriesCollection
37 extends XYSeriesCollection
38 implements XYItemLabelGenerator
39 {
40 public interface LabelGenerator {
41 String createLabel(QW qw);
42 } // interface LabelGenerator
43
44 public static class DateFormatLabelGenerator
45 implements LabelGenerator
46 {
47 protected DateFormat format;
48
49 public DateFormatLabelGenerator() {
50 this("dd.MM.yyyy");
51 }
52
53 public DateFormatLabelGenerator(String format) {
54 this(new SimpleDateFormat(format));
55 }
56
57 public DateFormatLabelGenerator(DateFormat format) {
58 this.format = format;
59 }
60
61 @Override
62 public String createLabel(QW qw) {
63 Date date = qw.getDate();
64 return date != null ? format.format(date) : "";
65 }
66 } // class DateFormatLabelGenerator
67
68 public static final LabelGenerator SIMPLE_GENERATOR =
69 new DateFormatLabelGenerator();
70
71 protected Date minDate;
72 protected Date maxDate;
73
74 protected List<List<QW>> labels;
75
76 protected Rectangle2D area;
77
78 protected LabelGenerator labelGenerator;
79
80 protected Map<ShapeRenderer.Entry, Integer> knownShapes =
81 new HashMap<ShapeRenderer.Entry, Integer>();
82
83 public QWSeriesCollection() {
84 labels = new ArrayList<List<QW>>();
85 labelGenerator = SIMPLE_GENERATOR;
86 }
87
88 public QWSeriesCollection(LabelGenerator labelGenerator) {
89 this();
90 this.labelGenerator = labelGenerator;
91 }
92
93 protected static ShapeRenderer.Entry classify(QW qw) {
94 boolean interpolated = qw.getInterpolated();
95
96 Shape shape = interpolated
97 ? ShapeUtils.INTERPOLATED_SHAPE
98 : ShapeUtils.MEASURED_SHAPE;
99
100 boolean filled = !interpolated;
101 Color color = interpolated
102 ? Color.green
103 : Color.blue;
104
105 return new ShapeRenderer.Entry(shape, color, filled);
106 }
107
108 public void add(QW qw) {
109
110 ShapeRenderer.Entry key = classify(qw);
111
112 Integer seriesNo = knownShapes.get(key);
113
114 XYSeries series;
115
116 if (seriesNo == null) {
117 seriesNo = Integer.valueOf(getSeriesCount());
118 knownShapes.put(key, seriesNo);
119 series = new XYSeries(seriesNo, false);
120 addSeries(series);
121 labels.add(new ArrayList<QW>());
122 }
123 else {
124 series = getSeries(seriesNo);
125 }
126
127 series.add(qw.getQ(), qw.getW());
128
129 labels.get(seriesNo).add(qw);
130
131 extendDateRange(qw);
132 extendArea(qw);
133 }
134
135 protected void extendDateRange(QW qw) {
136 Date date = qw.getDate();
137 if (date != null) {
138 if (minDate == null) {
139 minDate = maxDate = date;
140 }
141 else {
142 if (date.compareTo(minDate) < 0) {
143 minDate = date;
144 }
145 if (date.compareTo(maxDate) > 0) {
146 maxDate = date;
147 }
148 }
149 }
150 }
151
152 protected void extendArea(QW qw) {
153 if (area == null) {
154 area = new Rectangle2D.Double(
155 qw.getQ(), qw.getW(), 0d, 0d);
156 }
157 else {
158 area.add(qw.getQ(), qw.getW());
159 }
160 }
161
162 public Rectangle2D getArea() {
163 return area;
164 }
165
166 public Date getMinDate() {
167 return minDate;
168 }
169
170 public Date getMaxDate() {
171 return maxDate;
172 }
173
174 public LabelGenerator getLabelGenerator() {
175 return labelGenerator;
176 }
177
178 @Override
179 public String generateLabel(XYDataset dataset, int series, int item) {
180 return labelGenerator.createLabel(labels.get(series).get(item));
181 }
182
183 public StandardXYItemRenderer createRenderer() {
184 StandardXYItemRenderer renderer = new ShapeRenderer(knownShapes);
185 renderer.setBaseItemLabelGenerator(this);
186 renderer.setBaseSeriesVisibleInLegend(false);
187 renderer.setBaseItemLabelsVisible(true);
188 return renderer;
189 }
190
191 public static final LegendItem legendItem(
192 String label,
193 Paint paint,
194 Shape shape,
195 boolean filled
196 ) {
197 BasicStroke stroke = new BasicStroke();
198 return new LegendItem(
199 label, // label
200 null, // description
201 null, // tooltip
202 null, // url
203 true, // shape visible
204 shape, // shape
205 filled, // shape filled
206 filled ? paint : Color.white, // fill paint
207 true, // shape outline
208 paint, // outline paint
209 stroke, // outline stroke
210 false, // line visible
211 shape, // line
212 stroke, // line stroke
213 Color.white);
214 }
215
216 public void addLegendItems(
217 LegendItemCollection lic,
218 ShapeRenderer.LabelGenerator lg
219 ) {
220 for (ShapeRenderer.Entry entry: knownShapes.keySet()) {
221 lic.add(legendItem(
222 lg.createLabel(entry),
223 entry.getPaint(),
224 entry.getShape(),
225 entry.getFilled()));
226 }
227 }
228 }
229 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org