Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/AnnotationRenderer.java @ 9555:ef5754ba5573
Implemented legend aggregation based on type of themes.
Added theme-editor style configuration for aggregated legend entries.
Only configured themes get aggregated.
author | gernotbelger |
---|---|
date | Tue, 23 Oct 2018 16:26:48 +0200 |
parents | 1cc7653ca84f |
children |
comparison
equal
deleted
inserted
replaced
9554:33ce8eba9806 | 9555:ef5754ba5573 |
---|---|
9 */ | 9 */ |
10 package org.dive4elements.river.exports; | 10 package org.dive4elements.river.exports; |
11 | 11 |
12 import java.awt.BasicStroke; | 12 import java.awt.BasicStroke; |
13 import java.awt.Color; | 13 import java.awt.Color; |
14 import java.awt.Font; | |
15 import java.util.ArrayList; | 14 import java.util.ArrayList; |
16 import java.util.List; | 15 import java.util.List; |
17 import java.util.Map; | 16 import java.util.Map; |
18 import java.util.SortedMap; | 17 import java.util.SortedMap; |
19 | 18 |
24 import org.dive4elements.river.jfree.StickyAxisAnnotation; | 23 import org.dive4elements.river.jfree.StickyAxisAnnotation; |
25 import org.dive4elements.river.themes.LineStyle; | 24 import org.dive4elements.river.themes.LineStyle; |
26 import org.dive4elements.river.themes.TextStyle; | 25 import org.dive4elements.river.themes.TextStyle; |
27 import org.dive4elements.river.themes.ThemeDocument; | 26 import org.dive4elements.river.themes.ThemeDocument; |
28 import org.jfree.chart.LegendItem; | 27 import org.jfree.chart.LegendItem; |
29 import org.jfree.chart.LegendItemCollection; | |
30 import org.jfree.chart.annotations.XYLineAnnotation; | 28 import org.jfree.chart.annotations.XYLineAnnotation; |
31 import org.jfree.chart.annotations.XYTextAnnotation; | 29 import org.jfree.chart.annotations.XYTextAnnotation; |
32 import org.jfree.chart.plot.XYPlot; | 30 import org.jfree.chart.plot.XYPlot; |
33 import org.jfree.chart.renderer.xy.XYItemRenderer; | 31 import org.jfree.chart.renderer.xy.XYItemRenderer; |
34 import org.jfree.ui.TextAnchor; | 32 import org.jfree.ui.TextAnchor; |
40 | 38 |
41 private static final Logger log = Logger.getLogger(AnnotationRenderer.class); | 39 private static final Logger log = Logger.getLogger(AnnotationRenderer.class); |
42 | 40 |
43 private static float ANNOTATIONS_AXIS_OFFSET = 0.02f; | 41 private static float ANNOTATIONS_AXIS_OFFSET = 0.02f; |
44 | 42 |
45 private final ChartSettings settings; | |
46 | |
47 private final Map<Integer, AxisDataset> datasets; | 43 private final Map<Integer, AxisDataset> datasets; |
48 | 44 |
49 private final String fontName; | 45 public AnnotationRenderer(final Map<Integer, AxisDataset> datasets) { |
50 | |
51 public AnnotationRenderer(final ChartSettings settings, final Map<Integer, AxisDataset> datasets, final String fontName) { | |
52 this.settings = settings; | |
53 this.datasets = datasets; | 46 this.datasets = datasets; |
54 this.fontName = fontName; | |
55 } | 47 } |
56 | 48 |
57 /** | 49 /** |
58 * Add annotations (Sticky, Text and hyk zones) to a plot. | 50 * Add annotations (Sticky, Text and hyk zones) to a plot. |
59 * | 51 * |
64 * @param settings | 56 * @param settings |
65 * ChartSettings object for settings. | 57 * ChartSettings object for settings. |
66 * @param datasets | 58 * @param datasets |
67 * Map of axis index and datasets | 59 * Map of axis index and datasets |
68 */ | 60 */ |
69 public final void addAnnotationsToRenderer(final XYPlot plot, final List<RiverAnnotation> annotations) { | 61 public final void addAnnotationsToRenderer(final XYPlot plot, final LegendAggregator legendBuilder, final List<RiverAnnotation> annotations) { |
70 if (annotations == null || annotations.isEmpty()) { | 62 if (annotations == null || annotations.isEmpty()) { |
71 log.debug("addAnnotationsToRenderer: no annotations."); | 63 log.debug("addAnnotationsToRenderer: no annotations."); |
72 return; | 64 return; |
73 } | 65 } |
74 | 66 |
86 // Get Theming information and add legend item. | 78 // Get Theming information and add legend item. |
87 if (theme != null) { | 79 if (theme != null) { |
88 textStyle = theme.parseComplexTextStyle(); | 80 textStyle = theme.parseComplexTextStyle(); |
89 lineStyle = theme.parseComplexLineStyle(); | 81 lineStyle = theme.parseComplexLineStyle(); |
90 if (fa.getLabel() != null) { | 82 if (fa.getLabel() != null) { |
91 // Legend handling, maybe misplaced? | |
92 final LegendItemCollection lic = new LegendItemCollection(); | |
93 LegendItemCollection old = plot.getFixedLegendItems(); | |
94 | |
95 Color color = theme.parseLineColorField(); | 83 Color color = theme.parseLineColorField(); |
96 if (color == null) { | 84 if (color == null) { |
97 color = Color.BLACK; | 85 color = Color.BLACK; |
98 } | 86 } |
99 | 87 |
102 textColor = Color.BLACK; | 90 textColor = Color.BLACK; |
103 } | 91 } |
104 | 92 |
105 final LegendItem newItem = new LegendItem(fa.getLabel(), color); | 93 final LegendItem newItem = new LegendItem(fa.getLabel(), color); |
106 | 94 |
107 final LegendSection ls = this.settings != null ? this.settings.getLegendSection() : null; | |
108 | |
109 final Integer size = ls != null ? ls.getFontSize() : null; | |
110 | |
111 newItem.setLabelFont(new Font(this.fontName, Font.PLAIN, size)); | |
112 | |
113 newItem.setLabelPaint(textColor); | 95 newItem.setLabelPaint(textColor); |
114 | 96 |
115 lic.add(newItem); | 97 // REMARK: connection to facet is lost at this point |
116 // (Re-)Add prior legend entries. | 98 final String themeType = null; |
117 if (old != null) { | 99 |
118 old.addAll(lic); | 100 legendBuilder.addLegendItem(themeType, newItem); |
119 } else { | |
120 old = lic; | |
121 } | |
122 plot.setFixedLegendItems(old); | |
123 } | 101 } |
124 } | 102 } |
125 | 103 |
126 // The 'Sticky' Annotations (at axis, with line and text). | 104 // The 'Sticky' Annotations (at axis, with line and text). |
127 for (final StickyAxisAnnotation sta : fa.getAxisTextAnnotations()) { | 105 for (final StickyAxisAnnotation sta : fa.getAxisTextAnnotations()) { |
229 // Add the Annotations to renderer. | 207 // Add the Annotations to renderer. |
230 renderer.addAnnotation(textAnnotation, org.jfree.ui.Layer.FOREGROUND); | 208 renderer.addAnnotation(textAnnotation, org.jfree.ui.Layer.FOREGROUND); |
231 renderer.addAnnotation(lineAnnotation, org.jfree.ui.Layer.FOREGROUND); | 209 renderer.addAnnotation(lineAnnotation, org.jfree.ui.Layer.FOREGROUND); |
232 } | 210 } |
233 | 211 |
234 public final void addYAnnotationsToRenderer(final XYPlot plot, final SortedMap<Integer, RiverAnnotation> yAnnotations) { | 212 public final void addYAnnotationsToRenderer(final XYPlot plot, final LegendAggregator legendBuilder, |
213 final SortedMap<Integer, RiverAnnotation> yAnnotations) { | |
235 final List<RiverAnnotation> annotations = new ArrayList<>(); | 214 final List<RiverAnnotation> annotations = new ArrayList<>(); |
236 | 215 |
237 for (final Map.Entry<Integer, RiverAnnotation> entry : yAnnotations.entrySet()) { | 216 for (final Map.Entry<Integer, RiverAnnotation> entry : yAnnotations.entrySet()) { |
238 final int axis = entry.getKey(); | 217 final int axis = entry.getKey(); |
239 final AxisDataset dataset = this.datasets.get(new Integer(axis)); | 218 final AxisDataset dataset = this.datasets.get(new Integer(axis)); |
247 } | 226 } |
248 annotations.add(ya); | 227 annotations.add(ya); |
249 } | 228 } |
250 } | 229 } |
251 | 230 |
252 addAnnotationsToRenderer(plot, annotations); | 231 addAnnotationsToRenderer(plot, legendBuilder, annotations); |
253 } | 232 } |
254 | 233 |
255 /** | 234 /** |
256 * Create annotation that sticks to "ground" (X) axis. | 235 * Create annotation that sticks to "ground" (X) axis. |
257 * | 236 * |