comparison artifacts/src/main/java/org/dive4elements/river/exports/XYChartGenerator.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 d8e753d0fdb9
children 8f59aa01c22a
comparison
equal deleted inserted replaced
9554:33ce8eba9806 9555:ef5754ba5573
34 import org.jfree.data.Range; 34 import org.jfree.data.Range;
35 import org.jfree.data.general.Series; 35 import org.jfree.data.general.Series;
36 import org.jfree.data.xy.XYDataset; 36 import org.jfree.data.xy.XYDataset;
37 import org.jfree.data.xy.XYSeries; 37 import org.jfree.data.xy.XYSeries;
38 import org.jfree.data.xy.XYSeriesCollection; 38 import org.jfree.data.xy.XYSeriesCollection;
39 import org.jfree.ui.RectangleInsets;
39 import org.json.JSONArray; 40 import org.json.JSONArray;
40 import org.json.JSONException; 41 import org.json.JSONException;
41 42
42 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; 43 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
43 import org.dive4elements.artifacts.CallContext; 44 import org.dive4elements.artifacts.CallContext;
75 public static final int AXIS_SPACE = 5; 76 public static final int AXIS_SPACE = 5;
76 77
77 /** The log that is used in this generator. */ 78 /** The log that is used in this generator. */
78 private static Logger log = Logger.getLogger(XYChartGenerator.class); 79 private static Logger log = Logger.getLogger(XYChartGenerator.class);
79 80
80 protected List<Marker> domainMarkers = new ArrayList<Marker>(); 81 protected List<Marker> domainMarkers = new ArrayList<>();
81 82
82 protected List<Marker> valueMarkers = new ArrayList<Marker>(); 83 protected List<Marker> valueMarkers = new ArrayList<>();
83 84
84 /** The max X range to include all X values of all series for each axis. */ 85 /** The max X range to include all X values of all series for each axis. */
85 protected Map<Integer, Bounds> xBounds; 86 protected Map<Integer, Bounds> xBounds;
86 87
87 /** The max Y range to include all Y values of all series for each axis. */ 88 /** The max Y range to include all Y values of all series for each axis. */
91 private boolean inverted; 92 private boolean inverted;
92 93
93 public XYChartGenerator() { 94 public XYChartGenerator() {
94 super(); 95 super();
95 96
96 xBounds = new HashMap<Integer, Bounds>(); 97 xBounds = new HashMap<>();
97 yBounds = new HashMap<Integer, Bounds>(); 98 yBounds = new HashMap<>();
98 } 99 }
99 100
100 101
101 /** 102 /**
102 * Generate the chart anew (including localized axis and all). 103 * Generate the chart anew (including localized axis and all).
125 plot.setBackgroundPaint(Color.WHITE); 126 plot.setBackgroundPaint(Color.WHITE);
126 adjustPlot(plot); 127 adjustPlot(plot);
127 128
128 //debugAxis(plot); 129 //debugAxis(plot);
129 130
130 addDatasets(plot); 131 final LegendAggregator legendBuilder = createLegendBuilder();
132
133 addDatasets(plot, legendBuilder);
131 134
132 //debugDatasets(plot); 135 //debugDatasets(plot);
133 136
134 addMarkers(plot); 137 addMarkers(plot);
135 138
149 autoZoom(plot); 152 autoZoom(plot);
150 } 153 }
151 154
152 //debugAxis(plot); 155 //debugAxis(plot);
153 156
157
154 // These have to go after the autozoom. 158 // These have to go after the autozoom.
155 addAnnotationsToRenderer(plot); 159 addAnnotationsToRenderer(plot, legendBuilder);
156 160
157 // Add a logo (maybe). 161 // Add a logo (maybe).
158 addLogo(plot); 162 addLogo(plot);
159 163
160 aggregateLegendEntries(plot); 164 legendBuilder.apply(context, plot);
161 165
162 return chart; 166 return chart;
163 } 167 }
164 168
165 169
313 @Override 317 @Override
314 protected Series getSeriesOf(XYDataset dataset, int idx) { 318 protected Series getSeriesOf(XYDataset dataset, int idx) {
315 return ((XYSeriesCollection) dataset).getSeries(idx); 319 return ((XYSeriesCollection) dataset).getSeries(idx);
316 } 320 }
317 321
318
319 @Override
320 protected AxisDataset createAxisDataset(int idx) {
321 log.debug("Create new AxisDataset for index: " + idx);
322 return new AxisDataset(idx);
323 }
324
325
326 /** 322 /**
327 * Put debug output about datasets. 323 * Put debug output about datasets.
328 */ 324 */
329 public void debugDatasets(XYPlot plot) { 325 public void debugDatasets(XYPlot plot) {
330 log.debug("Number of datasets: " + plot.getDatasetCount()); 326 log.debug("Number of datasets: " + plot.getDatasetCount());
909 ThemeDocument theme, 905 ThemeDocument theme,
910 boolean visible, 906 boolean visible,
911 int axisIndex 907 int axisIndex
912 ) { 908 ) {
913 String seriesName = aandf.getFacetDescription(); 909 String seriesName = aandf.getFacetDescription();
914 XYSeries series = new StyledXYSeries(seriesName, theme); 910 XYSeries series = new StyledXYSeries(aandf.getFacetName(), seriesName, theme);
915 911
916 // Add text annotations for single points. 912 // Add text annotations for single points.
917 List<XYTextAnnotation> xy = new ArrayList<XYTextAnnotation>(); 913 List<XYTextAnnotation> xy = new ArrayList<XYTextAnnotation>();
918 914
919 try { 915 try {
944 // (null was passed for the aand before). 940 // (null was passed for the aand before).
945 doAnnotations(annotations, null, theme, visible); 941 doAnnotations(annotations, null, theme, visible);
946 addAxisSeries(series, axisIndex, visible); 942 addAxisSeries(series, axisIndex, visible);
947 } 943 }
948 944
949
950 /**
951 * Create a hash from a legenditem.
952 * This hash can then be used to merge legend items labels.
953 * @return hash for given legenditem to identify mergeables.
954 */
955 public static String legendItemHash(LegendItem li) {
956 // TODO Do proper implementation.
957 // Ensure that only mergable sets are created.
958 // getFillPaint()
959 // getFillPaintTransformer()
960 // getLabel()
961 // getLine()
962 // getLinePaint()
963 // getLineStroke()
964 // getOutlinePaint()
965 // getOutlineStroke()
966 // Shape getShape()
967 // String getToolTipText()
968 // String getURLText()
969 // boolean isLineVisible()
970 // boolean isShapeFilled()
971 // boolean isShapeOutlineVisible()
972 // boolean isShapeVisible()
973 String hash = li.getLinePaint().toString();
974 String label = li.getLabel();
975 if (label.startsWith("W (") || label.startsWith("W(")) {
976 hash += "-W-";
977 }
978 else if (label.startsWith("Q(") || label.startsWith("Q (")) {
979 hash += "-Q-";
980 }
981
982 // WQ.java holds example of using regex Matcher/Pattern.
983
984 return hash;
985 }
986
987 /** True if x axis has been inverted. */ 945 /** True if x axis has been inverted. */
988 public boolean isInverted() { 946 public boolean isInverted() {
989 return inverted; 947 return inverted;
990 } 948 }
991 949

http://dive4elements.wald.intevation.org