view gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalCrossSectionChart.java @ 469:62fc63d0f71d

Added a new State in Product Verticalprofile in Timeseriespoints. Now it will be displayed the Years where measurements happened and than only the dates of the chosen Year will be fetched and displayed. gnv-artifacts/trunk@532 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 12 Jan 2010 12:42:53 +0000
parents 20a480753ff9
children 79e80c289018
line wrap: on
line source
package de.intevation.gnv.chart;

import java.util.Locale;

import java.text.NumberFormat;

import java.awt.Color;
import java.awt.Paint;

import java.util.HashSet;

import de.intevation.gnv.math.AttributedXYColumns;

import de.intevation.gnv.jfreechart.PolygonDataset;
import de.intevation.gnv.jfreechart.PolygonPlot;
import de.intevation.gnv.jfreechart.PolygonSeries;
import de.intevation.gnv.jfreechart.PolygonRenderer;

import de.intevation.gnv.raster.Palette;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.SymbolAxis;

import org.jfree.chart.title.PaintScaleLegend;

import org.jfree.chart.renderer.LookupPaintScale;

import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

/**
 * @author Ingo Weinzierl      (ingo.weinzierl@intevation.de)
 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
 */
public class VerticalCrossSectionChart
implements   Chart
{
    public static final class PalettePaintLookup
    implements PolygonRenderer.PaintLookup
    {
        private Palette palette;

        public PalettePaintLookup(Palette palette) {
            this.palette = palette;
        }

        public Paint getPaint(int index) {
            return index < 0
                ? Color.black
                : palette.getColor(index);
        }
    } // class PalettePaintLookup

    public static class LocalizedLabelGenerator
    extends             PolygonRenderer.DefaultLabelGenerator
    {
        protected NumberFormat format;

        public LocalizedLabelGenerator() {
        }

        public LocalizedLabelGenerator(NumberFormat format) {
            this.format = format;
        }

        protected String toString(Object label) {
            return label instanceof Number
                ? format.format(((Number)label).doubleValue())
                : super.toString(label);
        }
    } // class LocalizedLabelGenerator

    protected JFreeChart chart;

    protected AttributedXYColumns columns;
    protected Palette             palette;
    protected Locale              locale;

    public VerticalCrossSectionChart() {
    }

    public VerticalCrossSectionChart(
        AttributedXYColumns columns,
        Palette             palette,
        Locale              locale
    ) {
        this.columns = columns;
        this.palette = palette;
        this.locale  = locale;
    }

    protected JFreeChart createChart() {

        String title        = "Neues 2D-Diagramm";
        String xAxis        = "x-Achse des Diagramms";
        String yAxis        = "y-Achse des Diagramms";
        boolean legendB     = false;
        boolean tooltips    = false;
        boolean urls        = false;

        PlotOrientation po  = PlotOrientation.HORIZONTAL;
        PolygonDataset data = columns.getPolygonDataset();

        HashSet<Integer> usedColors = new HashSet<Integer>();

        for (int i = data.getSeriesCount()-1; i >= 0; --i) {
            PolygonSeries ps = data.getSeries(i);
            Integer fill = (Integer)ps.getAttribute("fill");
            if (fill != null) {
                usedColors.add(fill);
            }
        }

        NumberFormat format = NumberFormat.getInstance(locale);
        format.setMinimumFractionDigits(0);
        format.setMaximumFractionDigits(2);

        PolygonRenderer renderer = new PolygonRenderer(
            new PalettePaintLookup(palette),
            new LocalizedLabelGenerator(format));

        ValueAxis domainAxis = new NumberAxis(xAxis);
        ValueAxis rangeAxis  = new NumberAxis(yAxis);

        PolygonPlot plot = new PolygonPlot(
            data,
            renderer,
            domainAxis,
            rangeAxis,
            null);

        String [] labels = new String[usedColors.size()];

        int colors = palette.getSize();
        LookupPaintScale lookupPaint =
            new LookupPaintScale(-0.5d, labels.length-0.5d, Color.white);

        Color color = null;

        for (int i = 0, j = labels.length-1; i < colors && j >= 0; i++) {
            if (usedColors.contains(i)) {
                Palette.Entry entry = palette.getEntryByIndex(i);
                color     = entry.getColor();
                labels[j] = entry.getDescription();
                lookupPaint.add(j-0.5d, color);
                --j;
            }
        }

        JFreeChart chart = new JFreeChart(
            title,
            JFreeChart.DEFAULT_TITLE_FONT,
            plot,
            legendB);

        chart.removeLegend();

        SymbolAxis scale = new SymbolAxis("Temperatur", labels);
        scale.setRange(-1.5d, labels.length+0.5d);
        scale.setGridBandsVisible(false);
        scale.setPlot(plot);

        PaintScaleLegend legend = new PaintScaleLegend(
            lookupPaint, scale);
        legend.setMargin(new RectangleInsets(3d, 10d, 3d, 10d));
        legend.setPosition(RectangleEdge.LEFT);
        legend.setAxisOffset(5d);

        chart.addSubtitle(legend);

        return chart;
    }

    public JFreeChart generateChart() {
        if (chart == null) {
            chart = createChart();
        }
        return chart;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org