在java应用程序中怎么绘制统计图呢,我和大家分享一下jfreechart绘制统计图的方法,我写的经验对你学习java有帮助的话,给我投票、点赞或者收藏!
1java使用jfreechart绘制条形统计图
工具/原料
- eclipse
方法/步骤
- 1
使用eclipse,新建一个java项目,项目的名称javachar。
- 2
在项目中增加jfreechar类库,没有jfreechar下载,http://www.jfree.org/jfreechart/download/。
- 3
新建一个javachar.java类,带有main方法的主类。
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 PieDataset shuju( )
{
DefaultPieDataset dataset = new DefaultPieDataset( );
dataset.setValue( \"安阳一中\" , new Double( 50 ) );
dataset.setValue( \"安阳二中\" , new Double( 100 ) );
dataset.setValue( \"安阳三中\" , new Double( 150 ) );
dataset.setValue( \"安阳六中\" , new Double( 80 ) );
return dataset;
}
- 6
定义饼型图:
public static JFreeChart createChart( PieDataset dataset )
{
StandardChartTheme standardChartTheme = new StandardChartTheme(\"CN\");
standardChartTheme.setExtraLargeFont(new Font(\"宋书\", Font.BOLD, 25));
standardChartTheme.setRegularFont(new Font(\"宋书\", Font.PLAIN, 15));
standardChartTheme.setLargeFont(new Font(\"宋书\", Font.PLAIN, 15));
ChartFactory.setChartTheme(standardChartTheme);
JFreeChart chart = ChartFactory.createPieChart(\"中学招生人数\",dataset, true, true, false);
return chart;
}
- 7
在main方法中,把统计图增加到窗口中。
jf.add(new ChartPanel(createChart(shuju())));
- 8
运行项目,结果如下图。