您的位置:首页 > 其它

JfreeChart(3)-------BarChart的简单使用

2014-10-27 17:26 120 查看
今天项目中用到了BarChart,其中的中文问题还是花了我比较多的时间的,所以就把这段代码记录下来,方便以后的查阅!感觉这个知识还真的是越用越活的,越
用越熟的,用少了过段时间就忘了,用多了的话很长时间都记得!

 

private byte[] createChart(List<Object[]> data, String title, String xtitle, String ytitle) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i=0;i<data.size();i++) {
Object[] objs = data.get(i);
//由上知objs里面只含有两个元素,所以可以直接对它们进行取值;
String name = (String)objs[0];
long sumNumber = (Long)objs[1];
dataset.addValue(sumNumber, name, name);
}
JFreeChart chart = ChartFactory.createBarChart3D(title, xtitle, ytitle, dataset, PlotOrientation.VERTICAL, true, true, false);
//设置标题的字体,不设置中文会乱码
chart.setTitle(new TextTitle(title,new Font("宋体",Font.BOLD,22)));

CategoryPlot plot = (CategoryPlot) chart.getPlot();
Font font = new Font("宋体",Font.BOLD,18);
Font font2 = new Font("宋体", Font.PLAIN,16);
plot.getRangeAxis().setLabelFont(font);//设置纵轴标签的字体,不设置中文会乱码
plot.getDomainAxis().setLabelFont(font);//设置横轴的标题的字体
plot.getDomainAxis().setTickLabelFont(font2);//设置X轴坐标上的字体
chart.getLegend(0).setItemFont(font2);
//		File file = Util.getChartFile4Save(prefix);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
//			ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
ChartUtilities.writeChartAsJPEG(out, chart, 800, 600);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return out.toByteArray();
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: