import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
public class BarChartDemo6 {
public BarChartDemo6() throws Exception{
createBarchart();
}
public void createBarchart() throws Exception{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(3,"18岁","123001");
dataset.addValue(11,"19岁","123001");
dataset.addValue(18,"20岁","123001");
dataset.addValue(12,"21岁","123001");
dataset.addValue(6,"22岁","123001");
dataset.addValue(3,"18岁","123002");
dataset.addValue(11,"19岁","123002");
dataset.addValue(18,"20岁","123002");
dataset.addValue(12,"21岁","123002");
dataset.addValue(6,"22岁","123002");
dataset.addValue(3,"18岁","123003");
dataset.addValue(11,"19岁","123003");
dataset.addValue(18,"20岁","123003");
dataset.addValue(12,"21岁","123003");
dataset.addValue(6,"22岁","123003");
//得到JFreeChart对象,createBarChart3D生成立方体图表对象
JFreeChart chart = ChartFactory.createBarChart3D("班级年龄分布图", // 图表标题
"年龄对照", // 目录轴的显示标签
"人数", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:垂直,水平
true, // 是否显示图例(对于简单的柱状图必须是false)
true, // 是否生成工具
false // 是否生成URL链接
);
// 设置样式
CategoryPlot plot=chart.getCategoryPlot(); // 获得图表区域对象
BarRenderer renderer = (BarRenderer)plot.getRenderer();
renderer.setSeriesPaint(0, Color.black);
renderer.setSeriesPaint(1, Color.red);
renderer.setSeriesPaint(2, Color.blue);
renderer.setSeriesPaint(3, Color.yellow);
renderer.setSeriesPaint(4, Color.green);
//在本地生成的窗口中显示图形
ChartFrame frame=new ChartFrame("年龄分布",chart);
frame.pack();
frame.setVisible(true) ;
}
public static void main(String[] args) throws Exception{
new BarChartDemo6();
}
}
|
|