jfreechart 生成折线图饼图柱状图堆栈柱状图.docx
《jfreechart 生成折线图饼图柱状图堆栈柱状图.docx》由会员分享,可在线阅读,更多相关《jfreechart 生成折线图饼图柱状图堆栈柱状图.docx(27页珍藏版)》请在冰点文库上搜索。
jfreechart生成折线图饼图柱状图堆栈柱状图
使用jfreechart来生成统计图,使用了折线图,饼图等
1.所需包
(1)jfreechart-1.0.8a.jar
(2)jcommon-1.0.12.jar
2.运行环境
JDK1.5
3.源代码
importjava.awt.Color;
importjava.awt.Font;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.text.DecimalFormat;
importjava.text.NumberFormat;
importorg.jfree.chart.ChartFactory;
importorg.jfree.chart.ChartUtilities;
importorg.jfree.chart.JFreeChart;
importorg.jfree.chart.axis.CategoryAxis;
importorg.jfree.chart.axis.CategoryLabelPositions;
importorg.jfree.chart.axis.NumberAxis;
importorg.jfree.chart.axis.ValueAxis;
importorg.jfree.chart.labels.StandardCategoryItemLabelGenerator;
importorg.jfree.chart.labels.StandardPieSectionLabelGenerator;
importorg.jfree.chart.plot.CategoryPlot;
importorg.jfree.chart.plot.PiePlot3D;
importorg.jfree.chart.plot.PlotOrientation;
importorg.jfree.chart.renderer.category.BarRenderer;
importorg.jfree.chart.renderer.category.LineAndShapeRenderer;
importorg.jfree.chart.renderer.category.StackedBarRenderer;
importorg.jfree.chart.title.TextTitle;
importorg.jfree.data.category.CategoryDataset;
importorg.jfree.data.general.DatasetUtilities;
importorg.jfree.data.general.DefaultPieDataset;
importorg.jfree.data.general.PieDataset;
/**
*实际取色的时候一定要16位的,这样比较准确
*
*@authornew
*/
publicclassCreateChartServiceImpl
{
privatestaticfinalStringCHART_PATH="E:
/test/";
publicstaticvoidmain(String[]args)
{
//TODOAuto-generatedmethodstub
CreateChartServiceImplpm=newCreateChartServiceImpl();
//生成饼状图
pm.makePieChart();
//生成单组柱状图
pm.makeBarChart();
//生成多组柱状图
pm.makeBarGroupChart();
//生成堆积柱状图
pm.makeStackedBarChart();
//生成折线图
pm.makeLineAndShapeChart();
}
/**
*生成折线图
*/
publicvoidmakeLineAndShapeChart()
{
double[][]data=newdouble[][]
{
{672,766,223,540,126},
{325,521,210,340,106},
{332,256,523,240,526}};
String[]rowKeys=
{"苹果","梨子","葡萄"};
String[]columnKeys=
{"北京","上海","广州","成都","深圳"};
CategoryDatasetdataset=getBarData(data,rowKeys,columnKeys);
createTimeXYChar("折线图","x轴","y轴",dataset,"lineAndShap.png");
}
/**
*生成分组的柱状图
*/
publicvoidmakeBarGroupChart()
{
double[][]data=newdouble[][]
{
{672,766,223,540,126},
{325,521,210,340,106},
{332,256,523,240,526}};
String[]rowKeys=
{"苹果","梨子","葡萄"};
String[]columnKeys=
{"北京","上海","广州","成都","深圳"};
CategoryDatasetdataset=getBarData(data,rowKeys,columnKeys);
createBarChart(dataset,"x坐标","y坐标","柱状图","barGroup.png");
}
/**
*生成柱状图
*/
publicvoidmakeBarChart()
{
double[][]data=newdouble[][]
{
{672,766,223,540,126}};
String[]rowKeys=
{"苹果"};
String[]columnKeys=
{"北京","上海","广州","成都","深圳"};
CategoryDatasetdataset=getBarData(data,rowKeys,columnKeys);
createBarChart(dataset,"x坐标","y坐标","柱状图","bar.png");
}
/**
*生成堆栈柱状图
*/
publicvoidmakeStackedBarChart()
{
double[][]data=newdouble[][]
{
{0.21,0.66,0.23,0.40,0.26},
{0.25,0.21,0.10,0.40,0.16}};
String[]rowKeys=
{"苹果","梨子"};
String[]columnKeys=
{"北京","上海","广州","成都","深圳"};
CategoryDatasetdataset=getBarData(data,rowKeys,columnKeys);
createStackedBarChart(dataset,"x坐标","y坐标","柱状图","stsckedBar.png");
}
/**
*生成饼状图
*/
publicvoidmakePieChart()
{
double[]data=
{9,91};
String[]keys=
{"失败率","成功率"};
createValidityComparePimChar(getDataPieSetByUtil(data,keys),"饼状图",
"pie2.png",keys);
}
//柱状图,折线图数据集
publicCategoryDatasetgetBarData(double[][]data,String[]rowKeys,
String[]columnKeys)
{
returnDatasetUtilities
.createCategoryDataset(rowKeys,columnKeys,data);
}
//饼状图数据集
publicPieDatasetgetDataPieSetByUtil(double[]data,
String[]datadescription)
{
if(data!
=null&&datadescription!
=null)
{
if(data.length==datadescription.length)
{
DefaultPieDatasetdataset=newDefaultPieDataset();
for(inti=0;i{
dataset.setValue(datadescription[i],data[i]);
}
returndataset;
}
}
returnnull;
}
/**
*柱状图
*
*@paramdataset数据集
*@paramxNamex轴的说明(如种类,时间等)
*@paramyNamey轴的说明(如速度,时间等)
*@paramchartTitle图标题
*@paramcharName生成图片的名字
*@return
*/
publicStringcreateBarChart(CategoryDatasetdataset,StringxName,
StringyName,StringchartTitle,StringcharName)
{
JFreeChartchart=ChartFactory.createBarChart(chartTitle,//图表标题
xName,//目录轴的显示标签
yName,//数值轴的显示标签
dataset,//数据集
PlotOrientation.VERTICAL,//图表方向:
水平、垂直
true,//是否显示图例(对于简单的柱状图必须是false)
false,//是否生成工具
false//是否生成URL链接
);
FontlabelFont=newFont("SansSerif",Font.TRUETYPE_FONT,12);
/*
*VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭,
*使用的关闭抗锯齿后,字体尽量选择12到14号的宋体字,这样文字最清晰好看
*/
//chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
chart.setTextAntiAlias(false);
chart.setBackgroundPaint(Color.white);
//createplot
CategoryPlotplot=chart.getCategoryPlot();
//设置横虚线可见
plot.setRangeGridlinesVisible(true);
//虚线色彩
plot.setRangeGridlinePaint(Color.gray);
//数据轴精度
NumberAxisvn=(NumberAxis)plot.getRangeAxis();
//vn.setAutoRangeIncludesZero(true);
DecimalFormatdf=newDecimalFormat("#0.00");
vn.setNumberFormatOverride(df);//数据轴数据标签的显示格式
//x轴设置
CategoryAxisdomainAxis=plot.getDomainAxis();
domainAxis.setLabelFont(labelFont);//轴标题
domainAxis.setTickLabelFont(labelFont);//轴数值
//Lable(Math.PI/3.0)度倾斜
//domainAxis.setCategoryLabelPositions(CategoryLabelPositions
//.createUpRotationLabelPositions(Math.PI/3.0));
domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);//横轴上的Lable是否完整显示
//设置距离图片左端距离
domainAxis.setLowerMargin(0.1);
//设置距离图片右端距离
domainAxis.setUpperMargin(0.1);
//设置columnKey是否间隔显示
//domainAxis.setSkipCategoryLabelsToFit(true);
plot.setDomainAxis(domainAxis);
//设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
plot.setBackgroundPaint(newColor(255,255,204));
//y轴设置
ValueAxisrangeAxis=plot.getRangeAxis();
rangeAxis.setLabelFont(labelFont);
rangeAxis.setTickLabelFont(labelFont);
//设置最高的一个Item与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
//设置最低的一个Item与图片底端的距离
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
BarRendererrenderer=newBarRenderer();
//设置柱子宽度
renderer.setMaximumBarWidth(0.05);
//设置柱子高度
renderer.setMinimumBarLength(0.2);
//设置柱子边框颜色
renderer.setBaseOutlinePaint(Color.BLACK);
//设置柱子边框可见
renderer.setDrawBarOutline(true);
////设置柱的颜色
renderer.setSeriesPaint(0,newColor(204,255,255));
renderer.setSeriesPaint(1,newColor(153,204,255));
renderer.setSeriesPaint(2,newColor(51,204,204));
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.0);
//显示每个柱的数值,并修改该数值的字体属性
renderer.setIncludeBaseInRange(true);
renderer
.setBaseItemLabelGenerator(newStandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
plot.setRenderer(renderer);
//设置柱的透明度
plot.setForegroundAlpha(1.0f);
FileOutputStreamfos_jpg=null;
try
{
isChartPathExist(CHART_PATH);
StringchartName=CHART_PATH+charName;
fos_jpg=newFileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg,chart,500,500,true,10);
returnchartName;
}
catch(Exceptione)
{
e.printStackTrace();
returnnull;
}
finally
{
try
{
fos_jpg.close();
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
/**
*横向图
*
*@paramdataset数据集
*@paramxNamex轴的说明(如种类,时间等)
*@paramyNamey轴的说明(如速度,时间等)
*@paramchartTitle图标题
*@paramcharName生成图片的名字
*@return
*/
publicStringcreateHorizontalBarChart(CategoryDatasetdataset,
StringxName,StringyName,StringchartTitle,StringcharName)
{
JFreeChartchart=ChartFactory.createBarChart(chartTitle,//图表标题
xName,//目录轴的显示标签
yName,//数值轴的显示标签
dataset,//数据集
PlotOrientation.VERTICAL,//图表方向:
水平、垂直
true,//是否显示图例(对于简单的柱状图必须是false)
false,//是否生成工具
false//是否生成URL链接
);
CategoryPlotplot=chart.getCategoryPlot();
//数据轴精度
NumberAxisvn=(NumberAxis)plot.getRangeAxis();
//设置刻度必须从0开始
//vn.setAutoRangeIncludesZero(true);
DecimalFormatdf=newDecimalFormat("#0.00");
vn.setNumberFormatOverride(df);//数据轴数据标签的显示格式
CategoryAxisdomainAxis=plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//横轴上的
//Lable
FontlabelFont=newFont("SansSerif",Font.TRUETYPE_FONT,12);
domainAxis.setLabelFont(labelFont);//轴标题
domainAxis.setTickLabelFont(labelFont);//轴数值
domainAxis.setMaximumCategoryLabelWidthRatio(0.8f);//横轴上的Lable是否完整显示
//domainAxis.setVerticalCategoryLabels(false);
plot.setDomainAxis(domainAxis);
ValueAxisrangeAxis=plot.getRangeAxis();
//设置最高的一个Item与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
//设置最低的一个Item与图片底端的距离
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
BarRendererrenderer=newBarRenderer();
//设置柱子宽度
renderer.setMaximumBarWidth(0.03);
//设置柱子高度
renderer.setMinimumBarLength(30);
renderer.setBaseOutlinePaint(Color.BLACK);
//设置柱的颜色
renderer.setSeriesPaint(0,Color.GREEN);
renderer.setSeriesPaint(1,newColor(0,0,255));
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.5);
//显示每个柱的数值,并修改该数值的字体属性
renderer
.setBaseItemLabelGenerator(newStandardCategoryItemLabelGenerator());
//设置柱的数值可见
renderer.setBaseItemLabelsVisible(true);
plot.setRenderer(renderer);
//设置柱的透明度
plot.setForegroundAlpha(0.6f);
FileOutputStreamfos_jpg=null;
try
{
isChartPat