sascha@446: package de.intevation.gnv.chart; sascha@446: sascha@446: import java.util.Locale; sascha@446: sascha@450: import java.text.NumberFormat; sascha@450: sascha@446: import java.awt.Color; sascha@446: import java.awt.Paint; sascha@446: sascha@449: import java.util.HashSet; sascha@449: sascha@446: import de.intevation.gnv.math.AttributedXYColumns; sascha@446: sascha@446: import de.intevation.gnv.jfreechart.PolygonDataset; sascha@446: import de.intevation.gnv.jfreechart.PolygonPlot; sascha@449: import de.intevation.gnv.jfreechart.PolygonSeries; sascha@446: import de.intevation.gnv.jfreechart.PolygonRenderer; sascha@446: sascha@446: import de.intevation.gnv.raster.Palette; sascha@446: sascha@446: import org.jfree.chart.JFreeChart; sascha@446: sascha@446: import org.jfree.chart.plot.PlotOrientation; sascha@446: sascha@446: import org.jfree.chart.axis.ValueAxis; sascha@446: import org.jfree.chart.axis.NumberAxis; sascha@446: import org.jfree.chart.axis.SymbolAxis; sascha@446: sascha@446: import org.jfree.chart.title.PaintScaleLegend; ingo@492: import org.jfree.chart.title.TextTitle; sascha@446: sascha@446: import org.jfree.chart.renderer.LookupPaintScale; sascha@446: sascha@446: import org.jfree.ui.RectangleEdge; sascha@446: import org.jfree.ui.RectangleInsets; sascha@446: sascha@446: /** sascha@446: * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) sascha@446: * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) sascha@446: */ sascha@446: public class VerticalCrossSectionChart sascha@446: implements Chart sascha@446: { sascha@446: public static final class PalettePaintLookup sascha@446: implements PolygonRenderer.PaintLookup sascha@446: { sascha@446: private Palette palette; sascha@446: sascha@446: public PalettePaintLookup(Palette palette) { sascha@446: this.palette = palette; sascha@446: } sascha@446: sascha@446: public Paint getPaint(int index) { sascha@446: return index < 0 sascha@446: ? Color.black sascha@446: : palette.getColor(index); sascha@446: } sascha@446: } // class PalettePaintLookup sascha@446: sascha@450: public static class LocalizedLabelGenerator sascha@450: extends PolygonRenderer.DefaultLabelGenerator sascha@450: { sascha@450: protected NumberFormat format; sascha@450: sascha@450: public LocalizedLabelGenerator() { sascha@450: } sascha@450: sascha@450: public LocalizedLabelGenerator(NumberFormat format) { sascha@450: this.format = format; sascha@450: } sascha@450: sascha@450: protected String toString(Object label) { sascha@450: return label instanceof Number sascha@450: ? format.format(((Number)label).doubleValue()) sascha@450: : super.toString(label); sascha@450: } sascha@450: } // class LocalizedLabelGenerator sascha@450: sascha@446: protected JFreeChart chart; sascha@446: sascha@446: protected AttributedXYColumns columns; sascha@446: protected Palette palette; sascha@446: protected Locale locale; ingo@492: protected ChartLabels labels; sascha@446: sascha@446: public VerticalCrossSectionChart() { sascha@446: } sascha@446: sascha@446: public VerticalCrossSectionChart( sascha@446: AttributedXYColumns columns, sascha@446: Palette palette, ingo@492: Locale locale, ingo@492: ChartLabels labels sascha@446: ) { sascha@446: this.columns = columns; sascha@446: this.palette = palette; sascha@446: this.locale = locale; ingo@492: this.labels = labels; sascha@446: } sascha@446: sascha@446: protected JFreeChart createChart() { sascha@446: ingo@492: boolean legendB = false; ingo@492: boolean tooltips = false; ingo@492: boolean urls = false; sascha@446: sascha@446: PlotOrientation po = PlotOrientation.HORIZONTAL; sascha@446: PolygonDataset data = columns.getPolygonDataset(); sascha@446: sascha@449: HashSet usedColors = new HashSet(); sascha@449: sascha@449: for (int i = data.getSeriesCount()-1; i >= 0; --i) { sascha@449: PolygonSeries ps = data.getSeries(i); sascha@449: Integer fill = (Integer)ps.getAttribute("fill"); sascha@449: if (fill != null) { sascha@449: usedColors.add(fill); sascha@449: } sascha@449: } sascha@449: sascha@450: NumberFormat format = NumberFormat.getInstance(locale); sascha@450: format.setMinimumFractionDigits(0); sascha@450: format.setMaximumFractionDigits(2); sascha@450: sascha@446: PolygonRenderer renderer = new PolygonRenderer( sascha@450: new PalettePaintLookup(palette), sascha@450: new LocalizedLabelGenerator(format)); sascha@446: ingo@492: ValueAxis domainAxis = new NumberAxis(this.labels.getDomainAxisLabel()); ingo@492: ValueAxis rangeAxis = new NumberAxis(this.labels.getRangeAxisLabel()); sascha@446: sascha@446: PolygonPlot plot = new PolygonPlot( sascha@446: data, sascha@446: renderer, sascha@446: domainAxis, sascha@446: rangeAxis, sascha@446: null); sascha@446: sascha@449: String [] labels = new String[usedColors.size()]; sascha@449: sascha@446: int colors = palette.getSize(); sascha@446: LookupPaintScale lookupPaint = sascha@449: new LookupPaintScale(-0.5d, labels.length-0.5d, Color.white); sascha@446: sascha@446: Color color = null; sascha@446: sascha@449: for (int i = 0, j = labels.length-1; i < colors && j >= 0; i++) { sascha@449: if (usedColors.contains(i)) { sascha@449: Palette.Entry entry = palette.getEntryByIndex(i); sascha@449: color = entry.getColor(); sascha@449: labels[j] = entry.getDescription(); sascha@449: lookupPaint.add(j-0.5d, color); sascha@449: --j; sascha@449: } sascha@446: } sascha@446: sascha@446: JFreeChart chart = new JFreeChart( ingo@492: this.labels.getTitle(), sascha@446: JFreeChart.DEFAULT_TITLE_FONT, sascha@446: plot, sascha@446: legendB); sascha@446: sascha@446: chart.removeLegend(); ingo@492: chart.addSubtitle(new TextTitle(this.labels.getSubtitle())); sascha@446: ingo@492: SymbolAxis scale = new SymbolAxis(this.labels.getParameterName(), labels); sascha@449: scale.setRange(-1.5d, labels.length+0.5d); sascha@446: scale.setGridBandsVisible(false); sascha@446: scale.setPlot(plot); sascha@446: sascha@446: PaintScaleLegend legend = new PaintScaleLegend( sascha@446: lookupPaint, scale); sascha@446: legend.setMargin(new RectangleInsets(3d, 10d, 3d, 10d)); sascha@446: legend.setPosition(RectangleEdge.LEFT); sascha@446: legend.setAxisOffset(5d); sascha@446: sascha@446: chart.addSubtitle(legend); sascha@446: sascha@446: return chart; sascha@446: } sascha@446: sascha@446: public JFreeChart generateChart() { sascha@446: if (chart == null) { sascha@446: chart = createChart(); sascha@446: } sascha@446: return chart; sascha@446: } sascha@446: } sascha@446: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :