comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalCrossSectionChart.java @ 657:af3f56758f59

merged gnv-artifacts/0.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:53 +0200
parents 92d7a8cd1ef1
children 79401c871da4
comparison
equal deleted inserted replaced
590:5f5f273c8566 657:af3f56758f59
1 package de.intevation.gnv.chart;
2
3 import de.intevation.gnv.jfreechart.PolygonDataset;
4 import de.intevation.gnv.jfreechart.PolygonPlot;
5 import de.intevation.gnv.jfreechart.PolygonRenderer;
6 import de.intevation.gnv.jfreechart.PolygonSeries;
7
8 import de.intevation.gnv.math.AttributedXYColumns;
9
10 import de.intevation.gnv.raster.Palette;
11
12 import java.awt.Color;
13 import java.awt.Paint;
14
15 import java.text.NumberFormat;
16
17 import java.util.HashSet;
18 import java.util.Locale;
19 import java.util.Map;
20
21 import org.jfree.chart.JFreeChart;
22
23 import org.jfree.chart.axis.NumberAxis;
24 import org.jfree.chart.axis.SymbolAxis;
25 import org.jfree.chart.axis.ValueAxis;
26
27 import org.jfree.chart.plot.PlotOrientation;
28
29 import org.jfree.chart.renderer.LookupPaintScale;
30
31 import org.jfree.chart.title.PaintScaleLegend;
32 import org.jfree.chart.title.TextTitle;
33
34 import org.jfree.ui.RectangleEdge;
35 import org.jfree.ui.RectangleInsets;
36
37 /**
38 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
39 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
40 */
41 public class VerticalCrossSectionChart
42 implements Chart
43 {
44 public static final class PalettePaintLookup
45 implements PolygonRenderer.PaintLookup
46 {
47 private Palette palette;
48 private Map<Integer, Paint> special;
49
50 public PalettePaintLookup(Palette palette) {
51 this(palette, null);
52 }
53
54 public PalettePaintLookup(
55 Palette palette,
56 Map<Integer, Paint> special
57 ) {
58 this.palette = palette;
59 this.special = special;
60 }
61
62 public Paint getPaint(int index) {
63 if (special != null) {
64 Paint paint = special.get(index);
65 if (paint != null) {
66 return paint;
67 }
68 }
69 return index < 0
70 ? Color.black
71 : palette.getColor(index);
72 }
73 } // class PalettePaintLookup
74
75 public static class LocalizedLabelGenerator
76 extends PolygonRenderer.DefaultLabelGenerator
77 {
78 protected NumberFormat format;
79
80 public LocalizedLabelGenerator() {
81 }
82
83 public LocalizedLabelGenerator(NumberFormat format) {
84 this.format = format;
85 }
86
87 protected String toString(Object label) {
88 return label instanceof Number
89 ? format.format(((Number)label).doubleValue())
90 : super.toString(label);
91 }
92 } // class LocalizedLabelGenerator
93
94 protected JFreeChart chart;
95
96 protected AttributedXYColumns columns;
97 protected Map<Integer, Paint> special;
98 protected Palette palette;
99 protected Locale locale;
100 protected ChartLabels labels;
101
102 public VerticalCrossSectionChart() {
103 }
104
105 public VerticalCrossSectionChart(
106 AttributedXYColumns columns,
107 Palette palette,
108 Locale locale,
109 ChartLabels labels
110 ) {
111 this(columns, palette, null, locale, labels);
112 }
113
114 public VerticalCrossSectionChart(
115 AttributedXYColumns columns,
116 Palette palette,
117 Map<Integer, Paint> special,
118 Locale locale,
119 ChartLabels labels
120 ) {
121 this.columns = columns;
122 this.palette = palette;
123 this.special = special;
124 this.locale = locale;
125 this.labels = labels;
126 }
127
128 protected JFreeChart createChart() {
129
130 boolean legendB = false;
131 boolean tooltips = false;
132 boolean urls = false;
133
134 PlotOrientation po = PlotOrientation.HORIZONTAL;
135 PolygonDataset data = columns.getPolygonDataset();
136
137 HashSet<Integer> usedColors = new HashSet<Integer>();
138
139 for (int i = data.getSeriesCount()-1; i >= 0; --i) {
140 PolygonSeries ps = data.getSeries(i);
141 Integer fill = (Integer)ps.getAttribute("fill");
142 if (fill != null
143 && (special != null && !special.containsKey(fill))) {
144 usedColors.add(fill);
145 }
146 }
147
148 NumberFormat format = NumberFormat.getInstance(locale);
149 format.setMinimumFractionDigits(0);
150 format.setMaximumFractionDigits(2);
151
152 PolygonRenderer renderer = new PolygonRenderer(
153 new PalettePaintLookup(palette, special),
154 new LocalizedLabelGenerator(format));
155
156 ValueAxis domainAxis = new NumberAxis(this.labels.getDomainAxisLabel());
157 ValueAxis rangeAxis = new NumberAxis(this.labels.getRangeAxisLabel());
158
159 PolygonPlot plot = new PolygonPlot(
160 data,
161 renderer,
162 domainAxis,
163 rangeAxis,
164 null);
165
166 plot.setOutlinePaint(Color.WHITE);
167
168 String [] labels = new String[usedColors.size()];
169
170 int colors = palette.getSize();
171 LookupPaintScale lookupPaint =
172 new LookupPaintScale(-0.5d, labels.length-0.5d, Color.white);
173
174 Color color = null;
175
176 for (int i = 0, j = labels.length-1; i < colors && j >= 0; i++) {
177 if (usedColors.contains(i)) {
178 Palette.Entry entry = palette.getEntryByIndex(i);
179 color = entry.getColor();
180 labels[j] = entry.getDescription();
181 lookupPaint.add(j-0.5d, color);
182 --j;
183 }
184 }
185
186 JFreeChart chart = new JFreeChart(
187 this.labels.getTitle(),
188 JFreeChart.DEFAULT_TITLE_FONT,
189 plot,
190 legendB);
191
192 chart.removeLegend();
193 chart.addSubtitle(new TextTitle(this.labels.getSubtitle()));
194
195 SymbolAxis scale = new SymbolAxis(this.labels.getParameterName(), labels);
196 scale.setRange(-1.5d, labels.length+0.5d);
197 scale.setGridBandsVisible(false);
198 scale.setPlot(plot);
199
200 PaintScaleLegend legend = new PaintScaleLegend(
201 lookupPaint, scale);
202 legend.setMargin(new RectangleInsets(3d, 10d, 3d, 10d));
203 legend.setPosition(RectangleEdge.LEFT);
204 legend.setAxisOffset(5d);
205
206 chart.addSubtitle(legend);
207
208 return chart;
209 }
210
211 public JFreeChart generateChart() {
212 if (chart == null) {
213 chart = createChart();
214 }
215
216 return chart;
217 }
218
219 public void setBackgroundPaint(Paint paint) {
220 chart.setBackgroundPaint(paint);
221 }
222 }
223 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org