eclipse
使用eclipse工具,新建一个java项目,项目的名称为javachart。
在项目添加jar:jfreechart.
在项目建立一个类:package javachart;public class javachart { public static void main(String[] args) { // TODO Auto-generated method stub }}
在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); }
在类中定义统计图的数据:public static CategoryDataset shuju() { DefaultCategoryDataset dataset=new DefaultCategoryDataset(); dataset.setValue(10,'语文','100-120'); dataset.setValue(30,'语文','90-100'); dataset.setValue(10,'语文','80-90'); dataset.setValue(5,'语文','80以下'); dataset.setValue(20,'数学','100-120'); dataset.setValue(20,'数学','90-100'); dataset.setValue(5,'数学','80-90'); dataset.setValue(1,'数学','80以下'); dataset.setValue(30,'英语','100-120'); dataset.setValue(20,'英语','90-100'); dataset.setValue(2,'英语','80-90'); dataset.setValue(2,'英语','80以下'); return dataset; }
定义一个3d条形统计图: public static JFreeChart tongjitu(CategoryDataset dataset) { 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.createBarChart3D('各科成绩分布统计图', '成绩分布', '人数', dataset, PlotOrientation.VERTICAL, true, true, false); chart.setBackgroundPaint(ChartColor.WHITE); CategoryPlot p = chart.getCategoryPlot(); p.setBackgroundPaint(new ChartColor(245,245,245)); return chart; }
统计图结果如下图。