jfreechart入门经典.docx
《jfreechart入门经典.docx》由会员分享,可在线阅读,更多相关《jfreechart入门经典.docx(19页珍藏版)》请在冰豆网上搜索。
![jfreechart入门经典.docx](https://file1.bdocx.com/fileroot1/2023-5/20/5637cf9e-e43d-423c-b9c2-a28933eb2743/5637cf9e-e43d-423c-b9c2-a28933eb27431.gif)
jfreechart入门经典
一、jFreeChart产生图形的流程
创建一个数据源(dataset)来包含将要在图形中显示的数据>>创建一个JFreeChart对象来代表要显示的图形
>>把图形输出
重要的类和接口:
org.jfree.data.general.Dataset所有数据源类都要实现的接口
org.jfree.chart.ChartFactory由它来产生JFreeChart对象
org.jfree.chart.JFreeChart所有对图形的调整都是通过它噢!
!
org.jfree.chart.plot.Plot通过JFreeChart对象获得它,然后再通过它对图形外部部分(例:
坐标轴)调整
注意:
它有很多子类,一般都下嗍造型到它的子类!
org.jfree.chart.renderer.AbstractRenderer通过JFreeChart对象获得它,然后再通过它对图形内部部分
(例:
折线的类型)调整。
同样,针对不同类型的报表图,它有
着不同的子类实现!
在下面我们简称它为Renderer
下面我们结合不同类型的图形来具体分析这个流程。
二、饼图
饼图的dataset一般是用PieDataset接口,具体实现类是DefaultPieDataset
1、创建一个数据源(dataset):
privatestaticPieDatasetcreateDataset()
{
DefaultPieDatasetdefaultpiedataset=newDefaultPieDataset();//注意是DefaultPieDataset!
!
defaultpiedataset.setValue(”One”,newDouble(43.200000000000003D));
defaultpiedataset.setValue(”Two”,newDouble(10D));
defaultpiedataset.setValue(”Three”,newDouble(27.5D));
defaultpiedataset.setValue(”Four”,newDouble(17.5D));
returndefaultpiedataset;
}
2、由ChartFactory产生JFreeChart对象
privatestaticJFreeChartcreateChart(PieDatasetpiedataset)
{
JFreeChartjfreechart=ChartFactory.createPieChart(”PieChartDemo1″,//图形标题名称
piedataset,//dataset
true,//legend?
true,//tooltips?
false);//URLs?
PiePlotpieplot=(PiePlot)jfreechart.getPlot();//通过JFreeChart对象获得plot:
PiePlot!
!
pieplot.setNoDataMessage(”Nodataavailable”);//没有数据的时候显示的内容
returnjfreechart;
}
一些重要的方法:
pieplot.setExplodePercent(0,0.3D)//把Lable为”One”的那一块”挖”出来30%
3、输出略
三、柱状图
柱状图的dataset一般是用CatagoryDataset接口(具体实现类是DefaultCategoryDataset),也会用IntervalXYDataset
接口
1、创建一个数据源(dataset):
privatestaticCategoryDatasetcreateDataset()
{
Stringseries1=“First”;
Stringseries2=“Second”;
Stringseries3=“Third”;
Stringcategory1=“Category1″;
Stringcategory2=“Category2″;
Stringcategory3=“Category3″;
Stringcategory4=“Category4″;
Stringcategory5=“Category5″;
DefaultCategoryDatasetdefaultcategorydataset=newDefaultCategoryDataset();
defaultcategorydataset.addValue(1.0D,series1,category1);
defaultcategorydataset.addValue(4D,series1,category2);
defaultcategorydataset.addValue(3D,series1,category3);
defaultcategorydataset.addValue(5D,series1,category4);
defaultcategorydataset.addValue(5D,series1,category5);
defaultcategorydataset.addValue(5D,series2,category1);
defaultcategorydataset.addValue(7D,series2,category2);
defaultcategorydataset.addValue(6D,series2,category3);
defaultcategorydataset.addValue(8D,series2,category4);
defaultcategorydataset.addValue(4D,series2,category5);
defaultcategorydataset.addValue(4D,series3,category1);
defaultcategorydataset.addValue(3D,series3,category2);
defaultcategorydataset.addValue(2D,series3,category3);
defaultcategorydataset.addValue(3D,series3,category4);
defaultcategorydataset.addValue(6D,series3,category5);
returndefaultcategorydataset;
}
2、由ChartFactory产生JFreeChart对象
privatestaticJFreeChartcreateChart(CategoryDatasetcategorydataset)
{
JFreeChartjfreechart=ChartFactory.createBarChart(”BarChartDemo”,//图形标题名称
“Category”,//domain轴Lable
这里先简单理解为横坐标Lable好了
“Value”,//range轴Lable
这里也先简单理解为纵坐标Lable好了
categorydataset,//dataset
PlotOrientation.VERTICAL,//垂直显示
true,//legend?
true,//tooltips?
false);//URLs?
jfreechart.setBackgroundPaint(Color.white);//设定背景色为白色
CategoryPlotcategoryplot=jfreechart.getCategoryPlot();//获得plot:
CategoryPlot!
!
categoryplot.setBackgroundPaint(Color.lightGray);//设定图表数据显示部分背景色
categoryplot.setDomainGridlinePaint(Color.white);//横坐标网格线白色
categoryplot.setDomainGridlinesVisible(true);//可见
categoryplot.setRangeGridlinePaint(Color.white);//纵坐标网格线白色
//下面两行使纵坐标的最小单位格为整数
NumberAxisnumberaxis=(NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRendererbarrenderer=(BarRenderer)categoryplot.getRenderer();//获得renderer注意这里是下嗍造型
到BarRenderer!
!
barrenderer.setDrawBarOutline(false);//Bar的外轮廓线不画
GradientPaintgradientpaint=newGradientPaint(0.0F,0.0F,Color.blue,
0.0F,0.0F,newColor(0,0,64));//设定特定颜色
GradientPaintgradientpaint1=newGradientPaint(0.0F,0.0F,Color.green,
0.0F,0.0F,newColor(0,64,0));
GradientPaintgradientpaint2=newGradientPaint(0.0F,0.0F,Color.red,
0.0F,0.0F,newColor(64,0,0));
barrenderer.setSeriesPaint(0,gradientpaint);//给series1Bar设定上面定义的颜色
barrenderer.setSeriesPaint(1,gradientpaint1);//给series2Bar设定上面定义的颜色
barrenderer.setSeriesPaint(2,gradientpaint2);//给series3Bar设定上面定义的颜色
CategoryAxiscategoryaxis=categoryplot.getDomainAxis();//横轴上的Lable45度倾斜
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
returnjfreechart;
}
一些重要的方法:
(增加一块标记)
IntervalMarkerintervalmarker=newIntervalMarker(4.5D,7.5D);
intervalmarker.setLabel(”TargetRange”);
intervalmarker.setLabelFont(newFont(”SansSerif”,2,11));
intervalmarker.setLabelAnchor(RectangleAnchor.LEFT);
intervalmarker.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
intervalmarker.setPaint(newColor(222,222,255,128));
categoryplot.addRangeMarker(intervalmarker,Layer.BACKGROUND);
四、折线图
折线图的dataset两种CatagoryDataset接口(具体实现类是DefaultCategoryDataset),XYDataset接口
1、CatagoryDataset接口:
A、创建一个数据源(dataset):
privatestaticCategoryDatasetcreateDataset()
{
Stringseries1=“First”;
Stringseries2=“Second”;
Stringseries3=“Third”;
Stringtype1=“Type1″;
Stringtype2=“Type2″;
Stringtype3=“Type3″;
Stringtype4=“Type4″;
Stringtype5=“Type5″;
Stringtype6=“Type6″;
Stringtype7=“Type7″;
Stringtype8=“Type8″;
DefaultCategoryDatasetdefaultcategorydataset=newDefaultCategoryDataset();
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);
returndefaultcategorydataset;
}
B、由ChartFactory产生JFreeChart对象(与上面重复的部分就不再注释)
privatestaticJFreeChartcreateChart(CategoryDatasetcategorydataset)
{
JFreeChartjfreechart=ChartFactory.createLineChart(”LineChartDemo1″,
“Type”,
“Value”,
categorydataset,
PlotOrientation.VERTICAL,
true,
true,
false);
jfreechart.setBackgroundPaint(Color.white);
CategoryPlotcategoryplot=(CategoryPlot)jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.white);
NumberAxisnumberaxis=(NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
//获得renderer注意这里是下嗍造型到lineandshaperenderer!
!
LineAndShapeRendererlineandshaperenderer=(LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setShapesVisible(true);//series点(即数据点)可见
lineandshaperenderer.setSeriesStroke(0,newBasicStroke(2.0F,1,1,1.0F,newfloat[]{
10F,6F
},0.0F));//定义series为”First”的(即series1)点之间的连线,这里是虚线,默认是直线
lineandshaperenderer.setSeriesStroke(1,newBasicStroke(2.0F,1,1,1.0F,newfloat[]{
6F,6F
},0.0F));//定义series为”Second”的(即series2)点之间的连线
lineandshaperenderer.setSeriesStroke(2,newBasicStroke(2.0F,1,1,1.0F,newfloat[]{
2.0F,6F
},0.0F));//定义series为”Third”的(即series3)点之间的连线
returnjfreechart;
}
一些重要的方法:
lineandshaperenderer.setLineVisible(true)//series点(即数据点)间有连线可见
2、XYDataset接口:
A、创建一个数据源(dataset):
privatestaticXYDatasetcreateDataset()
{
XYSeriesxyseries=newXYSeries(”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);
XYSeriesxyseries1=newXYSeries(”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);
XYSeriesxyseries2=newXYSeries(”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);
XYSeriesCollectionxyseriescollection=newXYSeriesCollection();//再用XYSeriesCollection添加入XYSeries对象
xyseriescollection.addSeries(xyseries);
xyseriescollection.addSeries(xyseries1);
xyseriescollection.addSeries(xyseries2);
returnxyseriescollection;
}
B、由ChartFactory产生JFreeChart对象
privatestaticJFreeChartcreateChart(XYDatasetxydataset)
{
JFreeChartjfreechart=ChartFactory.createXYLineChart(”LineChartDemo2″,
“X”,
“Y”,
xydataset,
PlotOrientation.VERTICAL,
true,
true,
false);
jfreechart.setBackgroundPaint(Color.white);
XYPlotxyplot=(XYPlot)jfreechart.getPlot();//获得plot:
XYPlot!
!
xyplot.setBackgroundPaint(Color.lightGray);//设定图表数据显示部分背景色
xyplot.setAxisOffset(newRe