001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb;
023:
024: import java.awt.*;
025: import java.awt.Color;
026: import java.awt.Dimension;
027: import java.text.DateFormat;
028: import java.util.Calendar;
029: import java.util.Date;
030: import java.util.Vector;
031: import javax.swing.*;
032: import javax.swing.JPanel;
033: import org.jfree.chart.ChartFactory;
034: import org.jfree.chart.ChartPanel;
035: import org.jfree.chart.JFreeChart;
036: import org.jfree.chart.axis.CategoryAxis;
037: import org.jfree.chart.axis.CategoryLabelPositions;
038: import org.jfree.chart.axis.NumberAxis;
039: import org.jfree.chart.plot.*;
040: import org.jfree.chart.renderer.category.CategoryItemRenderer;
041: import org.jfree.data.category.CategoryDataset;
042: import org.jfree.data.category.DefaultCategoryDataset;
043: import org.jfree.data.general.*;
044: import org.jfree.data.time.*;
045: import org.jfree.data.xy.*;
046: import org.jfree.ui.*;
047:
048: public class Chart {
049: public static final int BAR = 1;
050: public static final int BAR3D = 2;
051: public static final int PIE3D = 3;
052: public static final int XYLINE = 4;
053: public static final int PIE = 5;
054:
055: private ChartPanel chartpanel;
056: private JFreeChart jfreechart;
057: private int chartType;
058:
059: public Chart(Vector x, Vector y, int chartType) {
060: this .chartType = chartType;
061:
062: switch (chartType) {
063: case Chart.BAR:
064: case Chart.BAR3D: {
065: CategoryDataset categorydataset = this .createDataset(x, y);
066: jfreechart = this .createChart(categorydataset);
067: break;
068: }
069: case Chart.PIE3D:
070: case Chart.PIE: {
071: PieDataset categorydataset = this .createPieDataset(x, y);
072: jfreechart = this .createChart(categorydataset);
073: break;
074: }
075: case Chart.XYLINE: {
076: XYDataset categorydataset = this .createXYDataset(x, y);
077: jfreechart = this .createChart(categorydataset);
078: break;
079: }
080: }
081:
082: chartpanel = new ChartPanel(jfreechart);
083: }
084:
085: private CategoryDataset createDataset(Vector x, Vector y) {
086: DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
087:
088: for (int i = 0; i < x.size(); i++) {
089: String xValue = (String) x.get(i);
090: double yValue = 0;
091:
092: try {
093: yValue = Double.parseDouble((String) y.get(i));
094:
095: } catch (NumberFormatException e) {
096: JOptionPane.showMessageDialog(null,
097: "Y values can not be string!");
098: i = x.size();
099: }
100:
101: defaultcategorydataset.addValue(yValue, xValue, xValue);
102: }
103:
104: return defaultcategorydataset;
105: }
106:
107: private PieDataset createPieDataset(Vector x, Vector y) {
108: DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
109:
110: for (int i = 0; i < x.size(); i++) {
111: String xValue = (String) x.get(i);
112: double yValue = 0;
113:
114: try {
115: yValue = Double.parseDouble((String) y.get(i));
116:
117: } catch (NumberFormatException e) {
118: JOptionPane.showMessageDialog(null,
119: "Y values can not be string!");
120: i = x.size();
121: }
122:
123: defaultpiedataset.setValue(xValue, yValue);
124: }
125:
126: return defaultpiedataset;
127: }
128:
129: private XYDataset createXYDataset(Vector x, Vector y) {
130: //DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, java.util.Locale.getDefault());
131:
132: XYSeries xyseries = new XYSeries("First");
133: xyseries.add(0, 0);
134:
135: //TimeSeries xyseries = new TimeSeries("Series 1", org.jfree.data.time.Month.class);
136:
137: for (int i = 0; i < x.size(); i++) {
138: try {
139: // Date d = df.parse((String)x.get(i));
140: // Calendar c = Calendar.getInstance();
141: // c.setTime(d);
142: // int month = c.get(Calendar.MONTH);
143: // int year = c.get(Calendar.YEAR);
144:
145: int xValue = Integer.parseInt((String) x.get(i));
146: double yValue = Double.parseDouble((String) y.get(i));
147:
148: xyseries.add(xValue, yValue);
149: //xyseries.addOrUpdate(new Month(month + 1, year), yValue);
150:
151: } catch (NumberFormatException exc) {
152: JOptionPane
153: .showMessageDialog(null,
154: "Both the data on the aces for this graph have to be numerical.");
155: i = x.size();
156: }
157: }
158:
159: XYSeriesCollection xyseriescollection = new XYSeriesCollection();
160: //TimeSeriesCollection xyseriescollection = new TimeSeriesCollection();
161: xyseriescollection.addSeries(xyseries);
162:
163: return xyseriescollection;
164: }
165:
166: private JFreeChart createChart(PieDataset categorydataset) {
167: if (chartType == Chart.PIE3D) {
168: jfreechart = org.jfree.chart.ChartFactory.createPieChart3D(
169: "", categorydataset, false, true, false);
170: PiePlot3D pieplot3d = (PiePlot3D) jfreechart.getPlot();
171: pieplot3d.setStartAngle(290D);
172: pieplot3d.setDirection(org.jfree.util.Rotation.CLOCKWISE);
173: pieplot3d.setForegroundAlpha(0.5F);
174: pieplot3d.setNoDataMessage("No data to display");
175: } else {
176: jfreechart = org.jfree.chart.ChartFactory.createPieChart(
177: "", categorydataset, false, true, false);
178: PiePlot pieplot = (PiePlot) jfreechart.getPlot();
179: pieplot.setLabelFont(new Font("SansSerif", 0, 12));
180: pieplot.setNoDataMessage("No data available");
181: pieplot.setCircular(false);
182: pieplot.setLabelGap(0.02D);
183: }
184:
185: return jfreechart;
186: }
187:
188: private JFreeChart createChart(CategoryDataset categorydataset) {
189: if (chartType == Chart.BAR) {
190: jfreechart = ChartFactory.createBarChart("", "Category",
191: "Value", categorydataset,
192: org.jfree.chart.plot.PlotOrientation.VERTICAL,
193: false, true, false);
194: } else {
195: jfreechart = ChartFactory.createBarChart3D("", "Category",
196: "Value", categorydataset,
197: org.jfree.chart.plot.PlotOrientation.VERTICAL,
198: false, true, false);
199: }
200:
201: jfreechart.setBackgroundPaint(java.awt.Color.white);
202: CategoryPlot categoryplot = jfreechart.getCategoryPlot();
203: categoryplot.setBackgroundPaint(java.awt.Color.lightGray);
204: categoryplot.setDomainGridlinePaint(java.awt.Color.white);
205: categoryplot.setRangeGridlinePaint(java.awt.Color.white);
206: org.jfree.chart.axis.NumberAxis numberaxis = (org.jfree.chart.axis.NumberAxis) categoryplot
207: .getRangeAxis();
208: numberaxis.setStandardTickUnits(org.jfree.chart.axis.NumberAxis
209: .createIntegerTickUnits());
210: numberaxis.setUpperMargin(0.14999999999999999D);
211: org.jfree.chart.renderer.category.CategoryItemRenderer categoryitemrenderer = categoryplot
212: .getRenderer();
213: categoryitemrenderer.setSeriesItemLabelsVisible(0,
214: java.lang.Boolean.TRUE);
215: org.jfree.chart.axis.CategoryAxis categoryaxis = categoryplot
216: .getDomainAxis();
217: categoryaxis
218: .setCategoryLabelPositions(org.jfree.chart.axis.CategoryLabelPositions.UP_45);
219: return jfreechart;
220: }
221:
222: private JFreeChart createChart(XYDataset xydataset) {
223: org.jfree.chart.JFreeChart jfreechart = org.jfree.chart.ChartFactory
224: .createXYLineChart("", "X", "Y", xydataset,
225: org.jfree.chart.plot.PlotOrientation.VERTICAL,
226: true, true, false);
227: //org.jfree.chart.JFreeChart jfreechart = org.jfree.chart.ChartFactory.createTimeSeriesChart("", "Date", "Value", xydataset, true, true, false);
228: jfreechart.setBackgroundPaint(java.awt.Color.white);
229: org.jfree.chart.StandardLegend standardlegend = (org.jfree.chart.StandardLegend) jfreechart
230: .getLegend();
231: standardlegend.setDisplaySeriesShapes(true);
232: org.jfree.chart.plot.XYPlot xyplot = jfreechart.getXYPlot();
233:
234: xyplot.setBackgroundPaint(java.awt.Color.lightGray);
235: xyplot.setAxisOffset(new Spacer(1, 5D, 5D, 5D, 5D));
236: xyplot.setDomainGridlinePaint(java.awt.Color.white);
237: xyplot.setRangeGridlinePaint(java.awt.Color.white);
238: xyplot.setDomainCrosshairLockedOnData(true);
239: xyplot.setDomainCrosshairVisible(true);
240: xyplot.setRangeCrosshairLockedOnData(true);
241: xyplot.setRangeCrosshairVisible(true);
242: org.jfree.chart.renderer.xy.StandardXYItemRenderer standardxyitemrenderer = (org.jfree.chart.renderer.xy.StandardXYItemRenderer) xyplot
243: .getRenderer();
244: standardxyitemrenderer
245: .setDefaultShapesFilled(java.lang.Boolean.TRUE);
246: standardxyitemrenderer.setItemLabelsVisible(true);
247: org.jfree.chart.axis.NumberAxis numberaxis = (org.jfree.chart.axis.NumberAxis) xyplot
248: .getRangeAxis();
249: numberaxis.setStandardTickUnits(org.jfree.chart.axis.NumberAxis
250: .createIntegerTickUnits());
251:
252: /*org.jfree.chart.axis.DateAxis dateaxis = (org.jfree.chart.axis.DateAxis)xyplot.getDomainAxis();
253: dateaxis.setDateFormatOverride(new java.text.SimpleDateFormat("MMM-yyyy"));
254: org.jfree.chart.axis.ValueAxis valueaxis = xyplot.getRangeAxis();
255: valueaxis.setAutoRangeMinimumSize(1.0D);
256: */
257: return jfreechart;
258: }
259:
260: public javax.swing.JPanel getPanel() {
261: return chartpanel;
262: }
263: }
|