JFreeChart核心类库介绍:
研究jfreechart源码发现源码的主要由两个大的包组成:org.jfree.chart,org.jfree.data。其中前者主要与图形
本身有关,后者与图形显示的数据有关。具体研究如果大家有兴趣的话可以自己研究,以后有时间我会告诉大家怎么去
研究源码。
核心类主要有:
org.jfree.chart.JFreeChart:图表对象,任何类型的图表的最终表现形式都是在该对象进行一些属性的定制。JFreeChart引擎本身提供了一个工厂类用于创建不同类型的图表对象
org.jfree.data.category.XXXDataSet:数据集对象,用于提供显示图表所用的数据。根据不同类型的图表对应着很多类型的数据集对象类
org.jfree.chart.plot.XXXPlot:图表区域对象,基本上这个对象决定着什么样式的图表,创建该对象的时候需要Axis、Renderer以及数据集对象的支持
org.jfree.chart.axis.XXXAxis:用于处理图表的两个轴:纵轴和横轴
org.jfree.chart.render.XXXRender:负责如何显示一个图表对象
org.jfree.chart.urls.XXXURLGenerator:用于生成Web图表中每个项目的鼠标点击链接
XXXXXToolTipGenerator:用于生成图象的帮助提示,不同类型图表对应不同类型的工具提示类.
折线图
折线图的dataset 两种CatagoryDataset接口(具体实现类是DefaultCategoryDataset),XYDataset 接口
1、CatagoryDataset接口:
A、创建一个数据源(dataset):
private static CategoryDataset createDataset()
{
String series1 = “First”;
String series2 = “Second”;
String series3 = “Third”;
String type1 = “Type 1″;
String type2 = “Type 2″;
String type3 = “Type 3″;
String type4 = “Type 4″;
String type5 = “Type 5″;
String type6 = “Type 6″;
String type7 = “Type 7″;
String type8 = “Type 8″;
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(1.0D, series1, type1);
defaultcategorydataset.addValue(4D, series1, type2);
defaultcategorydataset.addValue(3D, series1, type3);
defaultcategorydataset.addValue(5D, series1, type4);
defaultcategorydataset.addValue(5D, series1, type5);
defaultcategorydataset.addValue(7D, series1, type6);
defaultcategorydataset.addValue(7D, series1, type7);
defaultcategorydataset.addValue(8D, series1, type8);
defaultcategorydataset.addValue(5D, series2, type1);
defaultcategorydataset.addValue(7D, series2, type2);
defaultcategorydataset.addValue(6D, series2, type3);
defaultcategorydataset.addValue(8D, series2, type4);
defaultcategorydataset.addValue(4D, series2, type5);
defaultcategorydataset.addValue(4D, series2, type6);
defaultcategorydataset.addValue(2D, series2, type7);
defaultcategorydataset.addValue(1.0D, series2, type8);
defaultcategorydataset.addValue(4D, series3, type1);
defaultcategorydataset.addValue(3D, series3, type2);
defaultcategorydataset.addValue(2D, series3, type3);
defaultcategorydataset.addValue(3D, series3, type4);
defaultcategorydataset.addValue(6D, series3, type5);
defaultcategorydataset.addValue(3D, series3, type6);
defaultcategorydataset.addValue(4D, series3, type7);
defaultcategorydataset.addValue(3D, series3, type8);
return defaultcategorydataset;
}
B、由ChartFactory 产生 JFreeChart 对象 (与上面重复的部分就不再注释)
private static JFreeChart createChart(CategoryDataset categorydataset)
{
JFreeChart jfreechart = ChartFactory.createLineChart(”Line Chart Demo 1″,
“Type”,
“Value”,
categorydataset,
PlotOrientation.VERTICAL,
true,
true,
false);
jfreechart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.white);
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
//获得renderer 注意这里是下嗍造型到lineandshaperenderer!!
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setShapesVisible(true); //series 点(即数据点)可见
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
10F, 6F
}, 0.0F)); //定义series为”First”的(即series1)点之间的连线 ,这里是虚线,默认是直线
lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
6F, 6F
}, 0.0F)); //定义series为”Second”的(即series2)点之间的连线
lineandshaperenderer.setSeriesStroke(2, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
2.0F, 6F
}, 0.0F)); //定义series为”Third”的(即series3)点之间的连线
return jfreechart;
}
一些重要的方法:
lineandshaperenderer.setLineVisible(true) //series 点(即数据点)间有连线可见
2、XYDataset 接口:
A、创建一个数据源(dataset):
private static XYDataset createDataset()
{
XYSeries xyseries = new XYSeries(”First”); //先产生XYSeries 对象
xyseries.add(1.0D, 1.0D);
xyseries.add(2D, 4D);
xyseries.add(3D, 3D);
xyseries.add(4D, 5D);
xyseries.add(5D, 5D);
xyseries.add(6D, 7D);
xyseries.add(7D, 7D);
xyseries.add(8D, 8D);
XYSeries xyseries1 = new XYSeries(”Second”);
xyseries1.add(1.0D, 5D);
xyseries1.add(2D, 7D);
xyseries1.add(3D, 6D);
xyseries1.add(4D, 8D);
xyseries1.add(5D, 4D);
xyseries1.add(6D, 4D);
xyseries1.add(7D, 2D);
xyseries1.add(8D, 1.0D);
XYSeries xyseries2 = new XYSeries(”Third”);
xyseries2.add(3D, 4D);
xyseries2.add(4D, 3D);
xyseries2.add(5D, 2D);
xyseries2.add(6D, 3D);
xyseries2.add(7D, 6D);
xyseries2.add(8D, 3D);
xyseries2.add(9D, 4D);
xyseries2.add(10D, 3D);
XYSeriesCollection xyseriescollection = new XYSeriesCollection(); //再用XYSeriesCollection添加入XYSeries 对象
xyseriescollection.addSeries(xyseries);
xyseriescollection.addSeries(xyseries1);
xyseriescollection.addSeries(xyseries2);
return xyseriescollection;
}
B、由ChartFactory 产生 JFreeChart 对象
private static JFreeChart createChart(XYDataset xydataset)
{
JFreeChart jfreechart = ChartFactory.createXYLineChart(”Line Chart Demo 2″,
“X”,
“Y”,
xydataset,
PlotOrientation.VERTICAL,
true,
true,
false);
jfreechart.setBackgroundPaint(Color.white);
XYPlot xyplot = (XYPlot)jfreechart.getPlot(); //获得 plot:XYPlot!!
xyplot.setBackgroundPaint(Color.lightGray); //设定图表数据显示部分背景色
xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); //设定坐标轴与图表数据显示部分距离
xyplot.setDomainGridlinePaint(Color.white); //网格线颜色
xyplot.setRangeGridlinePaint(Color.white);
//获得 renderer 注意这里是XYLineAndShapeRenderer !!
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
xylineandshaperenderer.setShapesVisible(true); //数据点可见
xylineandshaperenderer.setShapesFilled(true); //数据点被填充即不是空心点
NumberAxis numberaxis = (NumberAxis)xyplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
return jfreechart;
}
一些重要的方法:
XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer();
xylineandshaperenderer.setSeriesLinesVisible(0, false); //第一个XYSeries数据点间连线不可见
xylineandshaperenderer.setSeriesShapesVisible(1, false); //第二个XYSeries数据点不可见
xyplot.setRenderer(xylineandshaperenderer);