/**
* 要是用JFreeChar生成柱状图,不做任何注释,只求应用,生成的柱状图放在D:\\fruit.jpg
*
* 1.加入jcommon-1.0.5.jar和jfreechart-1.0.2.jar
*
* 2.设置值
* dataset.addValue(100, "", "苹果");
* dataset.addValue(200, "", "梨子");
* dataset.addValue(300, "", "葡萄");
* dataset.addValue(400, "", "香蕉");
* dataset.addValue(500, "", "荔枝");
*
* JFreeChart chart = ChartFactory.createBarChart3D(
"水果产量图", // 图表标题
"水果", // 目录轴的显示标签
"产量", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL,
true,
false,
false
);
*
* fos_jpg = new FileOutputStream("D:\\fruit.jpg");
*/
package com.bjpowernode.drp.statreport;
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.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class BarChartDemo {
public static void main(String[] args) throws IOException {
CategoryDataset dataset = getDataSet();
JFreeChart chart = ChartFactory.createBarChart3D(
"水果产量图", // 图表标题
"水果", // 目录轴的显示标签
"产量", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL,
true,
false,
false
);
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("D:\\fruit.jpg");
CategoryPlot plot = (CategoryPlot)chart.getPlot();
StackedBarRenderer3D renderer=new StackedBarRenderer3D();
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer);
ChartUtilities.writeChartAsJPEG(fos_jpg,1.0f,chart,400,300,null);
} finally {
try {
fos_jpg.close();
} catch (Exception e) {
throw new RuntimeException("生成柱状图失败!");
}
}
}
private static CategoryDataset getDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "", "苹果");
dataset.addValue(200, "", "梨子");
dataset.addValue(300, "", "葡萄");
dataset.addValue(400, "", "香蕉");
dataset.addValue(500, "", "荔枝");
return dataset;
}
}