原创 struts2.1.8.1+JFreeChart -1.0.13 的整合之-----教复杂的柱状图 收藏
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class BarChart3DDemo {
public static void main(String[] args) throws IOException
{
JFreeChart chart = ChartFactory.createBarChart3D(
"图书销售统计图", //标题
"图书", //横轴
"销量", //纵轴
getDataSet(),
PlotOrientation.VERTICAL,
true,
false,
false);
//重新设置图表标题,改变字体
chart.setTitle(new TextTitle("图书销售统计图",new Font("黑体",Font.ITALIC,22)));
//取得统计图表的第一个图例,并修改图例的字体
LegendTitle legend = chart.getLegend(0);
legend.setItemFont(new Font("宋体",Font.ITALIC,14));
CategoryPlot plot = (CategoryPlot)chart.getPlot();
//取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
//设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,22)); //设置横轴的标签(字体)
//分类标签以45度角倾斜
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryAxis.setTickLabelFont(new Font("宋体",Font.BOLD,18)); //设置横轴的每个柱子的标签(字体)
//取得纵轴
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
//设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体",Font.PLAIN,22));//设置纵轴的标签(字体)
//输出到图片文件
FileOutputStream fos = null;
fos = new FileOutputStream("bar3D1.jpg");
//将统计图表输出成JPG文件
ChartUtilities.writeChartAsJPEG(fos, 1, chart, 800, 600); //输出并生成文件到当前文件目录下
fos.close();
}
private static CategoryDataset getDataSet()
{
//提供创建统计图表的数据
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(45000, "北京", "疯狂JAVA讲义");
dataset.addValue(38000, "北京", "轻量级JAVA EE企业实战");
dataset.addValue(24000, "北京", "疯狂AJAX讲义");
dataset.addValue(32000, "北京", "STRUTS 2权威指南");
dataset.addValue(21000, "北京", "疯狂XML讲义");
dataset.addValue(37000, "上海", "疯狂JAVA讲义");
dataset.addValue(36000, "上海", "轻量级JAVA EE企业实战");
dataset.addValue(34000, "上海", "疯狂AJAX讲义");
dataset.addValue(42000, "上海", "STRUTS 2权威指南");
dataset.addValue(12000, "上海", "疯狂XML讲义");
dataset.addValue(42000, "广州", "疯狂JAVA讲义");
dataset.addValue(40000, "广州", "轻量级JAVA EE企业实战");
dataset.addValue(34000, "广州", "疯狂AJAX讲义");
dataset.addValue(18000, "广州", "STRUTS 2权威指南");
dataset.addValue(26000, "广州", "疯狂XML讲义");
return dataset;
}
}
效果图为:
忘了加数字了,呵呵
在加一下:
//让柱子上显示数值的方法---begin
BarRenderer renderer = new BarRenderer();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
plot.setRenderer(renderer);
//让柱子上显示数值的方法---end
添加在fos前面即可
效果图为:
其实呢:
也可以这样来获取DataSet,并生成JFreeChart实例
public JFreeChart getChart(){
double[][] data = new double[][]
{
getLastScore(),
getScore()
};
String[] rowKeys = {this.getLastYear()+"年",this.getThisYear()+"年"};
String[] columnKeys = { "一月", "二月", "三月", "四月", "五月","六月",
"七月", "八月", "九月", "十月", "十一月","十二月"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("营业厅检查记录柱状图", "月份", "历史检查成绩", dataset, PlotOrientation.VERTICAL, true, true, false);
return chart;
}
文章来源:http://blog.csdn.net/aiouwen521/archive/2010/11/24/6033117.aspx