Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalCrossSectionChart.java @ 629:d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
gnv-artifacts/trunk@706 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 24 Feb 2010 14:30:52 +0000 |
parents | 92d7a8cd1ef1 |
children | 79401c871da4 |
line wrap: on
line source
package de.intevation.gnv.chart; import de.intevation.gnv.jfreechart.PolygonDataset; import de.intevation.gnv.jfreechart.PolygonPlot; import de.intevation.gnv.jfreechart.PolygonRenderer; import de.intevation.gnv.jfreechart.PolygonSeries; import de.intevation.gnv.math.AttributedXYColumns; import de.intevation.gnv.raster.Palette; import java.awt.Color; import java.awt.Paint; import java.text.NumberFormat; import java.util.HashSet; import java.util.Locale; import java.util.Map; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.SymbolAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.LookupPaintScale; import org.jfree.chart.title.PaintScaleLegend; import org.jfree.chart.title.TextTitle; 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; private Map<Integer, Paint> special; public PalettePaintLookup(Palette palette) { this(palette, null); } public PalettePaintLookup( Palette palette, Map<Integer, Paint> special ) { this.palette = palette; this.special = special; } public Paint getPaint(int index) { if (special != null) { Paint paint = special.get(index); if (paint != null) { return paint; } } 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 Map<Integer, Paint> special; protected Palette palette; protected Locale locale; protected ChartLabels labels; public VerticalCrossSectionChart() { } public VerticalCrossSectionChart( AttributedXYColumns columns, Palette palette, Locale locale, ChartLabels labels ) { this(columns, palette, null, locale, labels); } public VerticalCrossSectionChart( AttributedXYColumns columns, Palette palette, Map<Integer, Paint> special, Locale locale, ChartLabels labels ) { this.columns = columns; this.palette = palette; this.special = special; 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 && (special != null && !special.containsKey(fill))) { usedColors.add(fill); } } NumberFormat format = NumberFormat.getInstance(locale); format.setMinimumFractionDigits(0); format.setMaximumFractionDigits(2); PolygonRenderer renderer = new PolygonRenderer( new PalettePaintLookup(palette, special), 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); plot.setOutlinePaint(Color.WHITE); 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; } public void setBackgroundPaint(Paint paint) { chart.setBackgroundPaint(paint); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :