博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JfreeChart的使用
阅读量:6002 次
发布时间:2019-06-20

本文共 13478 字,大约阅读时间需要 44 分钟。

hot3.png

Jfreechart可以用来制作各种类型的图表,这些图标包括饼图,柱状图,线图,区域图,分布图,混合图,甘特图及一些仪表盘。

jfreechart的maven引用如下:

org.jfree
jfreechart
1.0.15
org.jfree
jfreechart
1.0.15
sources
org.jfree
jfreechart
1.0.15
javadoc

官方地址为

jfreechart的图形可以导出为jpg和png格式,也可以和pdf和excel关联。

jfreechart主要两个大的包组成:org.jfree.chart,org.jfree.data。其中前者主要与图形本身有关,后者与图形显示的数据有关。

核心的类有

org.jfree.chart.JFreeChart :图表对象,任何类型的图表的最终表现形式都是在该对象进行一些属性的定制。JFreeChart引擎本身提供了一个工厂类用于创建不同类型的图表对象

org.jfree.data.category.XXXDataSet: 数据集对象,用于提供显示图表所用的数据。根据不同类型的图表对应着很多类型的数据集对象类

org.jfree.chart.plot.XXXPlot :图表区域对象,基本上这个对象决定着什么样式的图表,创建该对象的时候需要Axis、Renderer以及数据集对象的支持

org.jfree.chart.axis.XXXAxis :用于处理图表的两个轴:纵轴和横轴

org.jfree.chart.render.XXXRender :负责如何显示一个图表对象

org.jfree.chart.urls.XXXURLGenerator: 用于生成Web图表中每个项目的鼠标点击链接

XXXXXToolTipGenerator: 用于生成图象的帮助提示,不同类型图表对应不同类型的工具提示类

这里有一个完整的jfreechart的代码

public class JfreeChartTest {    public static void main(String [] args){        test();        test2();                test3();        test4();        test5();        test6();        test7();    }    public static void test() {        //创建饼图数据对象        DefaultPieDataset dfp=new DefaultPieDataset();        dfp.setValue("管理人员", 25);        dfp.setValue("市场人员", 35);        dfp.setValue("开发人员", 20);        dfp.setValue("后勤人员", 5);        dfp.setValue("财务人员", 15);        //Create JFreeChart object        JFreeChart a =ChartFactory.createPieChart("CityInfoPort公司组织架构图",dfp, true, true, true);        ChartFrame  frame=new ChartFrame ("CityInfoPort公司组织架构图 ",a,true);        frame.pack();        frame.setVisible(true);    }    public static void test2() {        //创建类别图(Category)数据对象        DefaultCategoryDataset dataset = new DefaultCategoryDataset();        dataset.addValue(100, "北京", "苹果");        dataset.addValue(100, "上海", "苹果");        dataset.addValue(100, "广州", "苹果");        dataset.addValue(200, "北京", "梨子");        dataset.addValue(200, "上海", "梨子");        dataset.addValue(200, "广州", "梨子");        dataset.addValue(300, "北京", "葡萄");        dataset.addValue(300, "上海", "葡萄");        dataset.addValue(300, "广州", "葡萄");        dataset.addValue(400, "北京", "香蕉");        dataset.addValue(400, "上海", "香蕉");        dataset.addValue(400, "广州", "香蕉");        dataset.addValue(500, "北京", "荔枝");        dataset.addValue(500, "上海", "荔枝");        dataset.addValue(500, "广州", "荔枝");        JFreeChart chart=ChartFactory.createBarChart3D("水果产量图", "水果", "水果", dataset, PlotOrientation.VERTICAL, true, true, true);        ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true);        frame.pack();        frame.setVisible(true);    }    private static void test3() {        String series1 = "First";        String series2 = "Second";        String series3 = "Third";        String type1 = "2001";        String type2 = "2002";        String type3 = "2003";        String type4 = "2004";        String type5 = "2005";        String type6 = "2006";        String type7 = "2007";        String type8 = "2008";        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();        defaultcategorydataset.addValue(1.0D, series1, type1);        defaultcategorydataset.addValue(4D, series1, type2);        defaultcategorydataset.addValue(3D, series1, type3);        defaultcategorydataset.addValue(5D, series1, type4);        defaultcategorydataset.addValue(5D, series1, type5);        defaultcategorydataset.addValue(7D, series1, type6);        defaultcategorydataset.addValue(7D, series1, type7);        defaultcategorydataset.addValue(8D, series1, type8);        defaultcategorydataset.addValue(5D, series2, type1);        defaultcategorydataset.addValue(7D, series2, type2);        defaultcategorydataset.addValue(6D, series2, type3);        defaultcategorydataset.addValue(8D, series2, type4);        defaultcategorydataset.addValue(4D, series2, type5);        defaultcategorydataset.addValue(4D, series2, type6);        defaultcategorydataset.addValue(2D, series2, type7);        defaultcategorydataset.addValue(1.0D, series2, type8);        defaultcategorydataset.addValue(4D, series3, type1);        defaultcategorydataset.addValue(3D, series3, type2);        defaultcategorydataset.addValue(2D, series3, type3);        defaultcategorydataset.addValue(3D, series3, type4);        defaultcategorydataset.addValue(6D, series3, type5);        defaultcategorydataset.addValue(3D, series3, type6);        defaultcategorydataset.addValue(4D, series3, type7);        defaultcategorydataset.addValue(3D, series3, type8);        JFreeChart jfreechart = ChartFactory.createLineChart("折线图 Demo 1",                "Type","Value",                defaultcategorydataset,PlotOrientation.VERTICAL,                true,true,false);        CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();        categoryplot.setBackgroundPaint(Color.lightGray);        categoryplot.setRangeGridlinePaint(Color.white);        ChartFrame  frame=new ChartFrame ("折线图 ",jfreechart,true);        frame.pack();        frame.setVisible(true);    }    private static void test4() {        XYSeries xyseries = new XYSeries("First"); //先产生XYSeries 对象        xyseries.add(1.0D, 1.0D);        xyseries.add(2D, 4D);        xyseries.add(3D, 3D);        xyseries.add(4D, 5D);        xyseries.add(5D, 5D);        xyseries.add(6D, 7D);        xyseries.add(7D, 7D);        xyseries.add(8D, 8D);        XYSeries xyseries1 = new XYSeries("Second");        xyseries1.add(1.0D, 5D);        xyseries1.add(2D, 7D);        xyseries1.add(3D, 6D);        xyseries1.add(4D, 8D);        xyseries1.add(5D, 4D);        xyseries1.add(6D, 4D);        xyseries1.add(7D, 2D);        xyseries1.add(8D, 1.0D);        XYSeries xyseries2 = new XYSeries("Third");        xyseries2.add(3D, 4D);        xyseries2.add(4D, 3D);        xyseries2.add(5D, 2D);        xyseries2.add(6D, 3D);        xyseries2.add(7D, 6D);        xyseries2.add(8D, 3D);        xyseries2.add(9D, 4D);        xyseries2.add(10D, 3D);        XYSeriesCollection xyseriescollection = new XYSeriesCollection(); //再用XYSeriesCollection添加入XYSeries 对象        xyseriescollection.addSeries(xyseries);        xyseriescollection.addSeries(xyseries1);        xyseriescollection.addSeries(xyseries2);        JFreeChart jfreechart = ChartFactory.createXYLineChart("Line Chart Demo 2",                "X",                "Y",                xyseriescollection,                PlotOrientation.VERTICAL,                true,                true,                false);        ChartFrame  frame=new ChartFrame ("折线图 ",jfreechart,true);        frame.pack();        frame.setVisible(true);    }    private static void test5() {        TimeSeries timeseries = new TimeSeries("L&G European Index Trust",Month.class);        timeseries.add(new Month(2, 2001), 181.8D);//这里用的是Month.class,同样还有Day.class Year.class 等等        timeseries.add(new Month(3, 2001), 167.3D);        timeseries.add(new Month(4, 2001), 153.8D);        timeseries.add(new Month(5, 2001), 167.6D);        timeseries.add(new Month(6, 2001), 158.8D);        timeseries.add(new Month(7, 2001), 148.3D);        timeseries.add(new Month(8, 2001), 153.9D);        timeseries.add(new Month(9, 2001), 142.7D);        timeseries.add(new Month(10, 2001), 123.2D);        timeseries.add(new Month(11, 2001), 131.8D);        timeseries.add(new Month(12, 2001), 139.6D);        timeseries.add(new Month(1, 2002), 142.9D);        timeseries.add(new Month(2, 2002), 138.7D);        timeseries.add(new Month(3, 2002), 137.3D);        timeseries.add(new Month(4, 2002), 143.9D);        timeseries.add(new Month(5, 2002), 139.8D);        timeseries.add(new Month(6, 2002), 137D);        timeseries.add(new Month(7, 2002), 132.8D);        TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust",Month.class);        timeseries1.add(new Month(2, 2001), 129.6D);        timeseries1.add(new Month(3, 2001), 123.2D);        timeseries1.add(new Month(4, 2001), 117.2D);        timeseries1.add(new Month(5, 2001), 124.1D);        timeseries1.add(new Month(6, 2001), 122.6D);        timeseries1.add(new Month(7, 2001), 119.2D);        timeseries1.add(new Month(8, 2001), 116.5D);        timeseries1.add(new Month(9, 2001), 112.7D);        timeseries1.add(new Month(10, 2001), 101.5D);        timeseries1.add(new Month(11, 2001), 106.1D);        timeseries1.add(new Month(12, 2001), 110.3D);        timeseries1.add(new Month(1, 2002), 111.7D);        timeseries1.add(new Month(2, 2002), 111D);        timeseries1.add(new Month(3, 2002), 109.6D);        timeseries1.add(new Month(4, 2002), 113.2D);        timeseries1.add(new Month(5, 2002), 111.6D);        timeseries1.add(new Month(6, 2002), 108.8D);        timeseries1.add(new Month(7, 2002), 101.6D);        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();        timeseriescollection.addSeries(timeseries);        timeseriescollection.addSeries(timeseries1);        timeseriescollection.setDomainIsPointsInTime(true); //domain轴上的刻度点代表的是时间点而不是时间段        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices",                "Date",                "Price Per Unit",                timeseriescollection,                true,                true,                false);        jfreechart.setBackgroundPaint(Color.white);        XYPlot xyplot = (XYPlot)jfreechart.getPlot(); //获得 plot : XYPlot!!        xyplot.setBackgroundPaint(Color.lightGray);        xyplot.setDomainGridlinePaint(Color.white);        xyplot.setRangeGridlinePaint(Color.white);        xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));        xyplot.setDomainCrosshairVisible(true);        xyplot.setRangeCrosshairVisible(true);        ChartFrame  frame=new ChartFrame ("折线图 ",jfreechart,true);        frame.pack();        frame.setVisible(true);    }    private static void test6() {        DefaultPieDataset dataset = new DefaultPieDataset();        dataset.setValue(" 市场前期", new Double(10));        dataset.setValue(" 立项", new Double(15));        dataset.setValue(" 计划", new Double(10));        dataset.setValue(" 需求与设计", new Double(10));        dataset.setValue(" 执行控制", new Double(35));        dataset.setValue(" 收尾", new Double(10));        dataset.setValue(" 运维",new Double(10));        JFreeChart chart = ChartFactory.createPieChart3D(                " 项目进度分布", // chart title                dataset,// data                true,// include legend                true,                false        );        PiePlot3D plot=(PiePlot3D)chart.getPlot();        // 图片中显示百分比:默认方式        //plot.setLabelGenerator(new           StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));        // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));        // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));        // 设置背景色为白色        chart.setBackgroundPaint(Color.white);        // 指定图片的透明度(0.0-1.0)        plot.setForegroundAlpha(1.0f);        // 指定显示的饼图上圆形(false)还椭圆形(true)        plot.setCircular(true);        // 设置图标题的字体        Font font = new Font(" 黑体",Font.CENTER_BASELINE,20);        TextTitle title = new TextTitle(" 项目状态分布");        title.setFont(font);        chart.setTitle(title);        FileOutputStream fos_jpg = null;        try {            fos_jpg=new FileOutputStream("D:\\ 项目状态分布.jpg");            ChartUtilities.writeChartAsJPEG(fos_jpg, 100, chart, 640, 480, null);            fos_jpg.close();        } catch (Exception e) {        }    }    private static void test7() {        JfreeChartOne one=new JfreeChartOne("CityInfoPort公司组织架构图");        one.pack();        one.setVisible(true);    }}

class JfreeChartOne extends ApplicationFrame {    private static final long serialVersionUID = 1L;    public JfreeChartOne(String s)    {        super(s);        setContentPane(createJPanel());    }    //利用静态方法设定数据源(饼状图)    public static PieDataset createPieDataset() {        DefaultPieDataset defaultpiedataset = new DefaultPieDataset();        defaultpiedataset.setValue("管理人员", 10.02D);        defaultpiedataset.setValue("市场人员", 20.23D);        defaultpiedataset.setValue("开发人员", 60.02D);        defaultpiedataset.setValue("OEM人员", 10.02D);        defaultpiedataset.setValue("其他人员", 5.11D);        return defaultpiedataset;    }    // 通过ChartFactory创建JFreeChart的实例    public static JFreeChart createJFreeChart(PieDataset p)    {        JFreeChart a = ChartFactory.createPieChart("CityInfoPort公司组织架构图", p, true, true, true);        PiePlot pie=(PiePlot)a.getPlot();        pie.setLabelFont(new Font("SansSerif",Font.BOLD,12));        pie.setNoDataMessage("No data available");        pie.setCircular(true);        pie.setLabelGap(0.01D);//间距        return a;    }    public static JPanel createJPanel (){        JFreeChart jfreechart = createJFreeChart(createPieDataset());        return new ChartPanel(jfreechart);    }}

参考文档:

总结一些,这里主要就介绍了如何用jfreechart输出图像,并没有和servlet结合起来。以后可以再试试如何和servlet或者web项目结合。

转载于:https://my.oschina.net/zimingforever/blog/146239

你可能感兴趣的文章