📄 chartcreator.java
字号:
/*
* Copyright (C) 2003 Erik Swenson - eswenson@opensourcesoft.net
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package org.efs.openreports.util;
import java.awt.Image;
import org.efs.openreports.objects.ReportChart;
import org.efs.openreports.objects.chart.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.*;
import org.jfree.data.time.*;
public class ChartCreator
{
public static Image createChart(
ReportChart reportChart,
ChartValue[] values)
{
JFreeChart chart = null;
switch (reportChart.getChartType())
{
case ReportChart.BAR_CHART :
chart = createBarChart(reportChart, values);
break;
case ReportChart.PIE_CHART :
chart = createPieChart(reportChart, values);
break;
case ReportChart.XY_CHART :
chart = createXYChart(reportChart, values);
break;
case ReportChart.TIME_CHART :
chart = createTimeChart(reportChart, values);
break;
}
if (chart == null)
return null;
return chart.createBufferedImage(
reportChart.getWidth(),
reportChart.getHeight());
}
public static JFreeChart createBarChart(
ReportChart reportChart,
ChartValue[] values)
{
CategoryDataset dataset = createCategoryDataset(values);
JFreeChart chart =
ChartFactory.createBarChart3D(
reportChart.getDescription(),
reportChart.getXAxisLabel(),
reportChart.getYAxisLabel(),
dataset,
PlotOrientation.VERTICAL,
true,
true,
false);
return chart;
}
public static JFreeChart createPieChart(
ReportChart reportChart,
ChartValue[] values)
{
PieDataset dataset = createPieDataset(values);
JFreeChart chart =
ChartFactory.createPieChart3D(
reportChart.getDescription(),
dataset,
true,
true,
false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS);
return chart;
}
public static JFreeChart createXYChart(
ReportChart reportChart,
ChartValue[] values)
{
XYDataset dataset = createXYDataset(values);
JFreeChart chart =
ChartFactory.createXYLineChart(
reportChart.getDescription(),
reportChart.getXAxisLabel(),
reportChart.getYAxisLabel(),
dataset,
PlotOrientation.VERTICAL,
true,
true,
false);
return chart;
}
public static JFreeChart createTimeChart(
ReportChart reportChart,
ChartValue[] values)
{
XYDataset dataset = createTimeDataset(values);
JFreeChart chart =
ChartFactory.createTimeSeriesChart(
reportChart.getDescription(),
reportChart.getXAxisLabel(),
reportChart.getYAxisLabel(),
dataset,
true,
true,
false);
return chart;
}
public static CategoryDataset createCategoryDataset(ChartValue[] values)
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < values.length; i++)
{
CategoryChartValue value = (CategoryChartValue) values[i];
dataset.addValue(
value.getValue(),
value.getSeries(),
value.getCategory());
}
return dataset;
}
public static PieDataset createPieDataset(ChartValue[] values)
{
DefaultPieDataset dataset = new DefaultPieDataset();
for (int i = 0; i < values.length; i++)
{
PieChartValue value = (PieChartValue) values[i];
dataset.setValue(value.getKey(), value.getValue());
}
return dataset;
}
public static XYDataset createXYDataset(ChartValue[] values)
{
XYSeries series = null;
XYSeriesCollection seriesCollection = new XYSeriesCollection();
for (int i = 0; i < values.length; i++)
{
XYChartValue value = (XYChartValue) values[i];
if (series == null || !series.getName().equals(value.getSeries()))
{
if (series != null)
{
seriesCollection.addSeries(series);
}
series = new XYSeries(value.getSeries());
}
series.add(value.getValue(), value.getSecondValue());
}
seriesCollection.addSeries(series);
return seriesCollection;
}
public static XYDataset createTimeDataset(ChartValue[] values)
{
TimeSeries series = null;
TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
for (int i = 0; i < values.length; i++)
{
TimeChartValue value = (TimeChartValue) values[i];
if (series == null || !series.getName().equals(value.getSeries()))
{
if (series != null)
{
seriesCollection.addSeries(series);
}
series = new TimeSeries(value.getSeries(), Second.class);
}
series.add(new Second(value.getTime()), value.getValue());
}
seriesCollection.addSeries(series);
return seriesCollection;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -