comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractHistogram.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents f953c9a559d8
children
comparison
equal deleted inserted replaced
1027:fca4b5eb8d2f 1119:7c4f81f74c47
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.gnv.chart;
10
11 import java.util.Locale;
12
13 import org.apache.log4j.Logger;
14
15 import org.jfree.chart.ChartFactory;
16 import org.jfree.chart.ChartTheme;
17 import org.jfree.chart.JFreeChart;
18
19 import org.jfree.chart.axis.NumberAxis;
20 import org.jfree.chart.axis.TickUnitSource;
21
22 import org.jfree.chart.plot.PlotOrientation;
23 import org.jfree.chart.plot.XYPlot;
24
25 import org.jfree.chart.renderer.xy.XYBarRenderer;
26
27 /**
28 * This abstract class defines some methods to adjust chart settings after its
29 * creation.
30 *
31 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
32 */
33 public abstract class AbstractHistogram
34 implements Chart
35 {
36 /**
37 * Logger used for logging with Apache log4j.
38 */
39 private Logger logger = Logger.getLogger(AbstractHistogram.class);
40
41 /**
42 * JFreeChart chart stored at this place after chart creation.
43 */
44 protected JFreeChart chart;
45
46 /**
47 * Labels used for chart title, subtitle, axis description.
48 */
49 protected ChartLabels labels;
50
51 /**
52 * Theme which is used to adjust the styling of this chart.
53 */
54 protected ChartTheme theme;
55
56 /**
57 * Raw data which should be displayed in the chart.
58 */
59 protected Object[] data;
60
61 /**
62 * Locale object used for i18n support.
63 */
64 protected Locale locale;
65
66
67 /**
68 * Constructor for creating histogram charts.
69 *
70 * @param labels See {@link #labels}
71 * @param data See {@link #data}
72 * @param theme See {@link #theme}
73 */
74 public AbstractHistogram(
75 ChartLabels labels, Object[] data, ChartTheme theme
76 ) {
77 this.labels = labels;
78 this.data = data;
79 this.theme = theme;
80 }
81
82
83 /**
84 * @see de.intevation.gnv.chart.Chart#generateChart()
85 */
86 public JFreeChart generateChart() {
87
88 if (chart != null)
89 return chart;
90
91 chart = ChartFactory.createHistogram(
92 labels.getTitle(),
93 labels.getDomainAxisLabel(),
94 labels.getRangeAxisLabel(),
95 null,
96 PlotOrientation.VERTICAL,
97 true,
98 false,
99 false);
100
101 applyDatasets();
102
103 theme.apply(chart);
104 adjustPlot();
105 adjustDomainAxis(chart);
106 adjustRangeAxis(chart);
107
108 return chart;
109 }
110
111
112 /**
113 * Method to do some changes in plot settings.
114 */
115 protected void adjustPlot() {
116 XYPlot plot = (XYPlot) chart.getPlot();
117 XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
118
119 renderer.setShadowVisible(false);
120 renderer.setSeriesVisibleInLegend(0, false);
121 }
122
123
124 protected void adjustDomainAxis(JFreeChart chart) {
125 XYPlot plot = (XYPlot) chart.getPlot();
126 NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
127
128 TickUnitSource tus = domainAxis.createStandardTickUnits(locale);
129 domainAxis.setStandardTickUnits(tus);
130 }
131
132
133 protected void adjustRangeAxis(JFreeChart chart) {
134 XYPlot plot = (XYPlot) chart.getPlot();
135 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
136
137 TickUnitSource tus = rangeAxis.createStandardTickUnits(locale);
138 rangeAxis.setStandardTickUnits(tus);
139 }
140
141
142
143
144 /**
145 * This method needs to be implemented by subclasses and should add valid
146 * <code>HistogramDataset</code> objects to the created chart. It is called
147 * by {@link #generateChart} after chart creation.
148 */
149 protected abstract void applyDatasets();
150 }
151 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org