本文概述
- 条形图示例
- BarChartExample.java
下图显示了JFreeChart库中合并的条形图的一些演示版本:
【JFreeChart条形图】条形图演示1:
文章图片
条形图演示2:
文章图片
条形图演示3:
文章图片
条形图演示4:
文章图片
条形图示例 让我们考虑条形图示例的样本输入数据。
年 | 百万人口 | ||
---|---|---|---|
USA | INDIA | CHINA | |
2005 | 10 | 15 | 20 |
2010 | 15 | 20 | 25 |
2015 | 20 | 25 | 30 |
BarChartExample.java
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class BarChartExample extends JFrame {private static final long serialVersionUID = 1L;
public BarChartExample(String appTitle) {super(appTitle);
// Create DatasetCategoryDataset dataset = createDataset();
//Create chartJFreeChart chart=ChartFactory.createBarChart("Bar Chart Example", //Chart Title"Year", // Category axis"Population in Million", // Value axisdataset, PlotOrientation.VERTICAL, true, true, false);
ChartPanel panel=new ChartPanel(chart);
setContentPane(panel);
}private CategoryDataset createDataset() {DefaultCategoryDataset dataset = new DefaultCategoryDataset();
// Population in 2005dataset.addValue(10, "USA", "2005");
dataset.addValue(15, "India", "2005");
dataset.addValue(20, "China", "2005");
// Population in 2010dataset.addValue(15, "USA", "2010");
dataset.addValue(20, "India", "2010");
dataset.addValue(25, "China", "2010");
// Population in 2015dataset.addValue(20, "USA", "2015");
dataset.addValue(25, "India", "2015");
dataset.addValue(30, "China", "2015");
return dataset;
}public static void main(String[] args) throws Exception {SwingUtilities.invokeAndWait(()->
{BarChartExample example=new BarChartExample("Bar Chart Window");
example.setSize(800, 400);
example.setLocationRelativeTo(null);
example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
example.setVisible(true);
});
}}
输出:
文章图片
推荐阅读
- JFreeChart架构
- 什么是JFreeChart()
- JFreeChart教程介绍
- PiePlot类介绍
- JFreeChart的关键类
- 安装JfreeChart
- Javafreechart功能
- JFreeChart类介绍
- ChartUtilities类