Java使用jfreechart画饼图_JFreeChart 使用一 饼图之简单实例

public classPieChartExample

{/*** 创建PieChart

*

*@author马建新(mervin)

*@return生成Chart图片的地址*/

publicString createPieChart()

{

String filePath= "D:\\testJfreeChart.jpg";try{

DefaultPieDataset dataset=getPieDataSet();//JFreeChart主要由三个部分构成:title(标题),legend(图释),plot(图表主体)。

JFreeChart chart = ChartFactory.createPieChart("测试PieChart",

dataset,true, true, false);//设置Chart属性

setChartProperties(chart);

ChartUtilities.saveChartAsJPEG(new File(filePath), chart, 400, 300);

}catch(IOException e)

{

e.printStackTrace();

}returnfilePath;

}/*** 设置Chart属性,可解决乱码问题

*@paramchart 统计图标*/

private voidsetChartProperties(JFreeChart chart){//三个部分设置字体的方法分别如下:

TextTitle textTitle =chart.getTitle();

textTitle.setFont(new Font("宋体", Font.BOLD, 20));

LegendTitle legend=chart.getLegend();if (legend != null) {

legend.setItemFont(new Font("宋体", Font.BOLD, 20));

}

PiePlot pie=(PiePlot) chart.getPlot();

pie.setLabelFont(new Font("宋体", Font.BOLD, 12));

pie.setNoDataMessage("No data available");//设置PieChart是否显示为圆形

pie.setCircular(true);//间距

pie.setLabelGap(0.01D);

}/*** 获得PieChart的数据集

*

*@returnpieChart数据集*/

privateDefaultPieDataset getPieDataSet()

{

DefaultPieDataset dataset= newDefaultPieDataset();

dataset.setValue("香蕉", 56.4);

dataset.setValue("苹果", 63.5);

dataset.setValue("橘子", 58.4);

dataset.setValue("西瓜", 76.3);returndataset;

}/***@paramargs*/

public static voidmain(String[] args)

{

PieChartExample piechart= newPieChartExample();

String filePath=piechart.createPieChart();

System.out.println("PieChart file path : " +filePath);try{

Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler " +filePath);

}catch(IOException e)

{

e.printStackTrace();

}

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值