[Schmitzm-commits] r170 - in trunk/src/schmitzm: jfree jfree/chart/style xml
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Tue Jun 30 21:41:57 CEST 2009
Author: mojays
Date: 2009-06-30 21:41:53 +0200 (Tue, 30 Jun 2009)
New Revision: 170
Added:
trunk/src/schmitzm/jfree/chart/style/ChartPlotStyle.java
trunk/src/schmitzm/jfree/chart/style/ChartRendererStyle.java
Modified:
trunk/src/schmitzm/jfree/JFreeChartUtil.java
trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java
trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java
trunk/src/schmitzm/jfree/chart/style/ChartLabelStyle.java
trunk/src/schmitzm/jfree/chart/style/ChartStyle.java
trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java
trunk/src/schmitzm/xml/XMLUtil.java
Log:
XML chart style definition extended (plot, renderer, ...)
Modified: trunk/src/schmitzm/jfree/JFreeChartUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/JFreeChartUtil.java 2009-06-30 08:03:29 UTC (rev 169)
+++ trunk/src/schmitzm/jfree/JFreeChartUtil.java 2009-06-30 19:41:53 UTC (rev 170)
@@ -22,13 +22,20 @@
import org.geotools.feature.FeatureIterator;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
+import org.jfree.chart.event.PlotChangeEvent;
+import org.jfree.chart.event.PlotChangeListener;
+import org.jfree.chart.event.RendererChangeEvent;
+import org.jfree.chart.event.RendererChangeListener;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.renderer.AbstractRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
+import org.jfree.chart.title.LegendTitle;
+import org.jfree.chart.title.Title;
import org.jfree.chart.urls.StandardXYURLGenerator;
import org.jfree.chart.urls.XYURLGenerator;
import org.jfree.data.function.Function2D;
@@ -64,6 +71,35 @@
public static ResourceProvider RESOURCE = new ResourceProvider( LangUtil.extendPackagePath(JFreeChartUtil.class,"resource.locales.JFreeResourceBundle"), Locale.ENGLISH );
/**
+ * Sets all chart legends (in)visible.
+ * @param chart a chart to apply
+ * @param visible indicates the visible property
+ */
+ public static void setLegendVisible(JFreeChart chart, boolean visible) {
+ for (Object title : chart.getSubtitles())
+ if ( title instanceof LegendTitle )
+ ((Title)title).setVisible(visible);
+ }
+
+ /**
+ * Fires a change event to all {@link PlotChangeListener} connected
+ * to the plot.
+ * @param plot a plot
+ */
+ public static void fireChangeEvent(Plot plot) {
+ plot.notifyListeners( new PlotChangeEvent(plot) );
+ }
+
+ /**
+ * Fires a change event to all {@link RendererChangeListener} connected
+ * to the renderer.
+ * @param renderer a renderer
+ */
+ public static void fireChangeEvent(AbstractRenderer renderer) {
+ renderer.notifyListeners( new RendererChangeEvent(renderer) );
+ }
+
+ /**
* Creates a {@link XYDataset} for 2 attributes of a {@link FeatureCollection}.
* @param fc a {@link FeatureCollection}
* @param seriesKey a (unique) key for the {@link XYSeries} in the dataset
@@ -192,7 +228,7 @@
/**
* @return all {@link DatasetSelectionModel DatasetSelectionModels} that can be reached via the renderers of this chart.
*/
-public static List<FeatureDatasetSelectionModel<?>> getFeatureDatasetSelectionModelFor(JFreeChart chart) {
+ public static List<FeatureDatasetSelectionModel<?>> getFeatureDatasetSelectionModelFor(JFreeChart chart) {
ArrayList<FeatureDatasetSelectionModel<?>> renderers = new ArrayList<FeatureDatasetSelectionModel<?>>();
@@ -213,7 +249,7 @@
// TODO Other types of Charts have the renderers hidden somewhere else.
return renderers;
-}
+ }
Modified: trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java 2009-06-30 08:03:29 UTC (rev 169)
+++ trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java 2009-06-30 19:41:53 UTC (rev 170)
@@ -11,13 +11,31 @@
package schmitzm.jfree.chart.style;
import java.awt.Color;
+import java.awt.Paint;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.axis.Axis;
+import org.jfree.chart.labels.CategoryToolTipGenerator;
+import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
+import org.jfree.chart.labels.StandardXYToolTipGenerator;
+import org.jfree.chart.labels.XYToolTipGenerator;
+import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.title.TextTitle;
+import org.jfree.chart.urls.CategoryURLGenerator;
+import org.jfree.chart.urls.StandardCategoryURLGenerator;
+import org.jfree.chart.urls.StandardXYURLGenerator;
+import org.jfree.chart.urls.XYURLGenerator;
+import org.jfree.data.general.Dataset;
+
+import schmitzm.jfree.JFreeChartUtil;
+import schmitzm.lang.LangUtil;
/**
* This class provides an abstract implementation for chart styles.
@@ -26,15 +44,29 @@
* @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
* @version 1.0
*/
-public abstract class AbstractChartStyle implements ChartStyle {
+public abstract class AbstractChartStyle implements ChartStyle {
+ /** Tooltip generator used for XY-Charts. */
+ protected XYToolTipGenerator TIPGEN_XY = new StandardXYToolTipGenerator();
+ /** Tooltip generator used for Category-Charts. */
+ protected CategoryToolTipGenerator TIPGEN_CAT = new StandardCategoryToolTipGenerator();
+ /** URL generator used for XY-Charts. */
+ protected XYURLGenerator URLGEN_XY = new StandardXYURLGenerator();
+ /** URL generator used for Category-Charts. */
+ protected CategoryURLGenerator URLGEN_CAT = new StandardCategoryURLGenerator();
+
+
/** Logger for this class */
- protected static final Category LOGGER = Logger.getLogger(AbstractChartStyle.class.getName());
+ protected final Category LOGGER = LangUtil.createLogger(this);
/** Holds the chart type.
* @see ChartStyle#ChartType */
protected ChartType type = null;
/** Holds the chart title. */
- protected ChartLabelStyle titleStyle = new ChartLabelStyle();
+ protected ChartLabelStyle titleStyle = new ChartLabelStyle();
+ /** Holds the background color for the chart. */
+ protected Paint bgPaint = null;
+ /** Stores whether a border is shown around the whole chart. */
+ protected boolean borderVisible = false;
/** Holds the styles of the chart axis. */
protected Map<Integer,ChartAxisStyle> axisStyle = new HashMap<Integer,ChartAxisStyle>();
/** Holds the plot orientation (horizontal/vertical). */
@@ -45,6 +77,10 @@
protected boolean tooltips = true;
/** Stores whether URLs are generated for the chart (Default: true). */
protected boolean urls = true;
+ /** Holds the styles for the chart renderers. */
+ protected Map<Integer,ChartRendererStyle> rendererStyle = new HashMap<Integer,ChartRendererStyle>();
+ /** Holds the style for the chart's plot. */
+ protected ChartPlotStyle plotStyle = null;
/**
* Creates a line style with default values.
@@ -72,7 +108,7 @@
* @param urls flag whether URLs are generated
*/
public AbstractChartStyle(ChartType type,
- String title, String xTitle, String yTitle,
+ String title, String xTitle, String yTitle,
boolean legend, boolean tooltips, boolean urls) {
setType(type);
setLegend(legend);
@@ -80,9 +116,9 @@
setURLs(urls);
getTitleStyle().setLabel(title);
if ( xTitle != null )
- setAxisStyle(DOMAIN_AXIS, new ChartAxisStyle(xTitle,Color.BLACK,0,0) );
+ setAxisStyle(DOMAIN_AXIS, new ChartAxisStyle(xTitle,Color.BLACK,0.0,0.0) );
if ( yTitle != null )
- setAxisStyle(RANGE_AXIS, new ChartAxisStyle(yTitle,Color.BLACK,0,0) );
+ setAxisStyle(RANGE_AXIS, new ChartAxisStyle(yTitle,Color.BLACK,0.0,0.0) );
}
/**
@@ -135,6 +171,35 @@
titleStyle = style;
}
+ /**
+ * Returns the background color of the chart.
+ */
+ public Paint getBackground() {
+ return bgPaint;
+ }
+
+ /**
+ * Sets the background color of the chart.
+ * @param paint the background color for the chart
+ */
+ public void setBackground(Paint paint) {
+ this.bgPaint = paint;
+ }
+
+ /**
+ * Returns whether a border is shown around the whole chart.
+ */
+ public boolean isBorderVisible() {
+ return this.borderVisible;
+ }
+
+ /**
+ * Sets whether a border is shown around the whole chart.
+ */
+ public void setBorderVisible(boolean visible) {
+ this.borderVisible = visible;
+ }
+
/**
* Returns whether the chart is configured to generate a legend.
*/
@@ -201,5 +266,97 @@
public void setAxisStyle(int axis, ChartAxisStyle style) {
axisStyle.put(axis, style);
}
+
+ /**
+ * Returns the style of a dataset renderer.
+ * @param dataset dataset number (starting with 0)
+ * @return {@code null} if the chart has no such renderer
+ */
+ public ChartRendererStyle getRendererStyle(int dataset) {
+ return rendererStyle.get(dataset);
+ }
+
+ /**
+ * Sets the style of a dataset renderer.
+ * @param dataset dataset number (starting with 0)
+ * @param style style for the dataset renderer
+ */
+ public void setRendererStyle(int dataset, ChartRendererStyle style) {
+ rendererStyle.put(dataset, style);
+ }
+ /**
+ * Returns the style of the chart's plot.
+ * @return {@code null} if no such style is defined
+ */
+ public ChartPlotStyle getPlotStyle() {
+ return plotStyle;
+ }
+
+ /**
+ * Sets the style of the chart's plot.
+ * @param style style for the plot
+ */
+ public void setPlotStyle(ChartPlotStyle style) {
+ plotStyle = style;
+ }
+
+ /**
+ * Applies the properties of this style to an existing chart.
+ * @param chart chart to be styled
+ */
+ public void applyToChart(JFreeChart chart) {
+ // Style the common attributes
+ if ( getBackground() != null )
+ chart.setBackgroundPaint( getBackground() );
+ if ( chart.getPlot() instanceof XYPlot ) {
+ XYPlot plot = chart.getXYPlot();
+ plot.getRenderer().setBaseToolTipGenerator( hasTooltips() ? TIPGEN_XY : null );
+ plot.getRenderer().setURLGenerator( hasURLs() ? URLGEN_XY : null );
+ if ( getOrientation() != null )
+ plot.setOrientation( getOrientation() );
+ } else if ( chart.getPlot() instanceof CategoryPlot ) {
+ CategoryPlot plot = chart.getCategoryPlot();
+ plot.getRenderer().setBaseToolTipGenerator( hasTooltips() ? TIPGEN_CAT : null );
+ plot.getRenderer().setBaseItemURLGenerator( hasURLs() ? URLGEN_CAT : null );
+ if ( getOrientation() != null )
+ plot.setOrientation( getOrientation() );
+ }
+
+ // Style the legend
+ JFreeChartUtil.setLegendVisible(chart, hasLegend());
+
+ // Style the chart title
+ TextTitle title = chart.getTitle();
+ if ( title == null && getTitleStyle() != null ) {
+ title = new TextTitle();
+ chart.setTitle( title );
+ }
+ if ( getTitleStyle().getLabel() != null )
+ title.setText( getTitleStyle().getLabel() );
+ if ( getTitleStyle().getPaint() != null )
+ title.setPaint( getTitleStyle().getPaint() );
+
+ // Style the chart axis
+ Vector<Axis> chartAxis = new Vector<Axis>();
+ if ( chart.getPlot() instanceof XYPlot ) {
+ XYPlot plot = chart.getXYPlot();
+ if ( plot.getDomainAxisCount() > 0 )
+ chartAxis.add( plot.getDomainAxis(0) );
+ for (int i=0; i<plot.getRangeAxisCount(); i++)
+ chartAxis.add( plot.getRangeAxis(i) );
+ }
+ if ( chart.getPlot() instanceof CategoryPlot ) {
+ CategoryPlot plot = chart.getCategoryPlot();
+ if ( plot.getDomainAxisCount() > 0 )
+ chartAxis.add( plot.getDomainAxis(0) );
+ for (int i=0; i<plot.getRangeAxisCount(); i++)
+ chartAxis.add( plot.getRangeAxis(i) );
+ }
+ for (int i=0; i<chartAxis.size(); i++)
+ if ( i<getAxisCount() )
+ getAxisStyle(i).applyToAxis( chartAxis.elementAt(i) );
+ else
+ LOGGER.warn("Style contains no style definition for axis "+i);
+ }
}
Modified: trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java 2009-06-30 08:03:29 UTC (rev 169)
+++ trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java 2009-06-30 19:41:53 UTC (rev 170)
@@ -176,7 +176,8 @@
break;
case BAR: chart = createBarChart(dataset);
break;
- }
+ }
+
return chart;
}
Modified: trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java 2009-06-30 08:03:29 UTC (rev 169)
+++ trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java 2009-06-30 19:41:53 UTC (rev 170)
@@ -13,21 +13,31 @@
import java.awt.Color;
import java.awt.Paint;
+import java.text.DateFormat;
import java.text.Format;
+import java.text.NumberFormat;
+import org.apache.log4j.Category;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.Axis;
+import org.jfree.chart.axis.CategoryAxis;
+import org.jfree.chart.axis.CategoryLabelPositions;
+import org.jfree.chart.axis.DateAxis;
+import org.jfree.chart.axis.NumberAxis;
import org.jfree.data.general.Dataset;
+
+import schmitzm.lang.LangUtil;
/**
- * This interface is a general super class for a design style of a chart
- * {@link Axis}.
+ * This class is defines the design style of a chart {@link Axis}.
* @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
* @version 1.0
*/
public class ChartAxisStyle extends ChartLabelStyle {
+ /** Holds the angle (in degrees) the label is rotated by. */
+ protected Double angleDegr = 0.0;
/** Holds the angle (in degrees) the axis values are rotated by. */
- protected double valuesAngleDegr = 0;
+ protected Double valuesAngleDegr = 0.0;
/** Holds whether the axis is visible. */
protected boolean visible = true;
/** Holds the number format to display the axis values. */
@@ -47,12 +57,35 @@
* @param labelAngle angel (in degrees) the axis label is rotated by
* @param valuesAngle angel (in degrees) the axis values are rotated by
*/
- public ChartAxisStyle(String title, Paint color, double labelAngle, double valuesAngle) {
- super(title,color,labelAngle);
+ public ChartAxisStyle(String title, Paint color, Double labelAngle, Double valuesAngle) {
+ super(title,color);
+ setLabelAngle(labelAngle);
setValuesAngle(valuesAngle);
}
/**
+ * Returns the angle (in degrees) the label text is rotated by.
+ */
+ public Double getLabelAngle() {
+ return angleDegr;
+ }
+
+ /**
+ * Returns the angle (in radian) the label text is rotated by.
+ */
+ public Double getLabelAngleRadian() {
+ return angleDegr == null ? null : angleDegr * Math.PI / 180;
+ }
+
+ /**
+ * Sets the angle (in degrees) the label text is rotated by.
+ * @param color Color for the label text
+ */
+ public void setLabelAngle(Double angle) {
+ this.angleDegr = angle;
+ }
+
+ /**
* Returns the angle (in degrees) the axis values are rotated by.
*/
public double getValuesAngle() {
@@ -101,4 +134,96 @@
public void setValuesFormat(Format format) {
this.valuesFormat = format;
}
+
+ /**
+ * Applies the style to a specific axis.<br>
+ * Note: Some style properties can only be applied to specific axis types.
+ * @param axis an axis
+ */
+ public void applyToAxis(Axis axis) {
+ if (axis == null) {
+ LOGGER.debug("No axis to be customised.");
+ return;
+ }
+
+ // Apply common attributes
+ axis.setLabel( getLabel() );
+ axis.setLabelPaint( getPaint() );
+ axis.setLabelAngle( getLabelAngle() );
+ axis.setVisible( isVisible() );
+
+ // Apply values format and angel according to the specific axis type
+ if (axis instanceof CategoryAxis)
+ applyToCategoryAxis((CategoryAxis)axis);
+ else if (axis instanceof NumberAxis)
+ applyToNumberAxis((NumberAxis)axis);
+ else if (axis instanceof DateAxis)
+ applyToDateAxis((DateAxis) axis);
+ else
+ LOGGER.warn("Only basic style properties applied to axis because of an unexpected axis type: " + LangUtil.getSimpleClassName(axis));
+ }
+
+
+ /**
+ * Applies style properties which can only be applied to
+ * {@link CategoryAxis} (values angle).<br>
+ * Called by {@link #applyToAxis(Axis)}.
+ * @param axis a {@link CategoryAxis}
+ */
+ protected void applyToCategoryAxis(CategoryAxis axis) {
+ // any angle can be applied
+ axis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions( getValuesAngleRadian() ));
+ // format can not be applied to category axis values,
+ // so give a warn if style should apply one
+ if ( getValuesFormat() != null )
+ LOGGER.warn("Not any axis values format can be applied to "+LangUtil.getSimpleClassName(axis));
+ }
+
+ /**
+ * Applies style properties which can only be applied to
+ * {@link NumberAxis} (values angle and format).<br>
+ * Called by {@link #applyToAxis(Axis)}.
+ * @param axis a {@link NumberAxis}
+ */
+ protected void applyToNumberAxis(NumberAxis axis) {
+ // Only NumberFormat can be applied to NumberAxis
+ if ( getValuesFormat() instanceof NumberFormat )
+ axis.setNumberFormatOverride( (NumberFormat)getValuesFormat() );
+ else
+ // Give a warn if another format should be applied
+ if ( getValuesFormat() != null )
+ LOGGER.warn("Only a NumberFormat can not be applied to values of "+LangUtil.getSimpleClassName(axis)+": "+LangUtil.getSimpleClassName(getValuesFormat()));
+
+ // Only a 0 or 90 degree angle can be applied to NumberAxis
+ double angle = getValuesAngle();
+ if ( angle == 90 || angle == 0 )
+ axis.setVerticalTickLabels( angle == 90 );
+ else
+ // Give a warn if another angle should be applied to NumberAxis
+ LOGGER.warn("Only 0 or 90 degrees (horizontal/vertical) can be applied to values of "+LangUtil.getSimpleClassName(axis)+": "+angle);
+ }
+
+ /**
+ * Applies style properties which can only be applied to
+ * {@link DateAxis} (values angle and format).<br>
+ * Called by {@link #applyToAxis(Axis)}.
+ * @param axis a {@link DateAxis}
+ */
+ protected void applyToDateAxis(DateAxis axis) {
+ // Only DateFormat can be applied to NumberAxis
+ if ( getValuesFormat() instanceof DateFormat )
+ axis.setDateFormatOverride( (DateFormat)getValuesFormat() );
+ else
+ // Give a warn if another format should be applied
+ if ( getValuesFormat() != null )
+ LOGGER.warn("Only a DateFormat can not be applied to values of "+LangUtil.getSimpleClassName(axis)+": "+LangUtil.getSimpleClassName(getValuesFormat()));
+
+ // Only a 0 or 90 degree angle can be applied to DateAxis
+ double angle = getValuesAngle();
+ if ( angle == 90 || angle == 0 )
+ axis.setVerticalTickLabels( angle == 90 );
+ else
+ // Give a warn if another angle should be applied to DateAxis
+ LOGGER.warn("Only 0 or 90 degrees (horizontal/vertical) can be applied to values of "+LangUtil.getSimpleClassName(axis)+": "+angle);
+ }
}
Modified: trunk/src/schmitzm/jfree/chart/style/ChartLabelStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartLabelStyle.java 2009-06-30 08:03:29 UTC (rev 169)
+++ trunk/src/schmitzm/jfree/chart/style/ChartLabelStyle.java 2009-06-30 19:41:53 UTC (rev 170)
@@ -14,9 +14,15 @@
import java.awt.Color;
import java.awt.Paint;
+import org.apache.log4j.Category;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.Axis;
+import org.jfree.chart.axis.CategoryAxis;
+import org.jfree.chart.axis.DateAxis;
+import org.jfree.chart.axis.NumberAxis;
import org.jfree.data.general.Dataset;
+
+import schmitzm.lang.LangUtil;
/**
* This interface is a general super class for a design style of a chart
@@ -25,29 +31,29 @@
* @version 1.0
*/
public class ChartLabelStyle {
+ /** Logger for this class */
+ protected final Category LOGGER = LangUtil.createLogger(this);
+
/** Holds the label text. */
protected String label = null;
/** Holds the text color of the label. */
protected Paint paint = null;
- /** Holds the angle (in degrees) the label is rotated by. */
- protected double angleDegr = 0;
/**
- * Creates a new style with default values (empty label, color black, angle 0).
+ * Creates a new style with default values (empty label, color black).
*/
public ChartLabelStyle() {
- this("",Color.BLACK, 0.0);
+ this("",Color.BLACK);
}
/**
* Creates a new style.
+ * @param label label text
* @param color text color
- * @param angle angle (in degrees) the text is rotated by
*/
- public ChartLabelStyle(String label, Paint color, double angle) {
+ public ChartLabelStyle(String label, Paint color) {
setLabel(label);
setPaint(color);
- setAngle(angle);
}
/**
@@ -80,26 +86,4 @@
this.paint = color;
}
- /**
- * Returns the angle (in degrees) the label text is rotated by.
- */
- public double getAngle() {
- return angleDegr;
- }
-
- /**
- * Returns the angle (in radian) the label text is rotated by.
- */
- public double getAngleRadian() {
- return angleDegr * Math.PI / 180;
- }
-
- /**
- * Sets the angle (in degrees) the label text is rotated by.
- * @param color Color for the label text
- */
- public void setAngle(double angle) {
- this.angleDegr = angle;
- }
-
}
Added: trunk/src/schmitzm/jfree/chart/style/ChartPlotStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartPlotStyle.java 2009-06-30 08:03:29 UTC (rev 169)
+++ trunk/src/schmitzm/jfree/chart/style/ChartPlotStyle.java 2009-06-30 19:41:53 UTC (rev 170)
@@ -0,0 +1,286 @@
+/** SCHMITZM - This file is part of the java library of Martin O.J. Schmitz (SCHMITZM)
+
+ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
+ This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
+
+ Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
+ Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License.
+ Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
+ **/
+
+package schmitzm.jfree.chart.style;
+
+import java.awt.Color;
+import java.awt.Insets;
+import java.awt.Paint;
+import java.text.DateFormat;
+import java.text.Format;
+import java.text.NumberFormat;
+import java.util.HashMap;
+import java.util.Map;
+
+import net.sf.jsqlparser.statement.insert.Insert;
+
+import org.apache.log4j.Category;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.axis.Axis;
+import org.jfree.chart.axis.CategoryAxis;
+import org.jfree.chart.axis.CategoryLabelPositions;
+import org.jfree.chart.axis.DateAxis;
+import org.jfree.chart.axis.NumberAxis;
+import org.jfree.chart.event.PlotChangeEvent;
+import org.jfree.chart.plot.CategoryPlot;
+import org.jfree.chart.plot.Plot;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.renderer.AbstractRenderer;
+import org.jfree.chart.renderer.xy.XYBarRenderer;
+import org.jfree.data.general.Dataset;
+import org.jfree.ui.RectangleInsets;
+
+import schmitzm.jfree.JFreeChartUtil;
+import schmitzm.lang.LangUtil;
+
+/**
+ * This class defines the rendering style the charts plot(s). The following
+ * properties are available:
+ * <ul>
+ * <li>foreground transparency</li>
+ * <li>background color and transparency</li>
+ * <li>insets margins</li>
+ * <li>domain grid line visibility and color (only applicable for {@link XYPlot} and
+ * {@link CategoryPlot})</li>
+ * <li>range grid line visibility and color (only applicable for {@link XYPlot} and
+ * {@link CategoryPlot})</li>
+ * </ul>
+ * @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
+ * @version 1.0
+ */
+public class ChartPlotStyle {
+ /** Logger for this class */
+ protected final Category LOGGER = LangUtil.createLogger(this);
+
+ /** Holds the inner margins of the plot. */
+ protected RectangleInsets insets = null;;
+ /** Holds the foreground transparency. */
+ protected Float foregroundAlpha = 1.0f;
+ /** Holds the background color of the plot. */
+ protected Paint backgroundPaint = null;
+ /** Holds the background transparency. */
+ protected Float backgroundAlpha = 1.0f;
+ /** Holds whether the domain grid line is visible. */
+ protected Boolean domainGridlineVisible = null;
+ /** Holds the color of the domain grid line. */
+ protected Paint domainGridlinePaint = null;
+ /** Holds whether the range grid line is visible. */
+ protected Boolean rangeGridlineVisible = null;
+ /** Holds the color of the range grid line. */
+ protected Paint rangeGridlinePaint = null;
+
+
+ /**
+ * Creates a new style with default values.
+ */
+ public ChartPlotStyle() {
+ }
+
+ /**
+ * Returns the inner margin of the plot.
+ */
+ public RectangleInsets getInsets() {
+ return insets;
+ }
+
+ /**
+ * Sets the inner margin of the plot.
+ */
+ public void setInsets(RectangleInsets insets) {
+ this.insets = insets;
+ }
+
+ /**
+ * Returns the transparency of the foreground.
+ * @return {@code null} if no specific transparency is set for the plot
+ */
+ public Float getForegroundAlpha() {
+ return foregroundAlpha;
+ }
+
+ /**
+ * Sets the transparency of the foreground.
+ * @param alpha transparency value (0..1); can be {@code null} to reset
+ * to a non-specific value
+ */
+ public void setForegroundAlpha(Float alpha) {
+ this.foregroundAlpha = alpha;
+ }
+
+ /**
+ * Returns the transparency of the background.
+ * @return {@code null} if no specific transparency is set for the plot
+ */
+ public Float getBackgroundAlpha() {
+ return backgroundAlpha;
+ }
+
+ /**
+ * Sets the transparency of the background.
+ * @param alpha transparency value (0..1); can be {@code null} to reset
+ * to a non-specific value
+ */
+ public void setBackgroundAlpha(Float alpha) {
+ this.backgroundAlpha = alpha;
+ }
+
+ /**
+ * Returns the background color of the plot.
+ * @return {@code null} if no specific color is set
+ */
+ public Paint getBackgroundPaint() {
+ return backgroundPaint;
+ }
+
+ /**
+ * Sets the background color of the plot.
+ * @param color background color for the plot (can be {@code null} to reset
+ * to a non-specific color)
+ */
+ public void setBackgroundPaint(Paint color) {
+ this.backgroundPaint = color;
+ }
+
+
+ /**
+ * Returns whether the plot's domain grid line is visible.
+ * @return {@code null} if property is not set for the plot
+ */
+ public Boolean isDomainGridlineVisible() {
+ return domainGridlineVisible;
+ }
+
+ /**
+ * Sets whether the plot's domain grid line is visible.
+ * @param visible indicates the visibility of the grid line (can be
+ * {@code null} to reset to a non-specific property)
+ */
+ public void setDomainGridlineVisible(Boolean visible) {
+ domainGridlineVisible = visible;
+ }
+
+ /**
+ * Returns the color of the plot's domain grid line.
+ * @return {@code null} if no specific color is set
+ */
+ public Paint getDomainGridlinePaint() {
+ return domainGridlinePaint;
+ }
+
+ /**
+ * Sets the color of the plot's domain grid line.
+ * @param color color for the domain grid line (can be {@code null} to reset
+ * to a non-specific color)
+ */
+ public void setDomainGridlinePaint(Paint color) {
+ this.domainGridlinePaint = color;
+ }
+
+ /**
+ * Returns whether the plot's range grid line is visible.
+ * @return {@code null} if property is not set for the plot
+ */
+ public Boolean isRangeGridlineVisible() {
+ return rangeGridlineVisible;
+ }
+
+ /**
+ * Sets whether the plot's range grid line is visible.
+ * @param visible indicates the visibility of the grid line (can be
+ * {@code null} to reset to a non-specific property)
+ */
+ public void setRangeGridlineVisible(Boolean visible) {
+ rangeGridlineVisible = visible;
+ }
+
+ /**
+ * Returns the color of the plot's range grid line.
+ * @return {@code null} if no specific color is set
+ */
+ public Paint getRangeGridlinePaint() {
+ return rangeGridlinePaint;
+ }
+
+ /**
+ * Sets the color of the plot's range grid line.
+ * @param color color for the range grid line (can be {@code null} to reset
+ * to a non-specific color)
+ */
+ public void setRangeGridlinePaint(Paint color) {
+ this.rangeGridlinePaint = color;
+ }
+
+ /**
+ * Applies the style to a specific plot.<br>
+ * Note: Some style properties can only be applied to specific plot types.
+ * @param plot a plot
+ */
+ public void applyToPlot(Plot plot) {
+ if (plot == null) {
+ LOGGER.debug("No plot to be customised.");
+ return;
+ }
+
+ // Apply common attributes
+ if ( getForegroundAlpha() != null )
+ plot.setForegroundAlpha( getForegroundAlpha() );
+ if ( getBackgroundAlpha() != null )
+ plot.setBackgroundAlpha( getBackgroundAlpha() );
+ if ( getBackgroundPaint() != null )
+ plot.setBackgroundPaint( getBackgroundPaint() );
+ if ( getInsets() != null )
+ plot.setInsets(insets);
+
+ // Apply margin according to the specific renderer type
+ if (plot instanceof CategoryPlot)
+ applyToCategoryPlot((CategoryPlot)plot);
+ if (plot instanceof XYPlot)
+ applyToCategoryPlot((XYPlot)plot);
+
+ // notify the listeners about style change
+ JFreeChartUtil.fireChangeEvent(plot);
+ }
+
+
+ /**
+ * Applies style properties which can only be applied to
+ * {@link CategoryPlot}.<br>
+ * Called by {@link #applyToPlot(Plot)}.
+ * @param plot a {@link CategoryPlot}
+ */
+ protected void applyToCategoryPlot(CategoryPlot plot) {
+ if ( isDomainGridlineVisible() != null )
+ plot.setDomainGridlinesVisible( isDomainGridlineVisible() );
+ if ( getDomainGridlinePaint() != null )
+ plot.setDomainGridlinePaint( getDomainGridlinePaint() );
+ if ( isRangeGridlineVisible() != null )
+ plot.setRangeGridlinesVisible( isRangeGridlineVisible() );
+ if ( getRangeGridlinePaint() != null )
+ plot.setRangeGridlinePaint( getRangeGridlinePaint() );
+ }
+
+ /**
+ * Applies style properties which can only be applied to
+ * {@link XYPlot}.<br>
+ * Called by {@link #applyToPlot(Plot)}.
+ * @param plot a {@link XYPlot}
+ */
+ protected void applyToCategoryPlot(XYPlot plot) {
+ if ( isDomainGridlineVisible() != null )
+ plot.setDomainGridlinesVisible( isDomainGridlineVisible() );
+ if ( getDomainGridlinePaint() != null )
+ plot.setDomainGridlinePaint( getDomainGridlinePaint() );
+ if ( isRangeGridlineVisible() != null )
+ plot.setRangeGridlinesVisible( isRangeGridlineVisible() );
+ if ( getRangeGridlinePaint() != null )
+ plot.setRangeGridlinePaint( getRangeGridlinePaint() );
+ }
+}
Added: trunk/src/schmitzm/jfree/chart/style/ChartRendererStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartRendererStyle.java 2009-06-30 08:03:29 UTC (rev 169)
+++ trunk/src/schmitzm/jfree/chart/style/ChartRendererStyle.java 2009-06-30 19:41:53 UTC (rev 170)
@@ -0,0 +1,201 @@
+/** SCHMITZM - This file is part of the java library of Martin O.J. Schmitz (SCHMITZM)
+
+ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
+ This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
+
+ Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
+ Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License.
+ Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
+ **/
+
+package schmitzm.jfree.chart.style;
+
+import java.awt.Color;
+import java.awt.Paint;
+import java.text.DateFormat;
+import java.text.Format;
+import java.text.NumberFormat;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.log4j.Category;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.axis.Axis;
+import org.jfree.chart.axis.CategoryAxis;
+import org.jfree.chart.axis.CategoryLabelPositions;
+import org.jfree.chart.axis.DateAxis;
+import org.jfree.chart.axis.NumberAxis;
+import org.jfree.chart.event.RendererChangeEvent;
+import org.jfree.chart.plot.CategoryPlot;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.renderer.AbstractRenderer;
+import org.jfree.chart.renderer.xy.XYBarRenderer;
+import org.jfree.data.general.Dataset;
+
+import schmitzm.jfree.JFreeChartUtil;
+import schmitzm.lang.LangUtil;
+
+/**
+ * This class defines the rendering style the charts series. The following
+ * properties are available:
+ * <ul>
+ * <li>color of series graphs</li>
+ * <li>visibility of series item labels</li>
+ * <li>visibility of series shapes</li>
+ * <li>percentage margin between bars (only applicable for {@link XYBarRenderer})</li>
+ * </ul>
+
+ * @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
+ * @version 1.0
+ */
+public class ChartRendererStyle {
+ /** Logger for this class */
+ protected final Category LOGGER = LangUtil.createLogger(this);
+
+ /** 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. */
+ protected Map<Integer, Paint> seriesPaint = new HashMap<Integer, Paint>();
+ /** Holds whether the shape of a series is visible. */
+ protected Map<Integer, Boolean> seriesShapesVisible = new HashMap<Integer, Boolean>();
+ /** Holds the (percentage) margin for the bars. Only applicable for {@link XYBarRenderer}. */
+ protected double margin = 0.0;
+
+ /**
+ * Creates a new style with default values.
+ */
+ public ChartRendererStyle() {
+ }
+
+ /**
+ * Returns the margin (percentage) between the chart bars. The margin is
+ * only applicable on bar charts.
+ */
+ public Double getMargin() {
+ return margin;
+ }
+
+ /**
+ * Sets the margin (percentage) between the chart bars. The margin is
+ * only applicable on bar charts.
+ */
+ public void setMargin(Double margin) {
+ this.margin = margin;
+ }
+
+ /**
+ * Returns the color, a series is painted with.
+ * @param series series index
+ * @return {@code null} if no specific color is set for the series
+ */
+ public Paint getSeriesPaint(int series) {
+ return seriesPaint.get(series);
+ }
+
+ /**
+ * Sets the color, a series is painted with.
+ * @param series series index
+ * @param color color for the series (can be {@code null} to reset
+ * to a non-specific color)
+ */
+ public void setSeriesPaint(int series, Paint color) {
+ seriesPaint.put(series,color);
+ }
+
+ /**
+ * Returns whether the item labels of a series are visible.
+ * @param series series index
+ * @return {@code null} if property is not set for the series
+ */
+ public Boolean isSeriesItemLabelsVisible(int series) {
+ return seriesItemLabelsVisible.get(series);
+ }
+
+ /**
+ * Sets whether the item labels of a series are visible.
+ * @param series series index
+ * @param visible indicates the visibility of the series (can be
+ * {@code null} to reset to a non-specific property)
+ */
+ public void setSeriesItemLabelsVisible(int series, Boolean visible) {
+ seriesItemLabelsVisible.put(series,visible);
+ }
+
+ /**
+ * Returns whether the shapes of a series are visible.
+ * @param series series index
+ * @return {@code null} if property is not set for the series
+ */
+ public Boolean isSeriesShapesVisible(int series) {
+ return seriesShapesVisible.get(series);
+ }
+
+ /**
+ * Sets whether the shapes of a series are visible.
+ * @param series series index
+ * @param visible indicates the visibility of the series (can be
+ * {@code null} to reset to a non-specific property)
+ */
+ public void setSeriesShapesVisible(int series, Boolean visible) {
+ seriesShapesVisible.put(series,visible);
+ }
+
+ /**
+ * Applies the style to a specific renderer.<br>
+ * Note: Some style properties can only be applied to specific renderer types.
+ * @param renderer a renderer
+ */
+ public void applyToRenderer(AbstractRenderer renderer) {
+ if (renderer == null) {
+ LOGGER.debug("No renderer to be customised.");
+ return;
+ }
+
+ // Apply item label visibility
+ for (int i=0; i<seriesItemLabelsVisible.size(); i++)
+ try {
+ if ( isSeriesItemLabelsVisible(i) != null )
+ renderer.setSeriesItemLabelsVisible(i, isSeriesItemLabelsVisible(i), false);
+ } catch (Exception err) {
+ LOGGER.warn("Renderer contains not series "+i+". Label visibility can not be applied.");
+ }
+
+ // Apply shape visibility
+ for (int i=0; i<seriesShapesVisible.size(); i++)
+ try {
+ if ( isSeriesShapesVisible(i) != null )
+ renderer.setSeriesShape(i, isSeriesShapesVisible(i) ? AbstractRenderer.DEFAULT_SHAPE : null, false);
+ } catch (Exception err) {
+ LOGGER.warn("Renderer contains not series "+i+". Shapes visibility can not be applied.");
+ }
+
+ // Apply rendering color
+ for (int i=0; i<seriesPaint.size(); i++)
+ try {
+ if ( getSeriesPaint(i) != null )
+ renderer.setSeriesPaint(i, getSeriesPaint(i), false);
+ } catch (Exception err) {
+ LOGGER.warn("Renderer contains not series "+i+". Paint can not be applied.");
+ }
+
+ // Apply margin according to the specific renderer type
+ if (renderer instanceof XYBarRenderer)
+ applyToBarRenderer((XYBarRenderer)renderer);
+
+ // notify the listeners about style change
+ JFreeChartUtil.fireChangeEvent(renderer);
+ }
+
+
+ /**
+ * Applies style properties which can only be applied to
+ * {@link XYBarRenderer}.<br>
+ * Called by {@link #applyToRenderer(AbstractRenderer)}.
+ * @param renderer a {@link XYBarRenderer}
+ */
+ protected void applyToBarRenderer(XYBarRenderer renderer) {
+ if ( getMargin() != null )
+ renderer.setMargin(margin);
+ }
+}
Modified: trunk/src/schmitzm/jfree/chart/style/ChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyle.java 2009-06-30 08:03:29 UTC (rev 169)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyle.java 2009-06-30 19:41:53 UTC (rev 170)
@@ -11,6 +11,8 @@
package schmitzm.jfree.chart.style;
+import java.awt.Paint;
+
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
@@ -86,6 +88,27 @@
*/
public void setTitleStyle(ChartLabelStyle titleStyle);
+ /**
+ * Returns the background color of the chart.
+ */
+ public Paint getBackground();
+
+ /**
+ * Sets the background color of the chart.
+ * @param paint the background color for the chart
+ */
+ public void setBackground(Paint paint);
+
+ /**
+ * Returns whether a border is shown around the whole chart.
+ */
+ public boolean isBorderVisible();
+
+ /**
+ * Sets whether a border is shown around the whole chart.
+ */
+ public void setBorderVisible(boolean visible);
+
/**
* Returns whether the chart is configured to generate a legend.
*/
@@ -136,6 +159,41 @@
*/
public void setAxisStyle(int axis, ChartAxisStyle style);
+
+ /**
+ * Returns the style of a dataset renderer.
+ * @param dataset dataset number (starting with 0)
+ * @return {@code null} if the chart has no such renderer
+ */
+ public ChartRendererStyle getRendererStyle(int dataset);
+
+ /**
+ * Sets the style of a dataset renderer.
+ * @param dataset dataset number (starting with 0)
+ * @param style style for the axis
+ */
+ public void setRendererStyle(int dataset, ChartRendererStyle style);
+
+ /**
+ * Returns the style of the chart's plot.
+ * @return {@code null} if no such style is defined
+ */
+ public ChartPlotStyle getPlotStyle();
+
+ /**
+ * Sets the style of the chart's plot.
+ * @param style style for the plot
+ */
+ public void setPlotStyle(ChartPlotStyle style);
+
+ /**
+ * Applies the style to an existing chart.
+ * @param chart chart to be styled
+ * @exception UnsupportedOperationException if the style can not be applied
+ * to the given chart
+ */
+ public void applyToChart(JFreeChart chart);
+
/**
* Generates a chart using the given data and the style represented by
* this class.
Modified: trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java 2009-06-30 08:03:29 UTC (rev 169)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java 2009-06-30 19:41:53 UTC (rev 170)
@@ -11,16 +11,20 @@
package schmitzm.jfree.chart.style;
+import java.awt.Paint;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
+import java.util.List;
import org.jdom.Element;
import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.ui.RectangleInsets;
import schmitzm.jfree.chart.style.ChartStyle.ChartType;
import schmitzm.swing.SwingUtil;
+import schmitzm.xml.XMLUtil;
/**
* This class contains static utility methods related to chart styling.
@@ -29,26 +33,12 @@
*/
public class ChartStyleUtil {
- /**
- * Gets the attribute value from element.
- * @param element element where the attribute is determined from
- * @param attrName name of the attribute
- * @param defValue default value returned if attribute is not found (or empty)
- */
- private static String getAttributeValue(Element element, String attrName, String... defValue) {
- String attrValue = element.getAttributeValue(attrName);
- if ( defValue.length >= 1 && (attrValue == null || "".equals(attrValue)) )
- return defValue[0];
- return attrValue;
- }
-
-
public static ChartStyle createStyleFromXML(Element element) {
if (!"style".equals(element.getName()))
throw new IllegalArgumentException("Element is no style element: "+element.getName());
// Determine the chart type
- String typeStr = getAttributeValue(element, "type");
+ String typeStr = XMLUtil.getAttribute(element, "type");
ChartStyle.ChartType chartType = null;
if ("area".equalsIgnoreCase(typeStr))
chartType = ChartType.AREA;
@@ -74,27 +64,52 @@
// create a style according to the type
ChartStyle chartStyle = createDefaultChartStyle(chartType);
- // common attributes
- chartStyle.setLegend( "true".equalsIgnoreCase( getAttributeValue(element,"legend","true") ) );
- chartStyle.setTooltips( "true".equalsIgnoreCase( getAttributeValue(element,"tooltips","true") ) );
- chartStyle.setURLs( "true".equalsIgnoreCase( getAttributeValue(element,"urls","true") ) );
+ // Legend visible
+ chartStyle.setLegend( XMLUtil.getBooleanAttribute(element,"legend",true) );
+ chartStyle.setTooltips( XMLUtil.getBooleanAttribute(element,"tooltips",true) );
+ chartStyle.setURLs( XMLUtil.getBooleanAttribute(element,"urls",true) );
+
+ // Background color
+ Paint color = XMLUtil.getColorAttribute(element.getChild("background"),"paint");
+ if ( color != null )
+ chartStyle.setBackground( color );
+
+ // Border around chart visible
+ Boolean borderVisible = XMLUtil.getBooleanAttribute(element.getChild("border"),"visible");
+ if ( borderVisible != null )
+ chartStyle.setBorderVisible( borderVisible );
// Plot orientation
- String orientationStr = getAttributeValue(element,"orientation","horzontal");
+ String orientationStr = XMLUtil.getAttribute(element,"orientation","horizontal");
if ("horizontal".equalsIgnoreCase(orientationStr))
chartStyle.setOrientation( PlotOrientation.HORIZONTAL );
else if ("vertical".equalsIgnoreCase(orientationStr))
chartStyle.setOrientation( PlotOrientation.VERTICAL );
+ else
+ throw new UnsupportedOperationException("Unknown orientation definition: "+orientationStr);
+ // Title style
if ( element.getChild("title") != null );
chartStyle.setTitleStyle( createTitleStyleFromXML(element.getChild("title") ) );
+ // Domain axis
if ( element.getChild("domainAxis") != null );
chartStyle.setAxisStyle( ChartStyle.DOMAIN_AXIS,createAxisStyleFromXML(element.getChild("domainAxis") ) );
+ // Range axis
if ( element.getChild("rangeAxis") != null );
chartStyle.setAxisStyle( ChartStyle.RANGE_AXIS,createAxisStyleFromXML(element.getChild("rangeAxis") ) );
+ // Second range axis
if ( element.getChild("rangeAxis2") != null );
chartStyle.setAxisStyle( ChartStyle.RANGE_AXIS2,createAxisStyleFromXML(element.getChild("rangeAxis2") ) );
+ // Rendering of series
+ List<Element> rendererElements = element.getChildren("renderer");
+ for (int i=0; i<rendererElements.size(); i++)
+ chartStyle.setRendererStyle(i,createRendererStyleFromXML(rendererElements.get(i)));
+
+ // Plot
+ if ( element.getChild("plot") != null );
+ chartStyle.setPlotStyle( createPlotStyleFromXML(element.getChild("plot") ) );
+
return chartStyle;
}
@@ -104,9 +119,9 @@
*/
public static ChartStyle createDefaultChartStyle(ChartType type) {
switch ( type ) {
- case AREA:
- case LINE:
- case POINT: break;
+ case AREA:
+ case LINE:
+ case POINT: return new BasicChartStyle(type);
}
throw new UnsupportedOperationException("Style for this chart type not yet supported: "+type);
}
@@ -123,20 +138,14 @@
*/
private static void applyLabelStyleFromXML(Element element, ChartLabelStyle style) {
// color in attribute
- String colorStr = getAttributeValue(element, "paint");
- if ( colorStr != null )
- style.setPaint( SwingUtil.parseColor(colorStr) );
+ Paint color = XMLUtil.getColorAttribute(element, "paint");
+ if ( color != null )
+ style.setPaint( color );
- // angle in attribute
- String angleStr = getAttributeValue(element, "angle");
- if ( angleStr != null )
- style.setAngle( Double.parseDouble(angleStr) );
-
// label in child element <label>
Element labelElem = element.getChild("label");
if ( labelElem != null )
style.setLabel( labelElem.getTextTrim() );
-
}
/**
@@ -163,25 +172,126 @@
applyLabelStyleFromXML(element,style);
// read "visible" property from attribute
- String visibleStr = getAttributeValue(element, "visible");
- if ( visibleStr != null )
- style.setVisible( "true".equalsIgnoreCase(visibleStr) );
+ Boolean visible = XMLUtil.getBooleanAttribute(element, "visible");
+ if ( visible != null )
+ style.setVisible( visible );
+ // read label angle in attribute
+ Double labelAngle = XMLUtil.getDoubleAttribute(element, "angle");
+ if ( labelAngle != null )
+ style.setLabelAngle( labelAngle );
+
// values format and angle from child element <valueLabels>
Element valuesElem = element.getChild("valueLabels");
if ( valuesElem != null ) {
- String angleStr = getAttributeValue(valuesElem, "angle");
- if ( angleStr != null )
- style.setValuesAngle( Double.parseDouble(angleStr) );
- String formatStr = getAttributeValue(valuesElem, "numberFormat");
+ Double valuesAngle = XMLUtil.getDoubleAttribute(valuesElem, "angle");
+ if ( valuesAngle != null )
+ style.setValuesAngle( valuesAngle );
+ String formatStr = XMLUtil.getAttribute(valuesElem, "numberFormat");
if ( formatStr != null )
- style.setValuesFormat(new DecimalFormat(formatStr) );
- formatStr = getAttributeValue(valuesElem, "dateFormat");
- if ( formatStr != null )
- style.setValuesFormat(new SimpleDateFormat(formatStr) );
+ style.setValuesFormat(new DecimalFormat(formatStr) );
+ else {
+ formatStr = XMLUtil.getAttribute(valuesElem, "dateFormat");
+ if ( formatStr != null )
+ style.setValuesFormat(new SimpleDateFormat(formatStr) );
+ }
}
return style;
}
+ /**
+ * Creates a {@link ChartRendererStyle} from XML element.
+ * @param element an element
+ */
+ public static ChartRendererStyle createRendererStyleFromXML(Element element) {
+ ChartRendererStyle style = new ChartRendererStyle();
+
+ // read "margin" property from attribute
+ Double margin = XMLUtil.getDoubleAttribute(element, "margin");
+ if ( margin != null )
+ style.setMargin( margin );
+
+ // read series properties
+ List<Element> seriesElements = element.getChildren("series");
+ for (int i=0; i<seriesElements.size(); i++) {
+ Element seriesElement = seriesElements.get(i);
+ // series paint
+ Paint seriesPaint = XMLUtil.getColorAttribute(seriesElement, "paint");
+ if ( seriesPaint != null )
+ style.setSeriesPaint(i, seriesPaint);
+
+ // item label visibility
+ Boolean labelsVisible = XMLUtil.getBooleanAttribute(seriesElement, "itemLabelsVisible");
+ if ( labelsVisible != null )
+ style.setSeriesItemLabelsVisible(i, labelsVisible);
+
+ // item shapes visibility
+ Boolean shapesVisible = XMLUtil.getBooleanAttribute(seriesElement, "shapesVisible");
+ if ( shapesVisible != null )
+ style.setSeriesShapesVisible(i, shapesVisible);
+ }
+ return style;
+ }
+
+ /**
+ * Creates a {@link ChartPlotStyle} from XML element.
+ * @param element an element
+ */
+ public static ChartPlotStyle createPlotStyleFromXML(Element element) {
+ ChartPlotStyle style = new ChartPlotStyle();
+
+ // foreground transparency
+ Element foregroundElem = element.getChild("foreground");
+ if ( foregroundElem != null ) {
+ Float alpha = XMLUtil.getFloatAttribute(foregroundElem, "alpha");
+ if ( alpha != null )
+ style.setForegroundAlpha(alpha);
+ }
+
+ // background color and transparency
+ Element backegroundElem = element.getChild("background");
+ if ( backegroundElem != null ) {
+ Float alpha = XMLUtil.getFloatAttribute(backegroundElem, "alpha");
+ if ( alpha != null )
+ style.setBackgroundAlpha(alpha);
+ Paint color = XMLUtil.getColorAttribute(backegroundElem, "paint");
+ if ( color != null )
+ style.setBackgroundPaint(color);
+ }
+
+ // Insets
+ Element insetsElem = element.getChild("insets");
+ if ( insetsElem != null ) {
+ double top = XMLUtil.getDoubleAttribute(insetsElem, "top", 0.0);
+ double bottom = XMLUtil.getDoubleAttribute(insetsElem, "bottom", 0.0);
+ double left = XMLUtil.getDoubleAttribute(insetsElem, "left", 0.0);
+ double right = XMLUtil.getDoubleAttribute(insetsElem, "right", 0.0);
+ style.setInsets( new RectangleInsets(top,left,bottom,right) );
+ }
+
+ // domain grid line color and visibility
+ Element domainGridElem = element.getChild("domainGridline");
+ if ( domainGridElem != null ) {
+ Boolean visible = XMLUtil.getBooleanAttribute(domainGridElem, "visible");
+ if ( visible != null )
+ style.setDomainGridlineVisible(visible);
+ Paint color = XMLUtil.getColorAttribute(domainGridElem, "paint");
+ if ( color != null )
+ style.setDomainGridlinePaint(color);
+ }
+
+ // domain grid line color and visibility
+ Element rangeGridElem = element.getChild("domainGridline");
+ if ( rangeGridElem != null ) {
+ Boolean visible = XMLUtil.getBooleanAttribute(rangeGridElem, "visible");
+ if ( visible != null )
+ style.setDomainGridlineVisible(visible);
+ Paint color = XMLUtil.getColorAttribute(rangeGridElem, "paint");
+ if ( color != null )
+ style.setDomainGridlinePaint(color);
+ }
+
+ return style;
+ }
}
Modified: trunk/src/schmitzm/xml/XMLUtil.java
===================================================================
--- trunk/src/schmitzm/xml/XMLUtil.java 2009-06-30 08:03:29 UTC (rev 169)
+++ trunk/src/schmitzm/xml/XMLUtil.java 2009-06-30 19:41:53 UTC (rev 170)
@@ -1,9 +1,14 @@
package schmitzm.xml;
+import java.awt.Color;
+
import org.apache.log4j.Logger;
+import org.jdom.Element;
import org.jdom.adapters.JAXPDOMAdapter;
+import schmitzm.swing.SwingUtil;
+
/**
* Diese Klasse enthaelt statische Helper-Methoden fuer das Arbeiten mit XML.
* @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
@@ -16,7 +21,108 @@
/** Wrapper from JDOM to W3C. */
public static final JAXPDOMAdapter JDOM_TO_JAX = new JAXPDOMAdapter();
-// /**
+ /**
+ * Gets the attribute value from element.
+ * @param element element where the attribute is determined from
+ * @param attrName name of the attribute
+ * @param defValue default value returned if attribute is not found (or empty)
+ * @return {@code null} if {@code element} is {@code null} or attribute is not
+ * specified in element
+ */
+ public static String getAttribute(Element element, String attrName, String... defValue) {
+ if ( element == null )
+ return null;
+ String attrValue = element.getAttributeValue(attrName);
+ if ( (attrValue == null || "".equals(attrValue)) && defValue.length >= 1 )
+ return defValue[0];
+ return attrValue;
+ }
+
+ /**
+ * Gets the attribute value from element as {@link Boolean}.
+ * @param element element where the attribute is determined from
+ * @param attrName name of the attribute
+ * @param defValue optional default value returned if attribute is not found (or empty)
+ * @return {@code null} if {@code element} is {@code null} or attribute is not
+ * specified in element
+ */
+ public static Boolean getBooleanAttribute(Element element, String attrName, Boolean... defValue) {
+ String value = getAttribute(element, attrName);
+ if ( value != null )
+ return Boolean.parseBoolean(value);
+ if ( defValue.length > 0 )
+ return defValue[0];
+ return null;
+ }
+
+ /**
+ * Gets the attribute value from element as {@link Integer}.
+ * @param element element where the attribute is determined from
+ * @param attrName name of the attribute
+ * @param defValue optional default value returned if attribute is not found (or empty)
+ * @return {@code null} if {@code element} is {@code null} or attribute is not
+ * specified in element
+ */
+ public static Integer getIntegerAttribute(Element element, String attrName, Integer... defValue) {
+ String value = getAttribute(element, attrName);
+ if ( value != null )
+ return Integer.parseInt(value);
+ if ( defValue.length > 0 )
+ return defValue[0];
+ return null;
+ }
+
+ /**
+ * Gets the attribute value from element as {@link Double}.
+ * @param element element where the attribute is determined from
+ * @param attrName name of the attribute
+ * @param defValue optional default value returned if attribute is not found (or empty)
+ * @return {@code null} if {@code element} is {@code null} or attribute is not
+ * specified in element
+ */
+ public static Double getDoubleAttribute(Element element, String attrName, Double... defValue) {
+ String value = getAttribute(element, attrName);
+ if ( value != null )
+ return Double.parseDouble(value);
+ if ( defValue.length > 0 )
+ return defValue[0];
+ return null;
+ }
+
+ /**
+ * Gets the attribute value from element as {@link Float}.
+ * @param element element where the attribute is determined from
+ * @param attrName name of the attribute
+ * @param defValue optional default value returned if attribute is not found (or empty)
+ * @return {@code null} if {@code element} is {@code null} or attribute is not
+ * specified in element
+ */
+ public static Float getFloatAttribute(Element element, String attrName, Float... defValue) {
+ String value = getAttribute(element, attrName);
+ if ( value != null )
+ return Float.parseFloat(value);
+ if ( defValue.length > 0 )
+ return defValue[0];
+ return null;
+ }
+
+ /**
+ * Gets the attribute value from element as {@link Color}.
+ * @param element element where the attribute is determined from
+ * @param attrName name of the attribute
+ * @param defValue optional default value returned if attribute is not found (or empty)
+ * @return {@code null} if {@code element} is {@code null} or attribute is not
+ * specified in element
+ */
+ public static Color getColorAttribute(Element element, String attrName, Color... defValue) {
+ String value = getAttribute(element, attrName);
+ if ( value != null )
+ return SwingUtil.parseColor(value);
+ if ( defValue.length > 0 )
+ return defValue[0];
+ return null;
+ }
+ // /**
// * Returns a {@link DocumentBuilder}. If validating is true,
// * the contents is validated against the XSD specified in the file.
// */
More information about the Schmitzm-commits
mailing list