view gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java @ 355:fce70ff3486b

Forget to check in pom.xml gnv-artifacts/trunk@428 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 15 Dec 2009 14:35:41 +0000
parents 4ac3c1c1c060
children 2413273f1c13
line wrap: on
line source
package de.intevation.gnv.chart;

import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import org.apache.log4j.Logger;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.Series;

import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.state.describedata.KeyValueDescibeData;


/**
 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
 */
public abstract class AbstractXYLineChart
extends               AbstractChart
{
    private static Logger log      = Logger.getLogger(AbstractXYLineChart.class);

    protected static Color[] COLOR = {
        Color.black, Color.red, Color.green, Color.blue, Color.yellow,
        Color.gray, Color.orange, Color.pink, Color.cyan
    };

    protected static int nextColor = 0;

    protected PlotOrientation PLOT_ORIENTATION = PlotOrientation.VERTICAL;

    protected Map datasets;

    protected abstract void initData();
    protected abstract void addValue(Result row, Series series);
    protected abstract void addSeries(Series series, String label, int idx);
    protected abstract void localizeDomainAxis(Axis axis, Locale locale);
    protected abstract String createSeriesName(
        String breakPoint1,
        String breakPoint2,
        String breakPoint3
    );


    public JFreeChart generateChart() {
        log.debug("generate XYLineChart");
        nextColor = 0;

        if (chart != null)
            return chart;

        initChart();

        chart.addSubtitle(new TextTitle(labels.getSubtitle()));

        theme.apply(chart);
        initData();

        adjustPlot((XYPlot)chart.getPlot());

        return chart;
    }


    protected void initChart() {
        chart = ChartFactory.createXYLineChart(
            labels.getTitle(),
            labels.getDomainAxisLabel(),
            null,
            null,
            PLOT_ORIENTATION,
            true,
            false,
            false
        );
    }


    protected void prepareAxis(String seriesKey, int idx) {
        log.debug("prepare axis of xychart");

        XYPlot plot      = chart.getXYPlot();
        Axis xAxis       = plot.getDomainAxis();
        NumberAxis yAxis = new NumberAxis(seriesKey);

        localizeDomainAxis(xAxis, locale);
        localizeRangeAxis(yAxis, locale);

        if (seriesKey.contains("richtung")) {
            yAxis.setTickUnit(new NumberTickUnit(30.0));
            yAxis.setUpperBound(360.0);
            yAxis.setLowerBound(0.0);
            plot.setRangeAxis(idx, yAxis);
        }
        else {
            yAxis.setFixedDimension(10.0);
            yAxis.setAutoRangeIncludesZero(false);
            plot.setRangeAxis(idx, yAxis);
            yAxis.configure();
        }

        if (idx % 2 != 0)
            plot.setRangeAxisLocation(idx, AxisLocation.BOTTOM_OR_RIGHT);
        else
            plot.setRangeAxisLocation(idx, AxisLocation.BOTTOM_OR_LEFT);
        plot.mapDatasetToRangeAxis(idx, idx);
    }


    protected void adjustRenderer(
        int     idx,
        int     seriesCount,
        boolean renderLines,
        boolean renderShapes
    ) {
        log.debug("Adjust render of series");
        XYLineAndShapeRenderer renderer = null;
        XYPlot                 plot     = chart.getXYPlot();

        try {
            renderer = (XYLineAndShapeRenderer)((XYLineAndShapeRenderer)
                (plot.getRenderer())).clone();
        }
        catch (CloneNotSupportedException cnse) {
            log.warn("Error while cloning renderer.", cnse);
            renderer = new XYLineAndShapeRenderer(renderLines, renderShapes);
            renderer.setBaseShape(new Ellipse2D.Double(-2,-2,4,4));
        }

        for (int i = 0; i < seriesCount; i++) {
            renderer.setSeriesShape(i, renderer.getSeriesShape(0));
            renderer.setSeriesPaint(i, COLOR[nextColor%COLOR.length]);
            renderer.setSeriesShapesVisible(i, renderShapes);
            renderer.setSeriesLinesVisible(i, renderLines);
            nextColor++;
        }
        plot.setRenderer(idx, renderer);
    }


    protected void adjustPlot(XYPlot plot) {
        if (plot.getRangeAxisCount() > 1)
            plot.setRangeGridlinesVisible(false);
    }


    protected void localizeRangeAxis(Axis axis, Locale locale) {
        if (locale == null)
            return;

        log.debug(
            "Set language of axis [" + axis.getLabel() + "] " +
            "to " + locale.toString()
        );

        NumberFormat format = NumberFormat.getInstance(locale);
        ((NumberAxis) axis).setNumberFormatOverride(format);
    }


    protected String findParameter(String label) {
        Iterator iter = parameters.iterator();

        while (iter.hasNext()) {
            KeyValueDescibeData data = (KeyValueDescibeData) iter.next();
            String              key  = data.getValue();

            if (label.indexOf(key) > -1)
                return key;
        }

        return label;
    }


    protected String findValueTitle(Collection values, String id) {
        log.debug("find description of dataset");

        if (values != null){
            Iterator it = values.iterator();
            while (it.hasNext()) {
                KeyValueDescibeData data = (KeyValueDescibeData) it.next();

                if (id.equals(data.getKey()))
                    return data.getValue();
            }
        }
        return "";
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org