2.1. Simple Bar Chart
First prepare the data for the histogram, the histogram must use Categorydataset, here we use Defaultcategorydataset.
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100,"Spring Security","Jan");
dataset.addValue(200,"jBPM 4","Jan");
dataset.addValue(300,"Ext JS","Jan");
dataset.addValue(400,"JFreeChart","Jan");
Each call to AddValue () adds a piece of data to the dataset, which appears to be a columnar rectangle, the first parameter 100 is the length of the column rectangle, the second parameter represents the name of the data, and the third parameter represents the classification of the data.
With the chart data, the next step is to create a columnar chart with this data.
JFreeChart chart = ChartFactory.createBarChart(
"chart",// 标题
"num",// 横轴名称
"type",// 纵轴名称
dataset,// 数据
PlotOrientation.VERTICAL,// 使用垂直柱状图
true,// 是否使用legend
false,// 是否使用tooltip
false // 是否使用url链接
);
Finally, use Chartutilities to export chart as a PNG picture.
FileOutputStream fos = null;
try {
fos = new FileOutputStream("target/bar1.png");
ChartUtilities.writeChartAsPNG(fos,chart,400,300);
} finally {
fos.close();
}
Figure 2.1. Simple bar Chart