view flys-artifacts/src/main/java/de/intevation/flys/exports/StyledXYSeries.java @ 1747:d2a17e990c70

Improved the Themes: we now support special themes for facets which need to match a given pattern string. flys-artifacts/trunk@3047 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 20 Oct 2011 13:45:45 +0000
parents 1bc926b5b435
children 741ba9e34c7d
line wrap: on
line source
package de.intevation.flys.exports;

import java.awt.BasicStroke;
import java.awt.Color;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;

import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;

import de.intevation.artifacts.common.utils.XMLUtils;


public class StyledXYSeries extends XYSeries {

    public static final String XPATH_LINE_COLOR =
        "/theme/field[@name='linecolor']/@default";

    public static final String XPATH_LINE_SIZE =
        "/theme/field[@name='linesize']/@default";

    public static final String XPATH_LINE_TYPE =
        "/theme/field[@name='linetype']/@default";

    protected Document theme;


    private static final Logger logger = Logger.getLogger(StyledXYSeries.class);


    public StyledXYSeries(String key, Document theme) {
        this(key, true, theme);
    }

    public StyledXYSeries(String key, boolean sorted, Document theme) {
        super(key, sorted);
        this.theme = theme;
    }


    public XYLineAndShapeRenderer applyTheme(XYLineAndShapeRenderer r, int idx){
        applyLineColor(r, idx);
        applyLineSize(r, idx);
        applyLineType(r, idx);

        r.setSeriesLinesVisible(idx, true);
        r.setSeriesShapesVisible(idx, false);

        return r;
    }


    protected void applyLineColor(XYLineAndShapeRenderer r, int idx) {
        String color = XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null);

        if (color == null || color.length() == 0) {
            return;
        }

        String[] rgb = color.split(",");

        try {
            Color c = new Color(
                Integer.valueOf(rgb[0].trim()),
                Integer.valueOf(rgb[1].trim()),
                Integer.valueOf(rgb[2].trim()));

            logger.debug("Set series paint color: " + c.toString());

            r.setSeriesPaint(idx, c);
        }
        catch (NumberFormatException nfe) {
            logger.warn("Unable to set color from string: '" + color + "'");
        }
    }


    protected void applyLineSize(XYLineAndShapeRenderer r, int idx) {
        String size = XMLUtils.xpathString(theme, XPATH_LINE_SIZE, null);

        if (size == null || size.length() == 0) {
            return;
        }

        try {
            r.setSeriesStroke(
                idx,
                new BasicStroke(Integer.valueOf(size)));
        }
        catch (NumberFormatException nfe) {
            logger.warn("Unable to set line size from string: '" + size + "'");
        }
    }


    protected void applyLineType(XYLineAndShapeRenderer r, int idx) {
        String dash = XMLUtils.xpathString(theme, XPATH_LINE_TYPE, null);
        String size = XMLUtils.xpathString(theme, XPATH_LINE_SIZE, null);

        if (dash == null || dash.length() == 0) {
            return;
        }
        if (size == null || size.length() == 0) {
            return;
        }

        String[] pattern = dash.split(",");
        if(pattern.length == 1) {
            return;
        }

        try {
            float[] dashes = new float[pattern.length];
            for (int i = 0; i < pattern.length; i++) {
                dashes[i] = Float.parseFloat(pattern[i]);
            }

            r.setSeriesStroke(
                idx,
                new BasicStroke(Integer.valueOf(size),
                                BasicStroke.CAP_BUTT,
                                BasicStroke.JOIN_ROUND,
                                1.0f,
                                dashes,
                                0.0f));
        }
        catch(NumberFormatException nfe) {
            logger.warn("Unable to set dash from string: '" + dash + "'");
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org