java使用jfreechart功能绘制线型统计图,我和大家分享一下jfreechart绘制统计图的方法,我写的经验对你学习java有帮助的话,给我投票、点赞或者收藏!
1java使用jfreechar绘制饼型统计图
1java使用jfreechart绘制条形统计图
工具/原料
- eclipse
方法/步骤
- 1
使用eclipse新建一个java项目,项目名称javachar。
- 2
在项目增加jfreechart文件,没有这个文件时上百度下载。
- 3
在项目中新建一个主类:
package javachar;
public class javachar {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
- 4
在main方法中,创建一个窗口显示统计图。
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf=new JFrame();
jf.setSize(600,500);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
- 5
在类中定义函数,返回要统计的数据:
public static DefaultCategoryDataset shuju(){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue( 100 , \"2018\" , \"1\" );
dataset.addValue( 60 , \"2018\" , \"2\" );
dataset.addValue( 150 , \"2018\" , \"3\" );
dataset.addValue( 170 , \"2018\" , \"4\" );
dataset.addValue( 180 , \"2018\" , \"5\" );
dataset.addValue( 200 , \"2018\" , \"6\" );
dataset.addValue( 130 , \"2018\" , \"7\" );
dataset.addValue( 190 , \"2018\" , \"8\" );
dataset.addValue( 120 , \"2018\" , \"9\" );
dataset.addValue( 130 , \"2018\" , \"10\" );
dataset.addValue( 170 , \"2018\" , \"11\" );
dataset.addValue( 50 , \"2018\" , \"12\" );
return dataset;
}
- 6
生成统计图:
StandardChartTheme standardChartTheme = new StandardChartTheme(\"CN\");
standardChartTheme.setExtraLargeFont(new Font(\"宋书\", Font.BOLD, 26));
standardChartTheme.setRegularFont(new Font(\"宋书\", Font.PLAIN, 16));
standardChartTheme.setLargeFont(new Font(\"宋书\", Font.PLAIN, 16));
ChartFactory.setChartTheme(standardChartTheme);
JFreeChart chart = ChartFactory.createLineChart(
\"年销售金额分布图\",\"月份\",
\"销售金额(万)\",
dataset,PlotOrientation.VERTICAL,
true,true,false);
return chart;
- 7
在窗口中显示统计图:
jf.add(new ChartPanel(tongjitu(shuju())));
- 8
运行项目,统计图如下图。