jfreechart 线图 柱图 饼图 散点图.docx
《jfreechart 线图 柱图 饼图 散点图.docx》由会员分享,可在线阅读,更多相关《jfreechart 线图 柱图 饼图 散点图.docx(42页珍藏版)》请在冰豆网上搜索。
jfreechart线图柱图饼图散点图
publicabstractclassChartActionextendsBaseAction{
publicJFreeChartchart;
privateString[]descriptionKeys;
privateString[]categoryKeys;
privateDouble[][]datas;
privateDefaultCategoryDatasetcddata;
privateDefaultPieDatasetdpdata;
privateXYDatasetxydata;
privateIntervalXYDatasetixydata;
privateStringtitle;//标题
privateStringx;//横坐标名称
privateStringy;//纵坐标名称
privateStringy2;//纵坐标名称2
/**
*线图
*/
publicvoidcreateLineChart(){
//设置数据
cddata=newDefaultCategoryDataset();
for(inti=0;iif(datas[i].length==0){
cddata.setValue(null,"",categoryKeys[i]);
}else{
for(intj=0;jcddata.setValue(datas[i][j],descriptionKeys[j],categoryKeys[i]);
}
}
}
/**数据
System.out.println("**************************datasbegin:
**************************");
for(inti=0;ifor(intj=0;jSystem.out.println(categoryKeys[i]+":
"+descriptionKeys[j]+"|"+datas[i][j]);
}
}
System.out.println("**************************datasend:
****************************");
*/
chart=ChartFactory.createLineChart(title,//图表标题
x,//目录轴的显示标签
y,//数值轴的显示标签
cddata,//数据集
PlotOrientation.VERTICAL,//图表方向:
水平、垂直
true,//是否显示图例(对于简单的柱状图必须是false)
true,//是否生成工具
false);//是否生成URL链接
CategoryPlotplot=chart.getCategoryPlot();
plot.setBackgroundPaint(newColor(238,244,255));//设置图表的颜色
plot.setDomainGridlinePaint(Color.lightGray);//设置垂直网格线的颜色
plot.setRangeGridlinePaint(Color.lightGray);//设置水平网格线的颜色
plot.setDomainGridlinesVisible(true);//设置垂直网格线是否显示
plot.setRangeGridlinesVisible(true);//设置水平网格线是否显示
LineAndShapeRendererlineandshaperenderer=(LineAndShapeRenderer)plot.getRenderer();
lineandshaperenderer.setBaseShapesVisible(true);
lineandshaperenderer.setBaseShapesFilled(true);
chart.setBackgroundPaint(Color.white);
setChartFont("");
}
/**
*饼图
*/
publicvoidcreatePieChart(){
dpdata=newDefaultPieDataset();
//生成JFreeChart对象
Doubled=0.0;
for(inti=0;ifor(intj=0;jd=d+(datas[j][i]==null?
0:
datas[j][i]);
}
dpdata.setValue(descriptionKeys[i],d.doubleValue());
}
chart=ChartFactory.createPieChart(title,dpdata,true,true,true);
PiePlotplot=(PiePlot)chart.getPlot();
plot.setBackgroundPaint(newColor(238,244,255));//设置图表的颜色
plot.setNoDataMessage("没有数据!
");
plot.setLabelGenerator(newStandardPieSectionLabelGenerator(
("{0}:
({2})"),
NumberFormat.getNumberInstance(),
newDecimalFormat("0.00%")));
chart.setBackgroundPaint(Color.white);
setChartFont("PieChart");
}
/**
*饼图
*/
publicvoidcreatePieChartValue(Double[]value){
dpdata=newDefaultPieDataset();
//生成JFreeChart对象
for(inti=0;idpdata.setValue(descriptionKeys[i],value[i]);
}
chart=ChartFactory.createPieChart(title,dpdata,true,true,true);
PiePlotplot=(PiePlot)chart.getPlot();
plot.setBackgroundPaint(newColor(238,244,255));//设置图表的颜色
plot.setNoDataMessage("没有数据!
");
plot.setLabelGenerator(null);
plot.setToolTipGenerator(newStandardPieToolTipGenerator(("{0}"),
NumberFormat.getNumberInstance(),
newDecimalFormat("0.00%")));
LegendTitlelegend=chart.getLegend();
legend.setBackgroundPaint(newColor(238,244,255));//图例背景
legend.setItemFont(newFont("宋体",Font.ITALIC,12));//图例的字体
legend.setPosition(RectangleEdge.RIGHT);//图例位置
legend.setHeight(1000.00);
chart.setBackgroundPaint(Color.white);
setChartFont("PieChart");
}
/**
*柱图
*/
publicvoidcreateBarChart(){
//设置数据
cddata=newDefaultCategoryDataset();
for(inti=0;iif(datas[i].length==0){
cddata.setValue(null,"",categoryKeys[i]);
}else{
for(intj=0;jcddata.setValue(datas[i][j],descriptionKeys[j],categoryKeys[i]);
}
}
}
chart=ChartFactory.createBarChart(title,//图表标题
x,//目录轴的显示标签
y,//数值轴的显示标签
cddata,//数据集
PlotOrientation.VERTICAL,//图表方向:
水平、垂直
true,//是否显示图例(对于简单的柱状图必须是false)
true,//是否生成工具
false//是否生成URL链接
);
CategoryPlotplot=chart.getCategoryPlot();
plot.setBackgroundPaint(newColor(238,244,255));//设置图表的颜色
chart.setBackgroundPaint(Color.white);
setChartFont("");
}
/**
*柱图
*/
publicvoidcreateBarChart3D(Object[]data){
//设置数据
cddata=newDefaultCategoryDataset();
for(inti=0;icddata.setValue((Number)data[i],categoryKeys[i],categoryKeys[i]);
}
chart=ChartFactory.createBarChart3D(title,//图表标题
x,//目录轴的显示标签
y,//数值轴的显示标签
cddata,//数据集
PlotOrientation.VERTICAL,//图表方向:
水平、垂直
true,//是否显示图例(对于简单的柱状图必须是false)
true,//是否生成工具
false//是否生成URL链接
);
CategoryPlotplot=chart.getCategoryPlot();
plot.setBackgroundPaint(newColor(238,244,255));//设置图表的颜色
chart.setBackgroundPaint(Color.white);
setChartFont("");
}
/**
*对比柱状图
*/
publicvoidcreateBarChart(CategoryDatasetdataset){
chart=ChartFactory.createBarChart(title,//图表标题
x,//目录轴的显示标签
y,//数值轴的显示标签
dataset,//数据集
PlotOrientation.VERTICAL,//图表方向:
水平、垂直
true,//是否显示图例(对于简单的柱状图必须是false)
true,//是否生成工具
false//是否生成URL链接
);
CategoryPlotplot=chart.getCategoryPlot();
plot.setBackgroundPaint(newColor(238,244,255));//设置图表的颜色
chart.setBackgroundPaint(Color.white);
setChartFont("");
}
/**
*面积图
*/
publicvoidcreateAreaChart(){
//设置数据
cddata=newDefaultCategoryDataset();
cddata=newDefaultCategoryDataset();
for(inti=0;iif(datas[i].length==0){
cddata.setValue(null,"",categoryKeys[i]);
}else{
for(intj=0;jcddata.setValue(datas[i][j],descriptionKeys[j],categoryKeys[i]);
}
}
}
chart=ChartFactory.createAreaChart(title,//图表标题
x,//目录轴的显示标签
y,//数值轴的显示标签
cddata,//数据集
PlotOrientation.VERTICAL,//图表方向:
水平、垂直
true,//是否显示图例(对于简单的柱状图必须是false)
true,//是否生成工具
false//是否生成URL链接
);
CategoryPlotplot=chart.getCategoryPlot();
plot.setBackgroundPaint(newColor(238,244,255));//设置图表的颜色
chart.setBackgroundPaint(Color.white);
setChartFont("");
}
/**
*X,Y线图chart
*@paramchartMap
*/
publicvoidcreateXYLineChart(Map>chartMap){
//创建XYDataset对象(准备数据)
xydata=createXYDataset(chartMap);
//创建JFreeChart对象:
ChartFactory.createXYLineChart
chart=ChartFactory.createXYLineChart(title,//标题
x,//categoryAxisLabel(category轴,横轴,X轴标签)
y,//valueAxisLabel(value轴,纵轴,Y轴的标签)
xydata,//dataset
PlotOrientation.VERTICAL,
true,//legend
true,//tooltips
true);//URLs
//使用CategoryPlot设置各种参数。
以下设置可以省略。
XYPlotplot=(XYPlot)chart.getPlot();
//x轴整数显示
NumberAxisna=(NumberAxis)plot.getDomainAxis();
na.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
XYLineAndShapeRendererrenderer=(XYLineAndShapeRenderer)plot.getRenderer();
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
plot.setRenderer(0,renderer);
plot.setBackgroundPaint(newColor(238,244,255));//设置图表的颜色
plot.setDomainGridlinePaint(Color.lightGray);//设置垂直网格线的颜色
plot.setRangeGridlinePaint(Color.lightGray);//设置水平网格线的颜色
plot.setDomainGridlinesVisible(true);//设置垂直网格线是否显示
plot.setRangeGridlinesVisible(true);//设置水平网格线是否显示
chart.setBackgroundPaint(Color.white);
setChartFont("XYChart");
}
/**
*双y轴曲线图,没有折线点
*@paramcddata
*/
publicvoidcreateXYsLineChart(DefaultCategoryDatasetcddataLeft,DefaultCategoryDatasetcddataRight){
//设置字体样式
Fontfs=newFont("微软雅黑",Font.BOLD,12);
Fontf=newFont("微软雅黑",Font.PLAIN,12);
//创建JFreeChart对象:
ChartFactory.createXYLineChart
chart=ChartFactory.createLineChart(title,//标题
x,//categoryAxisLabel(category轴,横轴,X轴标签)
y,//valueAxisLabel(value轴,纵轴,Y轴的标签)
cddataLeft,//dataset
PlotOrientation.VERTICAL,
true,//legend
true,//tooltips
true);//URLs
//使用CategoryPlot设置各种参数。
以下设置可以省略。
CategoryPlotplot=(CategoryPlot)chart.getPlot();
//设置X坐标的显示方式(默认水平)
CategoryAxiscategoryaxis=plot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
categoryaxis.setCategoryMargin(9);//分类轴边距
//左边y轴显示方式
//NumberAxisnumberaxis=(NumberAxis)plotxy.getRangeAxis();//y轴整数显示
NumberAxisnumberaxis=newNumberAxis(this.y);
numberaxis.setLabelFont(fs);
numberaxis.setTickLabelFont(f);
plot.setRangeAxis(0,numberaxis);
//右边y轴显示方式
NumberAxisnumberaxis2=newNumberAxis(this.y2);
numberaxis2.setLabelFont(fs);
numberaxis2.setTickLabelFont(f);
plot.setRangeAxis(1,numberaxis2);
plot.setDataset(1,cddataRight);
plot.mapDatasetToRangeAxis(1,1);
//y轴(左边)
LineAndShapeRendererrenderer=(LineAndShapeRenderer)plot.getRenderer();
renderer.setBaseItemLabelsVisible(true);
plot.setRenderer(0,renderer);
renderer.setSeriesStroke(0,newBasicStroke(1.6F));
//y轴(右边),绘制单元对象LineAndShapeRenderer