`
javeye
  • 浏览: 329315 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JFreeChart修改饼状图的方法

阅读更多
最近改了一下原来做的一个web项目,项目中用到了JFreeChart,要求修改用JFreeChart绘制饼图的颜色,希望能够做到一个key对应一个固定的颜色。
比如,有一个问题,选择ABCD四个答案的比例分别是:10%,30%,20%,40%。希望饼图中A的颜色是red,B的颜色是green,C的颜色是yellow,D的颜色是blue。
可以这么做:
1.准备好dataset,这一步就不讲了,pieChart有其要求的dataset格式。
2.书写一个创建pieChart的方法:
public static JFreeChart createPieChart(DefaultPieDataset dataset, String mytitle) {
			JFreeChart chart = ChartFactory.createPieChart(mytitle,dataset,true, true, false);
			PiePlot plot = (PiePlot) chart.getPlot();
			plot.setSectionOutlinesVisible(false);
			plot.setNoDataMessage("没有可供使用的数据!");
			plot.setSectionPaint("A", Color.RED);
			plot.setSectionPaint("B", Color.GREEN);
			plot.setSectionPaint("C", Color.YELLOW);
			plot.setSectionPaint("D", Color.BLUE);
			//就是这个地方,实现了对各个key对应饼图区域的颜色设置

//	        PiePlot plot = (PiePlot) chart.getPlot();
	        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
	        plot.setCircular(false);
	        plot.setLabelGap(0.02);
//do more things here
	        return chart;
}

注意到plot.setSectionPaint("A", Color.RED);这个方法了吧,该方法就是设置颜色的关键语句。如果没有此方法,则JFreeChart在其创建过程中,会调用默认的颜色来给各个区域进行颜色设置。
不知道说清楚了没有……
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics