这几天由于客观因素(天冷和停电)和主观因素(项目吃紧,每天都加班到9点,回来也就不想搞了)。就这样耽搁了好几天的时间了!不过偶然的机会在网上换到了一篇整理好的API,所以整理常用的柱状图就容易多了。里面的封装方法太多了,一般是很难记住的.很多都要实践了才知道其作用,这里就直接上代码了。也好之后备用巩固学习。
package com.huawei.jfreechart;
- importjava.awt.Color;
- importjava.awt.Font;
- importorg.jfree.chart.ChartFactory;
- importorg.jfree.chart.ChartFrame;
- importorg.jfree.chart.JFreeChart;
- importorg.jfree.chart.axis.CategoryAxis;
- importorg.jfree.chart.axis.CategoryLabelPositions;
- importorg.jfree.chart.axis.NumberAxis;
- importorg.jfree.chart.axis.NumberTickUnit;
- importorg.jfree.chart.plot.CategoryPlot;
- importorg.jfree.chart.plot.PlotOrientation;
- importorg.jfree.chart.title.TextTitle;
- importorg.jfree.data.category.CategoryDataset;
- importorg.jfree.data.category.DefaultCategoryDataset;
- /**
- *@name何枫
- *@date2010-12-17
- *@actioncreateBarChart3DTest.java
- *@time下午10:35:52
- *@package_namecom.huawei.jfreechart
- *@project_namejfreechartTest
- */
- publicclasscreateBarChart3DTest{
- privatestaticCategoryDatasetgetDataSet(){
- DefaultCategoryDatasetdataset=newDefaultCategoryDataset();
- dataset.addValue(200,"计划","清华大学");
- dataset.addValue(400,"实报","清华大学");
- dataset.addValue(360,"计划","天津大学");
- dataset.addValue(520,"实报","天津大学");
- dataset.addValue(100,"计划","北京大学");
- dataset.addValue(150,"实报","北京大学");
- dataset.addValue(280,"计划","复旦大学");
- dataset.addValue(300,"实报","复旦大学");
- returndataset;
- }
- publicstaticvoidmain(String[]args){
- CategoryDatasetdataset=getDataSet();
- JFreeChartchart=ChartFactory.createBarChart3D("水果产量图","水果产品","销售数量",dataset,PlotOrientation.VERTICAL,true,true,true);
- ChartFrameframe=newChartFrame("水果产量图",chart,true);
- //自定义设定背景色
- //chart.setBackgroundPaint(Color.getHSBColor(23,192,223));
- chart.setBackgroundPaint(Color.WHITE);
- //获得plot:3dBar为CategoryPlot
- CategoryPlotcategoryPlot=chart.getCategoryPlot();
- //设定图表数据显示部分背景色
- categoryPlot.setBackgroundPaint(Color.BLACK);
- //横坐标网格线
- categoryPlot.setDomainGridlinePaint(Color.RED);
- //设置网格线可见
- categoryPlot.setDomainGridlinesVisible(true);
- //纵坐标网格线
- categoryPlot.setRangeGridlinePaint(Color.RED);
- //重要的类,负责生成各种效果
- //BarRenderer3Drenderer=(BarRenderer3D)categoryPlot.getRenderer();
- //获取纵坐标
- NumberAxisnumberaxis=(NumberAxis)categoryPlot.getRangeAxis();
- //设置纵坐标的标题字体和大小
- numberaxis.setLabelFont(newFont("黑体",Font.CENTER_BASELINE,24));
- //设置丛坐标的坐标值的字体颜色
- numberaxis.setLabelPaint(Color.BLACK);
- //设置丛坐标的坐标轴标尺颜色
- numberaxis.setTickLabelPaint(Color.RED);
- //坐标轴标尺颜色
- numberaxis.setTickMarkPaint(Color.BLUE);
- //丛坐标的默认间距值
- //numberaxis.setAutoTickUnitSelection(true);
- //设置丛坐标间距值
- numberaxis.setAutoTickUnitSelection(false);
- numberaxis.setTickUnit(newNumberTickUnit(150));
- //获取横坐标
- CategoryAxisdomainAxis=categoryPlot.getDomainAxis();
- //设置横坐标的标题字体和大小
- domainAxis.setLabelFont(newFont("宋体",Font.PLAIN,13));
- //设置横坐标的坐标值的字体颜色
- domainAxis.setTickLabelPaint(Color.RED);
- //设置横坐标的坐标值的字体
- domainAxis.setTickLabelFont(newFont("宋体",Font.PLAIN,30));
- //设置横坐标的显示
- domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.4));
- //这句代码解决了底部汉字乱码的问题
- chart.getLegend().setItemFont(newFont("黑体",0,16));
- //设置图例标题
- Fontfont=newjava.awt.Font("黑体",java.awt.Font.CENTER_BASELINE,50);
- TextTitletitle=newTextTitle("项目状态分布");
- title.getBackgroundPaint();
- title.setFont(font);
- //设置标题的字体颜色
- title.setPaint(Color.RED);
- chart.setTitle(title);
- frame.pack();
- frame.setVisible(true);
- }
- }
原文链接:http://hefeng1987-net-163-com.iteye.com/blog/846041