读书人

JfreeChart(三)-BarChart的简单使用

发布时间: 2012-10-25 10:58:57 作者: rapoo

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

今天项目中用到了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 blocke.printStackTrace();}return out.toByteArray();}

?

读书人网 >编程

热点推荐