eclipse
在eclipse中新建一个java项目,项目名称为javachar。
在项目中增加类库:jfreechart-1.0.12.jar,jcommon-1.0.15.jar。没有这个文件在这个地址下载http://www.jfree.org/jfreechart/download/
在项目中新建一个主类javachar。
在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','语文100-120'); dataset.setValue(30,'语文90-100','语文90-100'); dataset.setValue(10,'语文80-90','语文80-90'); dataset.setValue(5,'语文80以下','语文80以下'); return dataset; }
在类中定义一个静态函数,返回统计图jfreechar/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.createBarChart('语文成绩分布统计图', '成绩分布', '部分数量', dataset, PlotOrientation.VERTICAL, true, true, false); return chart;
在main方法中,把统计图显示在窗口上:jf.add(new ChartPanel(tongjitu(shuju())));