[Schmitzm-commits] r2415 - in trunk/schmitzm-jfree: doc src/main/java/de/schmitzm/jfree/chart/style
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Mon Aug 31 11:58:04 CEST 2015
Author: mojays
Date: 2015-08-31 11:58:04 +0200 (Mon, 31 Aug 2015)
New Revision: 2415
Modified:
trunk/schmitzm-jfree/doc/Chart style XML structure.doc
trunk/schmitzm-jfree/doc/Chart style XML structure.pdf
trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartRendererStyle.java
trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartStyleUtil.java
trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
Log:
ChartRendererStyle:
- cache mapping "SeriesKey >> SeriesIndex", new methods getSeriesIndex(.), styleUsesSeriesKeys()
- maintain visibility of lines (default and by series)
- BugFix: apply series properties by seriesKey had used wrong index to access ChartRendererStyle arrays
- LegendLabelGenerator/LegendTooltipGenerator: consider seriesKey
ChartStyleUtil:
- new method applyStyleFromXML(.) apply XML Style to existing ChartStyle object
ChartStyleXMLFactory:
- new renderer style property "linesVisible"
Modified: trunk/schmitzm-jfree/doc/Chart style XML structure.doc
===================================================================
(Binary files differ)
Modified: trunk/schmitzm-jfree/doc/Chart style XML structure.pdf
===================================================================
(Binary files differ)
Modified: trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartRendererStyle.java
===================================================================
--- trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartRendererStyle.java 2015-04-08 21:07:16 UTC (rev 2414)
+++ trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartRendererStyle.java 2015-08-31 09:58:04 UTC (rev 2415)
@@ -36,6 +36,7 @@
import java.util.HashMap;
import java.util.Map;
+import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Category;
import org.jfree.chart.labels.CategorySeriesLabelGenerator;
import org.jfree.chart.labels.StandardCategorySeriesLabelGenerator;
@@ -43,8 +44,10 @@
import org.jfree.chart.labels.XYSeriesLabelGenerator;
import org.jfree.chart.renderer.AbstractRenderer;
import org.jfree.chart.renderer.category.AbstractCategoryItemRenderer;
+import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.AbstractXYItemRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
+import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYDataset;
@@ -62,6 +65,7 @@
* <li>dashing attributes for the series graphs</li>
* <li>visibility of series item labels</li>
* <li>visibility of series shapes</li>
+ * <li>visibility of series lines</li>
* <li>the legend labels of the series</li>
* <li>visibility of legend label</li>
* <li>percentage margin between bars (only applicable for {@link XYBarRenderer})</li>
@@ -95,6 +99,9 @@
/** Holds whether the shape of a series is visible (for all series this property
* is not set explicitly). */
protected Boolean defaultShapesVisible = null;
+ /** Holds whether the line of a series is visible (for all series this property
+ * is not set explicitly). */
+ protected Boolean defaultLinesVisible = null;
/** Holds whether the series is visible in the legend (for all series this property
* is not set explicitly). */
protected Boolean defaultLegendVisible = null;
@@ -108,6 +115,9 @@
/** Holds the ID of the series (series key of the dataset) the series style definition has
* to be used for.*/
protected Map<Integer, String> seriesKey = new HashMap<Integer, String>();
+ /** Holds the reverse Mapping of the series key to index in style definition (for performance reasons).*/
+ protected Map<String, Integer> seriesIdx = new HashMap<String,Integer>();
+
/** Holds whether the item labels are visible for a series. */
protected Map<Integer, Boolean> seriesItemLabelsVisible = new HashMap<Integer, Boolean>();
/** Holds the color a series is painted with. */
@@ -119,6 +129,8 @@
protected Map<Integer,Float[]> seriesDashAttr = new HashMap<Integer,Float[]>();
/** Holds whether the shape of a series is visible. */
protected Map<Integer, Boolean> seriesShapesVisible = new HashMap<Integer, Boolean>();
+ /** Holds whether the line of a series is visible. */
+ protected Map<Integer, Boolean> seriesLinesVisible = new HashMap<Integer, Boolean>();
/** Holds whether the series is visible in the legend. */
protected Map<Integer, Boolean> seriesLegendVisible = new HashMap<Integer, Boolean>();
/** Holds the label shown in the legend for the series. */
@@ -170,6 +182,7 @@
dest.setDefaultLineWidth(getDefaultLineWidth() );
dest.setDefaultLineDashAttributes(LangUtil.cloneArray(getDefaultLineDashAttibutes()));
dest.setDefaultShapesVisible(isDefaultShapesVisible() );
+ dest.setDefaultLinesVisible(isDefaultLinesVisible() );
dest.setDefaultLegendVisible(isDefaultLegendVisible() );
if ( getDefaultLegendLabel() != null )
dest.setDefaultLegendLabel(getDefaultLegendLabel().copy() );
@@ -177,6 +190,7 @@
dest.setDefaultLegendTooltip(getDefaultLegendTooltip().copy() );
dest.seriesKey.clear();
+ dest.seriesIdx.clear();
for ( int series : seriesKey.keySet() )
dest.setSeriesKey( series, getSeriesKey(series) );
@@ -200,6 +214,10 @@
for ( int series : seriesShapesVisible.keySet() )
dest.setSeriesShapesVisible( series, isSeriesShapesVisible(series) );
+ dest.seriesLinesVisible.clear();
+ for ( int series : seriesLinesVisible.keySet() )
+ dest.setSeriesLinesVisible( series, isSeriesLinesVisible(series) );
+
dest.seriesLegendVisible.clear();
for ( int series : seriesLegendVisible.keySet() )
dest.setSeriesLegendVisible( series, isSeriesLegendVisible(series) );
@@ -216,8 +234,35 @@
return dest;
}
+
+ /**
+ * Returns the index of the series in the style definition for
+ * a given series key.
+ * @param seriesKey the series key
+ * @return -1 if style contains not series with the key
+ */
+ public int getSeriesIndex(String seriesKey) {
+// for (int i=0; seriesKey != null && i<getSeriesCount(); i++)
+// if ( seriesKey.equals( getSeriesKey(i) ) )
+// return i;
+// return -1;
+ Integer idx = seriesIdx.get(seriesKey);
+ return idx != null ? idx : -1;
+ }
/**
+ * Returns whether the style definition works with series keys
+ * for series style definitions.
+ */
+ public boolean styleUsesSeriesKeys() {
+// for (int i=0; i<getSeriesCount(); i++)
+// if ( !StringUtils.isBlank(getSeriesKey(i)) )
+// return true;
+// return false;
+ return !seriesIdx.isEmpty();
+ }
+
+ /**
* Returns the margin (percentage) between the chart bars. The margin is
* only applicable on bar charts.
*/
@@ -259,6 +304,12 @@
* @param key the series key in the dataset
*/
public void setSeriesKey(int i, String key) {
+ // Update HashMap "SeriesKeys >> Index"
+ String oldSeriesKey = seriesKey.get(i);
+ if (oldSeriesKey != null)
+ seriesIdx.remove(oldSeriesKey);
+ seriesIdx.put(key,i);
+ // Update key for index
seriesKey.put(i,key);
updateSeriesCount();
}
@@ -367,6 +418,26 @@
}
/**
+ * Returns whether the line of a series are visible.
+ * @param series series index
+ * @return {@code null} if property is not set for the series
+ */
+ public Boolean isSeriesLinesVisible(int series) {
+ return seriesLinesVisible.get(series);
+ }
+
+ /**
+ * Sets whether the line of a series are visible.
+ * @param series series index
+ * @param visible indicates the visibility of the series line (can be
+ * {@code null} to reset to a non-specific property)
+ */
+ public void setSeriesLinesVisible(int series, Boolean visible) {
+ seriesLinesVisible.put(series,visible);
+ updateSeriesCount();
+ }
+
+ /**
* Returns whether the series is shown in the legend.
* @param series series index
* @return {@code null} if property is not set for the series
@@ -525,6 +596,25 @@
}
/**
+ * Returns whether the line of a series are visible for these
+ * series this property is not set explicitly.
+ * @return {@code null} if property is not set for the series
+ */
+ public Boolean isDefaultLinesVisible() {
+ return defaultLinesVisible;
+ }
+
+ /**
+ * Sets whether the line of a series are visible for these
+ * series this property is not set explicitly.
+ * @param visible indicates the visibility of the series (can be
+ * {@code null} to reset to a non-specific property)
+ */
+ public void setDefaultLinesVisible(Boolean visible) {
+ defaultLinesVisible = visible;
+ }
+
+ /**
* Returns whether the series is shown in the legend for these
* series this property is not set explicitly.
* @return {@code null} if property is not set for the series
@@ -608,6 +698,7 @@
seriesWidth.keySet(),
seriesDashAttr.keySet(),
seriesShapesVisible.keySet(),
+ seriesLinesVisible.keySet(),
seriesItemLabelsVisible.keySet(),
seriesLegendVisible.keySet(),
seriesLegendLabel.keySet(),
@@ -700,6 +791,14 @@
}
if ( isDefaultLegendVisible() != null )
renderer.setSeriesVisibleInLegend(series, isDefaultLegendVisible(), false);
+
+ // Line visibility (only for [XY]LineAndShapeRenderer)
+ if ( isDefaultLinesVisible() != null && (renderer instanceof LineAndShapeRenderer || renderer instanceof XYLineAndShapeRenderer)) {
+ if ( renderer instanceof LineAndShapeRenderer )
+ ((LineAndShapeRenderer)renderer).setSeriesLinesVisible( series, isDefaultLinesVisible() );
+ if ( renderer instanceof XYLineAndShapeRenderer )
+ ((XYLineAndShapeRenderer)renderer).setSeriesLinesVisible( series, isDefaultLinesVisible() );
+ }
}
// Apply explicit series properties
@@ -721,30 +820,37 @@
}
// Apply item label visibility
- if ( isSeriesItemLabelsVisible(series) != null )
- renderer.setSeriesItemLabelsVisible(series, isSeriesItemLabelsVisible(series), false);
+ if ( isSeriesItemLabelsVisible(i) != null )
+ renderer.setSeriesItemLabelsVisible(series, isSeriesItemLabelsVisible(i), false);
// Apply shape visibility
- if ( isSeriesShapesVisible(series) != null ) {
- Shape shape = isSeriesShapesVisible(series) ? AbstractRenderer.DEFAULT_SHAPE : null;
+ if ( isSeriesShapesVisible(i) != null ) {
+ Shape shape = isSeriesShapesVisible(i) ? AbstractRenderer.DEFAULT_SHAPE : null;
renderer.setSeriesShape(series, shape, false);
}
// Apply rendering color
- if ( getSeriesPaint(series) != null )
- renderer.setSeriesPaint(series, getSeriesPaint(series), false);
+ if ( getSeriesPaint(i) != null )
+ renderer.setSeriesPaint(series, getSeriesPaint(i), false);
// Apply rendering stroke (line width)
- if ( getSeriesLineWidth(series) != null || getSeriesLineDashAttibutes(series) != null ) {
+ if ( getSeriesLineWidth(i) != null || getSeriesLineDashAttibutes(i) != null ) {
float width = getDefaultLineWidth() == null ? 1.0f : getDefaultLineWidth();
Float[] dashAttr = getDefaultLineDashAttibutes();
- if ( getSeriesLineWidth(series) != null )
- width = getSeriesLineWidth(series);
- if ( getSeriesLineDashAttibutes(series) != null )
- dashAttr = getSeriesLineDashAttibutes(series);
+ if ( getSeriesLineWidth(i) != null )
+ width = getSeriesLineWidth(i);
+ if ( getSeriesLineDashAttibutes(i) != null )
+ dashAttr = getSeriesLineDashAttibutes(i);
Stroke stroke = JFreeChartUtil.createDefaultStroke(width, dashAttr);
renderer.setSeriesStroke(series, stroke, false);
}
// Apply legend visibility
- if ( isSeriesLegendVisible(series) != null )
- renderer.setSeriesVisibleInLegend(series, isSeriesLegendVisible(series), false);
+ if ( isSeriesLegendVisible(i) != null )
+ renderer.setSeriesVisibleInLegend(series, isSeriesLegendVisible(i), false);
+ // Line visibility (only for [XY]LineAndShapeRenderer)
+ if ( isSeriesLinesVisible(i) != null && (renderer instanceof LineAndShapeRenderer || renderer instanceof XYLineAndShapeRenderer)) {
+ if ( renderer instanceof LineAndShapeRenderer )
+ ((LineAndShapeRenderer)renderer).setSeriesLinesVisible( series, isSeriesLinesVisible(i) );
+ if ( renderer instanceof XYLineAndShapeRenderer )
+ ((XYLineAndShapeRenderer)renderer).setSeriesLinesVisible( series, isSeriesLinesVisible(i) );
+ }
}
@@ -886,12 +992,15 @@
*/
@Override
public String generateLabel(XYDataset dataset, int series) {
- if ( getSeriesLegendLabel(series) != null )
- return getSeriesLegendLabel(series).getLabel();
- if ( getDefaultLegendLabel() != null )
- return getDefaultLegendLabel().getLabel();
- // if label is not defined, use the standard label generator
- return LABELGEN_XY.generateLabel(dataset, series);
+//MS 2015-08-28.sc Consider SeriesKey mapping!
+// if ( getSeriesLegendLabel(series) != null )
+// return getSeriesLegendLabel(series).getLabel();
+// if ( getDefaultLegendLabel() != null )
+// return getDefaultLegendLabel().getLabel();
+// // if label is not defined, use the standard label generator
+// return LABELGEN_XY.generateLabel(dataset, series);
+ return generateLabel(series, dataset.getSeriesKey(series), LABELGEN_XY.generateLabel(dataset, series) );
+//MS 2015-08-28.ec
}
/**
@@ -902,13 +1011,35 @@
*/
@Override
public String generateLabel(CategoryDataset dataset, int series) {
- if ( getSeriesLegendLabel(series) != null )
- return getSeriesLegendLabel(series).getLabel();
+//MS 2015-08-28.sc Consider SeriesKey mapping!
+// if ( getSeriesLegendLabel(series) != null )
+// return getSeriesLegendLabel(series).getLabel();
+// if ( getDefaultLegendLabel() != null )
+// return getDefaultLegendLabel().getLabel();
+// // if label is not defined, use the standard label generator
+// return LABELGEN_CAT.generateLabel(dataset, series);
+ return generateLabel(series, dataset.getColumnKey(series), LABELGEN_CAT.generateLabel(dataset, series) );
+//MS 2015-08-28.ec
+ }
+
+//MS 2015-08-28.sn
+ private String generateLabel(int series, Comparable<?> seriesKey, String fallbackLabel) {
+ // Convert Comparable to String
+ String seriesKeyStr = seriesKey != null ? seriesKey.toString() : null;
+
+ // Default: standard label generator
+ String legendLabel = fallbackLabel;
+ // Default from style
if ( getDefaultLegendLabel() != null )
- return getDefaultLegendLabel().getLabel();
- // if label is not defined, use the standard label generator
- return LABELGEN_CAT.generateLabel(dataset, series);
+ legendLabel = getDefaultLegendLabel().getLabel();
+ // Find style definition for seriesKey
+ int styleIdx = styleUsesSeriesKeys() ? getSeriesIndex( seriesKeyStr ) : series;
+ if ( styleIdx >= 0 && getSeriesLegendLabel(styleIdx) != null )
+ legendLabel = getSeriesLegendLabel(styleIdx).getLabel();
+
+ return legendLabel;
}
+//MS 2015-08-28.en
}
/**
@@ -930,7 +1061,10 @@
*/
@Override
public String generateLabel(XYDataset dataset, int series) {
- return generateLabel(series);
+//MS 2015-08-28.sc Consider SeriesKey mapping
+// return generateLabel(series);
+ return generateLabel(series, dataset.getSeriesKey(series));
+//MS 2015-08-28.ec
}
/**
@@ -943,17 +1077,41 @@
*/
@Override
public String generateLabel(CategoryDataset dataset, int series) {
- return generateLabel(series);
+//MS 2015-08-28.sc Consider SeriesKey mapping
+// return generateLabel(series);
+ return generateLabel(series, dataset.getColumnKey(series));
+//MS 2015-08-28.ec
}
+
+//MS 2015-08-28.sc Consider SeriesKey mapping
+// public String generateLabel(int series) {
+// if ( getSeriesLegendTooltip(series) != null )
+// return getSeriesLegendTooltip(series).getLabel();
+// if ( getDefaultLegendTooltip() != null )
+// return getDefaultLegendTooltip().getLabel();
+// // if label is not defined, generate no tooltip
+// return null;
+// }
- public String generateLabel(int series) {
- if ( getSeriesLegendTooltip(series) != null )
- return getSeriesLegendTooltip(series).getLabel();
+ private String generateLabel(int series, Comparable<?> seriesKey) {
+ // Convert Comparable to String
+ String seriesKeyStr = seriesKey != null ? seriesKey.toString() : null;
+
+ // Default: no tooltip
+ String tooltipLabel = null;
+ // Default from style
if ( getDefaultLegendTooltip() != null )
- return getDefaultLegendTooltip().getLabel();
- // if label is not defined, generate no tooltip
- return null;
+ tooltipLabel = getDefaultLegendTooltip().getLabel();
+ // Find style definition for seriesKey
+ int styleIdx = styleUsesSeriesKeys() ? getSeriesIndex( seriesKeyStr ) : series;
+ if ( styleIdx >= 0 && getSeriesLegendTooltip(styleIdx) != null )
+ tooltipLabel = getSeriesLegendTooltip(styleIdx).getLabel();
+
+ return tooltipLabel;
}
+//MS 2015-08-28.ec
+
+
}
}
Modified: trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartStyleUtil.java
===================================================================
--- trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartStyleUtil.java 2015-04-08 21:07:16 UTC (rev 2414)
+++ trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartStyleUtil.java 2015-08-31 09:58:04 UTC (rev 2415)
@@ -104,4 +104,33 @@
}
}
+ /**
+ * Applies chart definition from {@link URL} to an existing {@link ChartStyle}.
+ * The URL must refers to a resource which contains exactly one chart definition as root element.
+ * @param style
+ * existing style (if {@code null}, {@link #readStyleFromXML(URL, String, ChartStyleXMLFactory)} is called
+ * and new style object is returned)
+ * @param url
+ * refers to XML resource
+ * @param id
+ * the ID for the style (if {@code null} the ID is taken from
+ * "id" attribute)
+ * @param factory
+ * factory to create the style with
+ * @return {@code style} parameter or generated style if {@code style} is {@code null}
+ */
+ public static <E extends ChartStyle> ChartStyle applyStyleFromXML(E style, URL url, String id,
+ ChartStyleXMLFactory<E> factory)
+ throws IOException {
+ if ( style == null )
+ return readStyleFromXML(url, id, factory);
+
+ try {
+ Document document = SAX_BUILDER.build(url);
+ factory.applyStyleFromXML(style, document.getRootElement());
+ return style;
+ } catch (JDOMException err) {
+ throw new IOException(err);
+ }
+ }
}
Modified: trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
===================================================================
--- trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java 2015-04-08 21:07:16 UTC (rev 2414)
+++ trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java 2015-08-31 09:58:04 UTC (rev 2415)
@@ -265,7 +265,7 @@
style.setPaint(color);
// label in element data
- String label = element.getTextTrim();
+ String label = element != null ? element.getTextTrim() : null;
style.setLabel(label != null ? label : "");
}
@@ -385,6 +385,12 @@
if (defaultShapesVisible != null)
style.setDefaultShapesVisible(defaultShapesVisible);
+ // item line visibility
+ Boolean defaultLinesVisible = XMLUtil.getBooleanAttribute(element,
+ "linesVisible");
+ if (defaultLinesVisible != null)
+ style.setDefaultLinesVisible(defaultLinesVisible);
+
// legend visibility
Boolean defaultLegendVisible = XMLUtil.getBooleanAttribute(element,
"legendVisible");
@@ -433,7 +439,13 @@
if (seriesLineDashAttr != null)
style.setSeriesLineDashAttributes(i, seriesLineDashAttr);
- // item label visibility
+ // item line visibility
+ Boolean seriesLinesVisible = XMLUtil.getBooleanAttribute(seriesElement,
+ "linesVisible");
+ if (seriesLinesVisible != null)
+ style.setSeriesLinesVisible(i, seriesLinesVisible);
+
+ // item label visibility
Boolean labelsVisible = XMLUtil.getBooleanAttribute(seriesElement,
"itemLabelsVisible");
if (labelsVisible != null)
More information about the Schmitzm-commits
mailing list