JFreeChart是一款java图形类库,可以生成饼图、树状图、散点图等等,并且可以以图片的形式输出。需要的jar包 jfreechart-1.0.10.jar、 gnujaxp.jar和 jcommon-1.0.13.jar这三个包;
简单生成一个饼图,在main方法中显示:public static void main(String[] args){
DefaultPieDataset dpd = new DefaultPieDataset(); // 建立一个默认的饼图
dpd.setValue("程序员", 25); // 输入数据
dpd.setValue("初级工程师", 30);
dpd.setValue("中级工程师", 35);
dpd.setValue("高级工程师", 40);
dpd.setValue("架构师", 50);
//设置字体为中文
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
// 设置标题字体
standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
// 设置图例的字体
standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
// 设置轴向的字体
standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
ChartFactory.setChartTheme(standardChartTheme);
// 可以查具体的API文档,第一个参数是标题,第二个参数是一个数据集,第三个参数表示是否显示Legend,第四个参数表示是否显示提示,第五个参数表示图中是否存在URL
JFreeChart chart = ChartFactory.createPieChart("开发层次结构图", dpd, true, true, false);
ChartFrame chartFrame = new ChartFrame("开发层次结构图", chart);
// chart要放在Java容器组件中,ChartFrame继承自java的Jframe类。该第一个参数的数据是放在窗口左上角的,不是正中间的标题。
chartFrame.pack(); // 以合适的大小展现图形
chartFrame.setVisible(true);// 图形是否可见
}
效果图如下:
简单生成一个饼图,在main方法中显示:public static void main(String[] args){
double[][] data = new double[][] { { 1230, 1110, 1120, 1210 }, { 720, 750, 860, 800 }, { 830, 780, 790, 700, },
{ 400, 380, 390, 450 } };
String[] rowKeys = { "番茄", "辣椒", "茄子", "苦瓜" };
String[] columnKeys = { "甘肃", "西安", "天水", "兰州" };
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("蔬菜销量统计图", "蔬菜", "销量", dataset, PlotOrientation.VERTICAL, true,
true, false);
//中文乱码设置
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体",Font.BOLD,20));
LegendTitle legend = chart.getLegend();
legend.setItemFont(new Font("宋体", Font.BOLD, 20));
CategoryPlot plot = (CategoryPlot)chart.getPlot();
CategoryAxis domainAxis = plot.getDomainAxis();//(柱状图的x轴)
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体
domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴上的标题的字体
ValueAxis valueAxis = plot.getRangeAxis();//(柱状图的y轴)
valueAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的字体
valueAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的标题的字体
CategoryPlot plot2 = chart.getCategoryPlot();
// 设置网格背景颜色
plot2.setBackgroundPaint(Color.white);
// 设置网格竖线颜色
plot2.setDomainGridlinePaint(Color.pink);
// 设置网格横线颜色
plot2.setRangeGridlinePaint(Color.pink);
// 显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
// 默认的数字显示在柱子中,通过如下两句可调整数字的显示
// 注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
renderer.setBasePositiveItemLabelPosition(
new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
// 设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.4);
plot.setRenderer(renderer);
ChartFrame chartFrame = new ChartFrame("蔬菜销量统计图", chart);
// chart要放在Java容器组件中,ChartFrame继承自java的Jframe类。该第一个参数的数据是放在窗口左上角的,不是正中间的标题。
chartFrame.pack(); // 以合适的大小展现图形
chartFrame.setVisible(true);// 图形是否可见
}
效果图: