• JFreeChart的使用示例


    示例一,饼图,简单示例:

    导入jar,代码文件:

    运行结果:

    代码:

    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartFrame;
    import org.jfree.chart.JFreeChart;
    import org.jfree.data.general.DefaultPieDataset;

    public class JFreeChartTest
    {
        public static void main(String[] args)
        {
            DefaultPieDataset dpd=new DefaultPieDataset(); //建立一个默认的饼图
            dpd.setValue("管理人员", 25);  //输入数据
            dpd.setValue("市场人员", 25);
            dpd.setValue("开发人员", 45);
            dpd.setValue("其他人员", 10);
            
            JFreeChart chart=ChartFactory.createPieChart("某公司人员组织数据图",dpd,true,true,false);
            //可以查具体的API文档,第一个参数是标题,第二个参数是一个数据集,第三个参数表示是否显示Legend,第四个参数表示是否显示提示,第五个参数表示图中是否存在URL
            
            ChartFrame chartFrame=new ChartFrame("某公司人员组织数据图",chart);
            //chart要放在Java容器组件中,ChartFrame继承自java的Jframe类。该第一个参数的数据是放在窗口左上角的,不是正中间的标题。
            chartFrame.pack(); //以合适的大小展现图形
            chartFrame.setVisible(true);//图形是否可见
            
        }
    }

    示例二,柱状图/直方图

    运行结果:

    代码:

    package com.test.jfreechart;

    import java.awt.Font;

    import javax.swing.JPanel;

    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    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.TextTitle;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.ui.ApplicationFrame;

    public class JFreeChartTest2 extends ApplicationFrame
    {
        public JFreeChartTest2(String title)
        {
            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) //用数据集创建一个图表
        {
            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));//设置横坐标字体
            return chart;
        }
        
        public static JPanel createPanel()
        {
            JFreeChart chart =createChart(createDataset());
            return new ChartPanel(chart); //将chart对象放入Panel面板中去,ChartPanel类已继承Jpanel
        }
        
        public static void main(String[] args)
        {
            JFreeChartTest2 chart=new JFreeChartTest2("某公司组织结构图");
            chart.pack();//以合适的大小显示
            chart.setVisible(true);
            
        }
    }

    示例三,柱状图/直方图,保存图片文件

    代码:

    import java.awt.Font;
    import java.io.FileOutputStream;
    import java.io.OutputStream;

    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PiePlot;
    import org.jfree.chart.title.LegendTitle;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.general.DefaultPieDataset;

    public class JFreeChartTest3
    {
        public static void main(String[] args) throws Exception
        {
            JFreeChart chart=ChartFactory.createPieChart("某公司人员组织数据图",getDataset(),true,true,false);
            chart.setTitle(new TextTitle("某公司组织结构图",new Font("宋体",Font.BOLD+Font.ITALIC,20)));
            
             LegendTitle legend=chart.getLegend(0);//设置Legend
             legend.setItemFont(new Font("宋体",Font.BOLD,14));
             PiePlot plot=(PiePlot) chart.getPlot();//设置Plot
             plot.setLabelFont(new Font("隶书",Font.BOLD,16));
             
            OutputStream os = new FileOutputStream("company.jpeg");//图片是文件格式的,故要用到FileOutputStream用来输出。
            ChartUtilities.writeChartAsJPEG(os, chart, 1000, 800);
            //使用一个面向application的工具类,将chart转换成JPEG格式的图片。第3个参数是宽度,第4个参数是高度。
            
            os.close();//关闭输出流
        }

        private static DefaultPieDataset getDataset()
        {
            DefaultPieDataset dpd=new DefaultPieDataset(); //建立一个默认的饼图
            dpd.setValue("管理人员", 25);  //输入数据
            dpd.setValue("市场人员", 25);
            dpd.setValue("开发人员", 45);
            dpd.setValue("其他人员", 10);
            return dpd;
        }
    }

    运行之后,刷新文件:

    示例四,JSP页面中显示JFreeChart生成的图

    文件结构图:

    配置servlet:

        <servlet>
            <servlet-name>DisplayChart</servlet-name>
            <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>DisplayChart</servlet-name>
            <url-pattern>/DisplayChart</url-pattern>
        </servlet-mapping>

    index.jsp 代码:

    <%@ page language="java" contentType="text/html; charset=GB18030"
        pageEncoding="GB18030"%>
        
    <%@ page import="org.jfree.data.general.DefaultPieDataset,org.jfree.chart.ChartFactory
    ,org.jfree.chart.JFreeChart,org.jfree.chart.servlet.*" %>
        
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
    <title>Insert title here</title>
    </head>
    <body>
    <%

        DefaultPieDataset dpd = new DefaultPieDataset();
        
        dpd.setValue("管理人员", 25);
        dpd.setValue("市场人员", 25);
        dpd.setValue("开发人员", 45);
        dpd.setValue("其他人员", 10);
        
        JFreeChart chart = ChartFactory.createPieChart("某公司组织结构图",dpd, true, false, false);
        
        String fileName = ServletUtilities.saveChartAsPNG(chart,800,600,session);
        //ServletUtilities是面向web开发的工具类,返回一个字符串文件名,文件名自动生成,生成好的图片会自动放在服务器(tomcat)的临时文件下(temp)
        
        String url = request.getContextPath() + "/DisplayChart?filename=" + fileName;
        //根据文件名去临时目录下寻找该图片,这里的/DisplayChart路径要与配置文件里用户自定义的<url-pattern>一致

    %>
    <img src="<%= url %>" width="800" height="600">
    </body>
    </html>

    运行结果:

    Ref:

    http://www.open-open.com/lib/view/open1365997415828.html (入门示例)

    http://www.cnblogs.com/xingyun/archive/2012/02/05/2339237.html (入门示例)

    http://www.jfree.org/jfreechart/ (下载代码和jar)

    http://sourceforge.net/projects/jfreechart/files/ (下载代码和jar)

    http://langhua9527.iteye.com/blog/395244 (设置字体,颜色,等等)

  • 相关阅读:
    【luogu3768】简单的数学题 欧拉函数(欧拉反演)+杜教筛
    【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并
    【bzoj5073】[Lydsy1710月赛]小A的咒语 后缀数组+倍增RMQ+贪心+dp
    【bzoj4596】[Shoi2016]黑暗前的幻想乡 容斥原理+矩阵树定理
    窗体的呈现、用户控件的呈现
    WPF和js交互 WebBrowser数据交互
    字符串string 转换为 Base64String
    静态资源加载过程及时序
    if else 与 if else if 的区分与使用总结
    复杂耗时逻辑处理——子线程
  • 原文地址:https://www.cnblogs.com/emanlee/p/4230105.html
Copyright © 2020-2023  润新知