view gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalCrossSectionChart.java @ 507:45be952a3215

Solved some issues. Removed encoding problems while formatting coordinates (issue137) and use this format as subtitle in charts (issue136). gnv-artifacts/trunk@590 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 21 Jan 2010 14:42:51 +0000
parents 79e80c289018
children 1bf058f1a2d1
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.title.TextTitle;

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;
    protected ChartLabels         labels;

    public VerticalCrossSectionChart() {
    }

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

    protected JFreeChart createChart() {

        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(this.labels.getDomainAxisLabel());
        ValueAxis rangeAxis  = new NumberAxis(this.labels.getRangeAxisLabel());

        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(
            this.labels.getTitle(),
            JFreeChart.DEFAULT_TITLE_FONT,
            plot,
            legendB);

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

        SymbolAxis scale = new SymbolAxis(this.labels.getParameterName(), 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