diff artifacts/src/main/java/org/dive4elements/river/exports/AbstractChartGenerator.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 9dc6427059b2
children
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/AbstractChartGenerator.java	Mon Oct 22 18:26:05 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/AbstractChartGenerator.java	Tue Oct 23 16:26:48 2018 +0200
@@ -58,7 +58,6 @@
 import org.dive4elements.river.themes.ThemeDocument;
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.LegendItem;
-import org.jfree.chart.LegendItemCollection;
 import org.jfree.chart.axis.NumberAxis;
 import org.jfree.chart.plot.XYPlot;
 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
@@ -96,6 +95,8 @@
 
     private static final String DEFAULT_CHART_FORMAT = "png";
 
+    private static final int DEFAULT_LEGEND_AGGREGATION_THRESHOLD = 10;
+
     private static final String XPATH_CHART_EXPORT = "/art:action/art:attributes/art:export/@art:value";
 
     private static final String XPATH_CHART_SIZE = "/art:action/art:attributes/art:size";
@@ -224,15 +225,6 @@
     protected abstract String getDefaultChartTitle(CallContext context);
 
     /**
-     * This method is used to create new AxisDataset instances which may differ
-     * in concrete subclasses.
-     *
-     * @param idx
-     *            The index of an axis.
-     */
-    protected abstract AxisDataset createAxisDataset(int idx);
-
-    /**
      * Combines the ranges of the X axis at index <i>idx</i>.
      *
      * @param bounds
@@ -340,7 +332,7 @@
         final LegendSection legendSection = new LegendSection();
         legendSection.setVisibility(isLegendVisible());
         legendSection.setFontSize(getLegendFontSize());
-        legendSection.setAggregationThreshold(10);
+        legendSection.setAggregationThreshold(DEFAULT_LEGEND_AGGREGATION_THRESHOLD);
         return legendSection;
     }
 
@@ -686,46 +678,25 @@
         return fontSize;
     }
 
-    /**
-     * Creates a new LegendItem with <i>name</i> and font provided by
-     * <i>createLegendLabelFont()</i>.
-     *
-     * @param theme
-     *            The theme of the chart line.
-     * @param name
-     *            The displayed name of the item.
-     *
-     * @return a new LegendItem instance.
-     */
-    protected final LegendItem createLegendItem(final ThemeDocument theme, final String name) {
-        // OPTIMIZE Pass font, parsed Theme items.
-
-        Color color = theme.parseLineColorField();
-        if (color == null)
-            color = Color.BLACK;
-
-        final LegendItem legendItem = new LegendItem(name, color);
-        legendItem.setLabelFont(createLegendLabelFont());
-        return legendItem;
+    protected final LegendAggregator createLegendBuilder() {
+        return new LegendAggregator(getLegendAggregationThreshold(), createLegendLabelFont());
     }
 
-    /**
-     * Create new legend entries, dependent on settings.
-     *
-     * @param plot
-     *            The plot for which to modify the legend.
-     */
-    protected final void aggregateLegendEntries(final XYPlot plot) {
+    private int getLegendAggregationThreshold() {
 
         final ChartSettings chartSettings = getChartSettings();
         if (chartSettings == null)
-            return;
+            return DEFAULT_LEGEND_AGGREGATION_THRESHOLD;
 
-        final Integer threshold = chartSettings.getLegendSection().getAggregationThreshold();
+        final LegendSection legendSection = chartSettings.getLegendSection();
+        if (legendSection == null)
+            return DEFAULT_LEGEND_AGGREGATION_THRESHOLD;
 
-        final int aggrThreshold = threshold != null ? threshold.intValue() : 0;
+        final Integer threshold = legendSection.getAggregationThreshold();
+        if (threshold == null)
+            return DEFAULT_LEGEND_AGGREGATION_THRESHOLD;
 
-        LegendProcessor.aggregateLegendEntries(plot, aggrThreshold);
+        return threshold;
     }
 
     /**
@@ -818,7 +789,7 @@
      * @param plot
      *            plot to add datasets to.
      */
-    protected void addDatasets(final XYPlot plot) {
+    protected void addDatasets(final XYPlot plot, final LegendAggregator legendBuilder) {
         log.debug("addDatasets()");
 
         // AxisDatasets are sorted, but some might be empty.
@@ -846,7 +817,7 @@
                         plot.setDataset(datasetIndex, dataset);
                         plot.mapDatasetToRangeAxis(datasetIndex, axisIndex);
 
-                        applyThemes(plot, dataset, datasetIndex);
+                        applyThemes(plot, legendBuilder, dataset, datasetIndex);
 
                         datasetIndex++;
                     }
@@ -855,7 +826,6 @@
                     }
                 }
 
-                axisDataset.setPlotAxisIndex(axisIndex);
                 axisIndex++;
             }
         }
@@ -875,14 +845,12 @@
      *            true if the series describes an area and shall be rendered
      *            as such.
      */
-    private void applyThemes(final XYPlot plot, final XYDataset dataset, final int datasetIndex) {
-
-        final Font legendFont = createLegendLabelFont();
+    private void applyThemes(final XYPlot plot, final LegendAggregator legendBuilder, final XYDataset dataset, final int datasetIndex) {
 
         if (dataset instanceof StyledXYDataset)
-            ((StyledXYDataset) dataset).applyTheme(this.context.getMeta(), plot, datasetIndex, legendFont);
+            ((StyledXYDataset) dataset).applyTheme(this.context.getMeta(), plot, legendBuilder, datasetIndex);
         else
-            applyLineTheme(plot, dataset, datasetIndex, legendFont);
+            applyLineTheme(plot, legendBuilder, dataset, datasetIndex);
     }
 
     /**
@@ -933,12 +901,9 @@
      *            The index of the renderer / dataset.
      * @param legendFont2
      */
-    private void applyLineTheme(final XYPlot plot, final XYDataset dataset, final int idx, final Font legendFont) {
+    private void applyLineTheme(final XYPlot plot, final LegendAggregator legendBuilder, final XYDataset dataset, final int idx) {
         log.debug("Apply LineTheme for dataset at index: " + idx);
 
-        final LegendItemCollection lic = new LegendItemCollection();
-        final LegendItemCollection anno = plot.getFixedLegendItems();
-
         final XYLineAndShapeRenderer renderer = createRenderer(plot, idx);
 
         for (int s = 0, num = dataset.getSeriesCount(); s < num; s++) {
@@ -952,29 +917,24 @@
             // special case: if there is just one single item, we need to enable
             // points for this series, otherwise we would not see anything in
             // the chart area.
-            if (series.getItemCount() == 1) {
-                renderer.setSeriesShapesVisible(s, true);
-            }
+            // if (series.getItemCount() == 1) {
+            // renderer.setSeriesShapesVisible(s, true);
+            // }
 
             LegendItem legendItem = renderer.getLegendItem(idx, s);
             if (legendItem.getLabel().endsWith(" ")) {
                 legendItem = null;
             }
 
+            final String themeType = series instanceof StyledSeries ? ((StyledSeries) series).getThemeType() : null;
+
             if (legendItem != null) {
-                legendItem.setLabelFont(legendFont);
-                lic.add(legendItem);
+                legendBuilder.addLegendItem(themeType, legendItem);
             } else {
                 log.warn("Could not get LegentItem for renderer: " + idx + ", series-idx " + s);
             }
         }
 
-        if (anno != null) {
-            lic.addAll(anno);
-        }
-
-        plot.setFixedLegendItems(lic);
-
         plot.setRenderer(idx, renderer);
     }
 
@@ -1077,7 +1037,7 @@
         AxisDataset axisDataset = this.datasets.get(idx);
 
         if (axisDataset == null) {
-            axisDataset = createAxisDataset(idx);
+            axisDataset = new AxisDataset();
             this.datasets.put(idx, axisDataset);
         }
 
@@ -1321,12 +1281,12 @@
         return true;
     }
 
-    protected final void addAnnotationsToRenderer(final XYPlot plot) {
+    protected final void addAnnotationsToRenderer(final XYPlot plot, final LegendAggregator legendBuilder) {
 
-        final AnnotationRenderer annotationRenderer = new AnnotationRenderer(getChartSettings(), this.datasets, DEFAULT_FONT_NAME);
-        annotationRenderer.addAnnotationsToRenderer(plot, this.annotations);
+        final AnnotationRenderer annotationRenderer = new AnnotationRenderer(this.datasets);
+        annotationRenderer.addAnnotationsToRenderer(plot, legendBuilder, this.annotations);
 
-        doAddFurtherAnnotations(plot, this.annotations);
+        doAddFurtherAnnotations(plot, legendBuilder, this.annotations);
     }
 
     /**
@@ -1334,7 +1294,7 @@
      *
      * Does nothing by default.
      */
-    protected void doAddFurtherAnnotations(final XYPlot plot, final List<RiverAnnotation> annotations) {
+    protected void doAddFurtherAnnotations(final XYPlot plot, final LegendAggregator legendBuilder, final List<RiverAnnotation> annotations) {
 
     }
 }
\ No newline at end of file

http://dive4elements.wald.intevation.org