001: /***********************************************************************************************
002: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
003: *
004: * Redistribution and use of this software and associated documentation ("Software"), with or
005: * without modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain copyright statements and notices.
008: * Redistributions must also contain a copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
011: * conditions and the following disclaimer in the documentation and/or other materials
012: * provided with the distribution.
013: *
014: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
015: * products derived from this Software without prior written permission of Nathaniel G.
016: * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
017: *
018: * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
019: * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
020: * registered trademark of Nathaniel G. Auvil.
021: *
022: * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
023: *
024: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
025: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
026: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
028: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
029: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
030: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
031: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
032: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
033: ************************************************************************************************/package org.krysalis.jcharts.demo.userGuide;
034:
035: import org.krysalis.jcharts.chartData.ChartDataException;
036: import org.krysalis.jcharts.chartData.DataSeries;
037: import org.krysalis.jcharts.chartData.AxisChartDataSet;
038: import org.krysalis.jcharts.axisChart.AxisChart;
039: import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition;
040: import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer;
041: import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.BackgroundRenderer;
042: import org.krysalis.jcharts.properties.BarChartProperties;
043: import org.krysalis.jcharts.properties.LegendProperties;
044: import org.krysalis.jcharts.properties.AxisProperties;
045: import org.krysalis.jcharts.properties.ChartProperties;
046: import org.krysalis.jcharts.properties.ClusteredBarChartProperties;
047: import org.krysalis.jcharts.properties.StackedBarChartProperties;
048: import org.krysalis.jcharts.properties.util.ChartStroke;
049: import org.krysalis.jcharts.types.ChartType;
050: import org.krysalis.jcharts.test.TestDataGenerator;
051:
052: import java.awt.*;
053:
054: /*************************************************************************************
055: *
056: * @author Nathaniel Auvil
057: * @version $Id: BarChartsGuide.java,v 1.2 2003/06/25 01:40:15 nathaniel_auvil Exp $
058: ************************************************************************************/
059: public class BarChartsGuide extends AxisChartsGuide {
060:
061: /*****************************************************************************************
062: * Tests a 'real' data set and usage.
063: *
064: * @throws ChartDataException
065: ******************************************************************************************/
066: public void run() throws ChartDataException {
067: this .barChart();
068: this .stackedBars();
069: this .clusteredBars();
070:
071: this .barLabels();
072: this .barWidths();
073: this .barOutlines();
074: this .barBackground();
075: }
076:
077: /*****************************************************************************************
078: *
079: *
080: ******************************************************************************************/
081: private AxisChart getBarChart(BarChartProperties barChartProperties)
082: throws ChartDataException {
083: String[] xAxisLabels = { "1998", "1999", "2000", "2001",
084: "2002", "2003", "2004" };
085: String xAxisTitle = "Years";
086: String yAxisTitle = "Problems";
087: String title = "Micro$oft at Work";
088: DataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle,
089: yAxisTitle, title);
090:
091: double[][] data = new double[][] { { 250, 45, -36, 66, 145, 80,
092: 55 } };
093: String[] legendLabels = { "Bugs" };
094: Paint[] paints = TestDataGenerator.getRandomPaints(1);
095:
096: AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data,
097: legendLabels, paints, ChartType.BAR, barChartProperties);
098:
099: dataSeries.addIAxisPlotDataSet(axisChartDataSet);
100:
101: ChartProperties chartProperties = new ChartProperties();
102: AxisProperties axisProperties = new AxisProperties();
103: LegendProperties legendProperties = new LegendProperties();
104:
105: AxisChart axisChart = new AxisChart(dataSeries,
106: chartProperties, axisProperties, legendProperties,
107: AxisChartsGuide.width, AxisChartsGuide.height);
108:
109: return axisChart;
110: }
111:
112: /*****************************************************************************************
113: *
114: *
115: ******************************************************************************************
116: private AxisChart getMultiBarCharts( ChartType chartType, BarChartProperties barChartProperties ) throws ChartDataException
117: {
118: String[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
119: String xAxisTitle= "Years";
120: String yAxisTitle= "Problems";
121: String title= "Micro$oft at Work";
122: DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
123:
124:
125: double[][] data= new double[][]{ { } };
126: String[] legendLabels= { "Bugs", "Security Holes", "Backdoors" };
127: Paint[] paints= TestDataGenerator.getRandomPaints( 3 );
128:
129: AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, chartType, barChartProperties );
130:
131: dataSeries.addIAxisPlotDataSet( axisChartDataSet );
132:
133: ChartProperties chartProperties= new ChartProperties();
134: AxisProperties axisProperties= new AxisProperties();
135: LegendProperties legendProperties= new LegendProperties();
136:
137: AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisCharts.width, AxisCharts.height );
138:
139: return axisChart;
140: }
141:
142:
143:
144: /******************************************************************************************/
145: private void barChart() throws ChartDataException {
146: String[] xAxisLabels = { "1998", "1999", "2000", "2001",
147: "2002", "2003", "2004" };
148: String xAxisTitle = "Years";
149: String yAxisTitle = "Problems";
150: String title = "Micro$oft at Work";
151: DataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle,
152: yAxisTitle, title);
153:
154: double[][] data = new double[][] { { 250, 45, -36, 66, 145, 80,
155: 55 } };
156: String[] legendLabels = { "Bugs" };
157: Paint[] paints = new Paint[] { Color.blue.darker() };
158: BarChartProperties barChartProperties = new BarChartProperties();
159: AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data,
160: legendLabels, paints, ChartType.BAR, barChartProperties);
161: dataSeries.addIAxisPlotDataSet(axisChartDataSet);
162:
163: ChartProperties chartProperties = new ChartProperties();
164: AxisProperties axisProperties = new AxisProperties();
165: LegendProperties legendProperties = new LegendProperties();
166: AxisChart axisChart = new AxisChart(dataSeries,
167: chartProperties, axisProperties, legendProperties,
168: AxisChartsGuide.width, AxisChartsGuide.height);
169:
170: super .exportImage(axisChart, "barChart");
171: }
172:
173: /******************************************************************************************/
174: private void barLabels() throws ChartDataException {
175: String[] xAxisLabels = { "1998", "1999", "2000", "2001",
176: "2002", "2003", "2004" };
177: String xAxisTitle = "Years";
178: String yAxisTitle = "Problems";
179: String title = "Micro$oft at Work";
180: DataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle,
181: yAxisTitle, title);
182:
183: double[][] data = new double[][] { { 250, 45, -36, 66, 145, 80,
184: 55 } };
185: String[] legendLabels = { "Bugs" };
186: Paint[] paints = new Paint[] { Color.yellow };
187: BarChartProperties barChartProperties = new BarChartProperties();
188:
189: ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer(
190: false, false, true, -1);
191: valueLabelRenderer
192: .setValueLabelPosition(ValueLabelPosition.AT_TOP);
193: valueLabelRenderer.useVerticalLabels(false);
194: barChartProperties
195: .addPostRenderEventListener(valueLabelRenderer);
196:
197: AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data,
198: legendLabels, paints, ChartType.BAR, barChartProperties);
199: dataSeries.addIAxisPlotDataSet(axisChartDataSet);
200:
201: ChartProperties chartProperties = new ChartProperties();
202: AxisProperties axisProperties = new AxisProperties(true);
203: LegendProperties legendProperties = new LegendProperties();
204: AxisChart axisChart = new AxisChart(dataSeries,
205: chartProperties, axisProperties, legendProperties,
206: AxisChartsGuide.width, AxisChartsGuide.height);
207:
208: super .exportImage(axisChart, "barLabels");
209: }
210:
211: /******************************************************************************************/
212: private void barBackground() throws ChartDataException {
213: String[] xAxisLabels = { "1998", "1999", "2000", "2001",
214: "2002", "2003", "2004" };
215: String xAxisTitle = "Years";
216: String yAxisTitle = "Problems";
217: String title = "Micro$oft at Work";
218: DataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle,
219: yAxisTitle, title);
220:
221: double[][] data = new double[][] { { 250, 45, -36, 66, 145, 80,
222: 55 } };
223: String[] legendLabels = { "Bugs" };
224: Paint[] paints = new Paint[] { Color.blue.darker() };
225: BarChartProperties barChartProperties = new BarChartProperties();
226:
227: //---paints over the background of every other bar area.
228: BackgroundRenderer backgroundRenderer = new BackgroundRenderer(
229: new Color(20, 20, 20, 50));
230: barChartProperties
231: .addPreRenderEventListener(backgroundRenderer);
232:
233: AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data,
234: legendLabels, paints, ChartType.BAR, barChartProperties);
235: dataSeries.addIAxisPlotDataSet(axisChartDataSet);
236:
237: ChartProperties chartProperties = new ChartProperties();
238: AxisProperties axisProperties = new AxisProperties();
239: LegendProperties legendProperties = new LegendProperties();
240: AxisChart axisChart = new AxisChart(dataSeries,
241: chartProperties, axisProperties, legendProperties,
242: AxisChartsGuide.width, AxisChartsGuide.height);
243:
244: super .exportImage(axisChart, "barBackground");
245: }
246:
247: /**********************************************************************************************/
248: private void stackedBars() throws ChartDataException {
249: String[] xAxisLabels = { "1998", "1999", "2000", "2001",
250: "2002", "2003", "2004" };
251: String xAxisTitle = "Years";
252: String yAxisTitle = "Problems";
253: String title = "Micro$oft at Work";
254: DataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle,
255: yAxisTitle, title);
256:
257: double[][] data = new double[][] {
258: { 250, 45, 36, 66, 145, 80, 55 },
259: { 150, 15, 6, 62, 54, 10, 84 },
260: { 250, 45, 36, 66, 145, 80, 55 } };
261: String[] legendLabels = { "Bugs", "Security Holes", "Backdoors" };
262: Paint[] paints = TestDataGenerator.getRandomPaints(3);
263: StackedBarChartProperties stackedBarChartProperties = new StackedBarChartProperties();
264: AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data,
265: legendLabels, paints, ChartType.BAR_STACKED,
266: stackedBarChartProperties);
267: dataSeries.addIAxisPlotDataSet(axisChartDataSet);
268:
269: ChartProperties chartProperties = new ChartProperties();
270: AxisProperties axisProperties = new AxisProperties();
271: LegendProperties legendProperties = new LegendProperties();
272: AxisChart axisChart = new AxisChart(dataSeries,
273: chartProperties, axisProperties, legendProperties,
274: AxisChartsGuide.width, AxisChartsGuide.height);
275:
276: super .exportImage(axisChart, "stackedBarChart");
277: }
278:
279: /**********************************************************************************************/
280: private void clusteredBars() throws ChartDataException {
281: String[] xAxisLabels = { "1998", "1999", "2000", "2001",
282: "2002", "2003", "2004" };
283: String xAxisTitle = "Years";
284: String yAxisTitle = "Problems";
285: String title = "Micro$oft at Work";
286: DataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle,
287: yAxisTitle, title);
288:
289: double[][] data = new double[][] {
290: { 250, 45, -36, 66, 145, 80, 55 },
291: { 150, 15, 6, 62, -54, 10, 84 },
292: { 20, 145, 36, 6, 45, 18, 5 } };
293: String[] legendLabels = { "Bugs", "Security Holes", "Backdoors" };
294: Paint[] paints = TestDataGenerator.getRandomPaints(3);
295: ClusteredBarChartProperties clusteredBarChartProperties = new ClusteredBarChartProperties();
296: AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data,
297: legendLabels, paints, ChartType.BAR_CLUSTERED,
298: clusteredBarChartProperties);
299: dataSeries.addIAxisPlotDataSet(axisChartDataSet);
300:
301: ChartProperties chartProperties = new ChartProperties();
302: AxisProperties axisProperties = new AxisProperties();
303: LegendProperties legendProperties = new LegendProperties();
304: AxisChart axisChart = new AxisChart(dataSeries,
305: chartProperties, axisProperties, legendProperties,
306: AxisChartsGuide.width, AxisChartsGuide.height);
307:
308: super .exportImage(axisChart, "clusteredBarChart");
309: }
310:
311: /******************************************************************************************/
312: private void barWidths() throws ChartDataException {
313: BarChartProperties barChartProperties = new BarChartProperties();
314: barChartProperties.setWidthPercentage(1f);
315:
316: AxisChart axisChart = this .getBarChart(barChartProperties);
317:
318: super .exportImage(axisChart, "barChartWidths");
319: }
320:
321: /******************************************************************************************/
322: private void barOutlines() throws ChartDataException {
323: BarChartProperties barChartProperties = new BarChartProperties();
324: barChartProperties.setShowOutlinesFlag(true);
325: ChartStroke outlineChartStroke = new ChartStroke(
326: new BasicStroke(2.0f), Color.red);
327: barChartProperties.setBarOutlineStroke(outlineChartStroke);
328:
329: AxisChart axisChart = this .getBarChart(barChartProperties);
330:
331: super .exportImage(axisChart, "barChartOutlines");
332: }
333:
334: }
|