工作需要后台生成直观的图形报表,搜了一下,用的最广泛的就是JFreeChart,官方网址:http://www.jfree.org/jfreechart/index.html
看了一下提供的demo,调用应该算比较简单的,但功能还是很强的,只要能了解每种图表对应的对象与数据格式,使用起来就很爽了,可以方便的生成各式各样的图表。只可惜具体的说明文档想看的话是要付费的…… (-___-!)
没有办法,只能反编译它所提供的demo,直接看源码,还好不算复杂。于是自己拿来改巴改巴用了,以下代码在jdk1.4 tomcat4.0上测试通过。(这个是折线图表)
import org.jfree.data.xy.XYDataset;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.XYPlot;
import org.jfree.ui.RectangleInsets;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.axis.DateAxis;
import java.text.SimpleDateFormat;
import java.awt.Color;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.Week;
import org.jfree.data.time.TimeSeriesCollection;
import java.io.FileOutputStream;
import org.jfree.chart.ChartUtilities;
import java.io.IOException;
public class JFree01 {
public static void main(String args[]) throws IOException {
XYDataset xydataset = createDataset();
JFreeChart jfreechart = createChart(xydataset);
// ChartPanel chartpanel = new ChartPanel(jfreechart, false);
// chartpanel.setPreferredSize(new Dimension(500, 270));
// chartpanel.setMouseZoomable(true, false);
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("D:/Program/2006/JFreeChart/jfc01.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,100,jfreechart,600,400,null);
} finally {
try {
fos_jpg.close();
} catch (Exception e) {}
}
}
private static JFreeChart createChart(XYDataset xydataset) {
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(
"Legal & General Unit Trust Prices", "Date", "Price Per Unit",
xydataset, true, true, false);
jfreechart.setBackgroundPaint(Color.white);
XYPlot xyplot = (XYPlot) jfreechart.getPlot();
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
xyplot.setDomainCrosshairVisible(true);
xyplot.setRangeCrosshairVisible(true);
org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot.getRenderer();
if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyitemrenderer;
xylineandshaperenderer.setBaseShapesVisible(true);
xylineandshaperenderer.setBaseShapesFilled(true);
}
DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
dateaxis.setDateFormatOverride(new SimpleDateFormat("ww-yyyy"));
return jfreechart;
}
private static XYDataset createDataset() {
TimeSeries ts[] = new TimeSeries[4];
ts[1] = new TimeSeries("L&G European Index Trust", org.jfree.data.time.Week.class);
TimeSeries timeseries = new TimeSeries("L&G European Index Trust", org.jfree.data.time.Week.class);
timeseries.add(new Week(2, 2001), 181.80000000000001D);
timeseries.add(new Week(3, 2001), 167.30000000000001D);
timeseries.add(new Week(4, 2001), 153.80000000000001D);
timeseries.add(new Week(5, 2001), 167.59999999999999D);
timeseries.add(new Week(6, 2001), 158.80000000000001D);
timeseries.add(new Week(7, 2001), 148.30000000000001D);
timeseries.add(new Week(8, 2001), 153.90000000000001D);
timeseries.add(new Week(9, 2001), 142.69999999999999D);
timeseries.add(new Week(10, 2001), 123.2D);
timeseries.add(new Week(11, 2001), 131.80000000000001D);
timeseries.add(new Week(12, 2001), 139.59999999999999D);
TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust", org.jfree.data.time.Week.class);
timeseries1.add(new Week(2, 2001), 129.59999999999999D);
timeseries1.add(new Week(3, 2001), 123.2D);
timeseries1.add(new Week(4, 2001), 117.2D);
timeseries1.add(new Week(5, 2001), 124.09999999999999D);
timeseries1.add(new Week(6, 2001), 122.59999999999999D);
timeseries1.add(new Week(7, 2001), 119.2D);
timeseries1.add(new Week(8, 2001), 116.5D);
timeseries1.add(new Week(9, 2001), 112.7D);
timeseries1.add(new Week(10, 2001), 101.5D);
timeseries1.add(new Week(11, 2001), 106.09999999999999D);
timeseries1.add(new Week(12, 2001), 110.3D);
TimeSeries timeseries2 = new TimeSeries("L&G CN Index Trust", org.jfree.data.time.Week.class);
timeseries2.add(new Week(2, 2001), 29.59999999999999D);
timeseries2.add(new Week(3, 2001), 23.2D);
timeseries2.add(new Week(4, 2001), 17.2D);
timeseries2.add(new Week(5, 2001), 24.09999999999999D);
timeseries2.add(new Week(6, 2001), 22.59999999999999D);
timeseries2.add(new Week(7, 2001), 19.2D);
timeseries2.add(new Week(8, 2001), 16.5D);
timeseries2.add(new Week(9, 2001), 12.7D);
timeseries2.add(new Week(10, 2001), 01.5D);
timeseries2.add(new Week(11, 2001), 06.09999999999999D);
timeseries2.add(new Week(12, 2001), 10.3D);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
timeseriescollection.addSeries(timeseries);
timeseriescollection.addSeries(timeseries1);
timeseriescollection.addSeries(timeseries2);
timeseriescollection.setDomainIsPointsInTime(true);
return timeseriescollection;
}
public JFree01() {
}
}
下图就是用上面代码生成的图表:

回复Comments
作者:
{commentrecontent}