ingo@924: package de.intevation.flys.exports; ingo@924: ingo@924: import java.awt.BasicStroke; ingo@924: import java.awt.Color; ingo@924: ingo@924: import org.apache.log4j.Logger; ingo@924: ingo@924: import org.w3c.dom.Document; ingo@924: ingo@924: import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; ingo@924: import org.jfree.data.xy.XYSeries; ingo@924: ingo@924: import de.intevation.artifacts.common.utils.XMLUtils; ingo@924: ingo@924: ingo@924: public class StyledXYSeries extends XYSeries { ingo@924: ingo@924: public static final String XPATH_LINE_COLOR = ingo@924: "/theme/field[@name='linecolor']/@default"; ingo@924: ingo@924: public static final String XPATH_LINE_SIZE = ingo@924: "/theme/field[@name='linesize']/@default"; ingo@924: raimund@1714: public static final String XPATH_LINE_TYPE = raimund@1714: "/theme/field[@name='linetype']/@default"; ingo@924: ingo@924: protected Document theme; ingo@924: ingo@924: ingo@924: private static final Logger logger = Logger.getLogger(StyledXYSeries.class); ingo@924: ingo@924: sascha@1741: public StyledXYSeries(String key, Document theme) { sascha@1741: this(key, true, theme); sascha@1741: } ingo@924: sascha@1741: public StyledXYSeries(String key, boolean sorted, Document theme) { sascha@1741: super(key, sorted); ingo@924: this.theme = theme; ingo@924: } ingo@924: ingo@924: ingo@924: public XYLineAndShapeRenderer applyTheme(XYLineAndShapeRenderer r, int idx){ ingo@924: applyLineColor(r, idx); ingo@924: applyLineSize(r, idx); raimund@1714: applyLineType(r, idx); ingo@924: ingo@924: r.setSeriesLinesVisible(idx, true); ingo@924: r.setSeriesShapesVisible(idx, false); ingo@924: ingo@924: return r; ingo@924: } ingo@924: ingo@924: ingo@924: protected void applyLineColor(XYLineAndShapeRenderer r, int idx) { ingo@924: String color = XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null); ingo@924: ingo@924: if (color == null || color.length() == 0) { ingo@924: return; ingo@924: } ingo@924: ingo@924: String[] rgb = color.split(","); ingo@924: ingo@924: try { ingo@924: Color c = new Color( ingo@924: Integer.valueOf(rgb[0].trim()), ingo@924: Integer.valueOf(rgb[1].trim()), ingo@924: Integer.valueOf(rgb[2].trim())); ingo@924: ingo@924: logger.debug("Set series paint color: " + c.toString()); ingo@924: ingo@924: r.setSeriesPaint(idx, c); ingo@924: } ingo@924: catch (NumberFormatException nfe) { ingo@924: logger.warn("Unable to set color from string: '" + color + "'"); ingo@924: } ingo@924: } ingo@924: ingo@924: ingo@924: protected void applyLineSize(XYLineAndShapeRenderer r, int idx) { ingo@924: String size = XMLUtils.xpathString(theme, XPATH_LINE_SIZE, null); ingo@924: ingo@924: if (size == null || size.length() == 0) { ingo@924: return; ingo@924: } ingo@924: ingo@924: try { ingo@924: r.setSeriesStroke( ingo@924: idx, ingo@924: new BasicStroke(Integer.valueOf(size))); ingo@924: } ingo@924: catch (NumberFormatException nfe) { ingo@924: logger.warn("Unable to set line size from string: '" + size + "'"); ingo@924: } ingo@924: } raimund@1714: raimund@1714: raimund@1714: protected void applyLineType(XYLineAndShapeRenderer r, int idx) { raimund@1714: String dash = XMLUtils.xpathString(theme, XPATH_LINE_TYPE, null); raimund@1714: String size = XMLUtils.xpathString(theme, XPATH_LINE_SIZE, null); raimund@1714: raimund@1714: if (dash == null || dash.length() == 0) { raimund@1714: return; raimund@1714: } raimund@1714: if (size == null || size.length() == 0) { raimund@1714: return; raimund@1714: } raimund@1714: raimund@1714: String[] pattern = dash.split(","); raimund@1714: if(pattern.length == 1) { raimund@1714: return; raimund@1714: } raimund@1714: raimund@1714: try { raimund@1714: float[] dashes = new float[pattern.length]; raimund@1714: for (int i = 0; i < pattern.length; i++) { raimund@1714: dashes[i] = Float.parseFloat(pattern[i]); raimund@1714: } raimund@1714: raimund@1714: r.setSeriesStroke( raimund@1714: idx, raimund@1714: new BasicStroke(Integer.valueOf(size), raimund@1714: BasicStroke.CAP_BUTT, raimund@1714: BasicStroke.JOIN_ROUND, raimund@1714: 1.0f, raimund@1714: dashes, raimund@1714: 0.0f)); raimund@1714: } raimund@1714: catch(NumberFormatException nfe) { raimund@1714: logger.warn("Unable to set dash from string: '" + dash + "'"); raimund@1714: } raimund@1714: } ingo@924: } ingo@924: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :