JFreeChart简单demo

来源:互联网 发布:c语言工资待遇 编辑:程序博客网 时间:2024/06/06 08:54

资源下载地址http://sourceforge.net/projects/jfreechart/files/

需要导入的包

 jcommon-1.0.23.jar 

 jfreechart-1.0.19.jar

饼状图

package com.test.jfreechart;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartFrame;import org.jfree.chart.JFreeChart;import org.jfree.data.general.DefaultPieDataset;/** * 饼状图 * @author Administrator * */public class JFreeChartTestPoe {/** * @param args */public static void main(String[] args) {DefaultPieDataset dpd = new DefaultPieDataset();dpd.setValue("管理人员", 10);dpd.setValue("市场人员", 20);dpd.setValue("开发人员", 30);dpd.setValue("后勤人员", 5);dpd.setValue("后勤人员1", 5);dpd.setValue("后勤人员2", 5);JFreeChart chart = ChartFactory.createPieChart("QQ公司人员结构", dpd);ChartFrame chartFrame = new ChartFrame("hhh",chart);chartFrame.pack();chartFrame.setVisible(true);}}

柱状图

package com.test.jfreechart;import java.awt.Font;import java.io.File;import java.io.IOException;import javax.swing.JPanel;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartPanel;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.title.LegendTitle;import org.jfree.chart.title.TextTitle;import org.jfree.data.category.CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.ui.ApplicationFrame;public class CategoryTest extends ApplicationFrame{ public CategoryTest(String title) throws IOException    {        super(title);        this.setContentPane(createPanel()); //构造函数中自动创建Java的panel面板    }        public static CategoryDataset createDataset() //创建柱状图数据集    {        DefaultCategoryDataset dataset=new DefaultCategoryDataset();        dataset.setValue(10,"管理人员","a");        dataset.setValue(20,"市场人员","b");        dataset.setValue(40,"开发人员","c");        dataset.setValue(15,"其他人员","d");        return dataset;    }        public static JFreeChart createChart(CategoryDataset dataset) throws IOException //用数据集创建一个图表    {        JFreeChart chart=ChartFactory.createBarChart("hi", "人员分布",                 "人员数量", dataset, PlotOrientation.VERTICAL, true, true, false); //创建一个JFreeChart        chart.setTitle(new TextTitle("某公司组织结构图",new Font("宋体",Font.BOLD+Font.ITALIC,20)));//可以重新设置标题,替换“hi”标题        CategoryPlot plot=(CategoryPlot)chart.getPlot();//获得图标中间部分,即plot        CategoryAxis categoryAxis=plot.getDomainAxis();//获得横坐标        categoryAxis.setLabelFont(new Font("微软雅黑",Font.BOLD,12));//设置横坐标字体        categoryAxis.setTickLabelFont(new Font("微软雅黑",Font.BOLD,12));                 LegendTitle lt = chart.getLegend(); //获取图例        lt.setItemFont(new Font("微软雅黑",Font.BOLD,12));        //生成图片        ChartUtilities.saveChartAsJPEG(new File("1.jpg"), chart, 400, 400);                return chart;    }        public static JPanel createPanel() throws IOException    {        JFreeChart chart =createChart(createDataset());        return new ChartPanel(chart); //将chart对象放入Panel面板中去,ChartPanel类已继承Jpanel    }        public static void main(String[] args) throws IOException    {    CategoryTest chart=new CategoryTest("某公司组织结构图");        chart.pack();//以合适的大小显示        chart.setVisible(true);            }}


0 0