• JFreeChart插件使用


    以java project为例,首先需要导入需要的jar包:jcommon-1.0.23.jar, jfreechart-1.0.19.jar.

    画饼状图示例:

    package com.it.jfchart;
    
    import java.awt.Font;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    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;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
    /**
     * jFreeChart画饼状图
     * ApplicationFrame是JFreeChart中写好的一个类,使用它就可用一个单独的JFrame来显示图表
     */
    public class Mypie extends ApplicationFrame {
    	// 字体设置
    	private static final Font font = new Font("simsun",Font.ITALIC,22);
    	
    	public Mypie(String title) {
    		super(title);
    		setContentPane(new ChartPanel(getChart()));
    	}
    
    	/*
    	 * 封装画图所需的数据集对象DefaultPieDataset
    	 */
    	private static DefaultPieDataset getDataset(){
    		DefaultPieDataset dataset = new DefaultPieDataset();
    		dataset.setValue("本科生", 50);
    		dataset.setValue("研究生", 30);
    		dataset.setValue("博士生", 20);
    		return dataset;
    	}
    	
    	/*
    	 * 返回JFreeChart对象
    	 * JFreeChart对中文支持不好, 所有涉及汉字的部分,必须重新设置字体,否则显示乱码
    	 */
    	public JFreeChart getChart(){
    		final DefaultPieDataset dataset = getDataset();
    		// 有标题,无悬浮提示,无连接(true,false,false)
    		JFreeChart chart = ChartFactory.createPieChart("XX公司员工学历比例图", dataset,true,false,false);
    		
    		/*
    		 * 重新设置字体
    		 */
    		// 重新设置标题字体
    		chart.setTitle(new TextTitle("XX公司员工学历比例图",font));
    		// 重新设置图例字体
    		LegendTitle legend = chart.getLegend();
    		legend.setItemFont(font);
    		// 重新设置统计表图像字体
    		PiePlot plot = (PiePlot) chart.getPlot();
    		plot.setBackgroundAlpha(0.9f);
    		plot.setLabelFont(font);
    		
    		return chart;
    	}
    	
    	// 测试方法
    	public static void main(String[] args){
    		Mypie demo = new Mypie("XX公司员工学历比例图");
    		demo.pack();
    		RefineryUtilities.centerFrameOnScreen(demo);
    		demo.setVisible(true);
    	}
    	
    }

    画柱状图示例:

    package com.it.jfchart;
    
    import java.awt.Font;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.title.LegendTitle;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
    /**
     * JFreeChart画柱状图
     */
    public class Mybar extends ApplicationFrame {
    	// 字体设置
    	private static final Font font = new Font("simsun",Font.ITALIC,22);
    	
    	public Mybar(String title) {
    		super(title);
    		setContentPane(new ChartPanel(getChart()));
    	}
    	
    	/*
    	 * 封装柱状图所需的数据集对象DefaultCategoryDataset
    	 */
    	private static DefaultCategoryDataset getDataset(){
    		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    		dataset.setValue(50,"本科生","本科生");
    		dataset.setValue(40,"研究生","研究生");
    		dataset.setValue(30,"博士生","博士生");
    		return dataset;
    	}
    	
    	/*
    	 * 返回JFreeChart对象
    	 * JFreeChart对中文支持不好, 所有涉及汉字的部分,必须重新设置字体,否则显示乱码
    	 */
    	public JFreeChart getChart(){
    		final DefaultCategoryDataset dataset = getDataset();
    		// 有标题,无悬浮提示,无连接(true,false,false)
    		JFreeChart chart = ChartFactory.createBarChart3D("XX公司员工学历比例图", "学历", "人数", dataset,PlotOrientation.VERTICAL,true,false,false);
    		
    		/*
    		 * 重新设置字体
    		 */
    		// 重新设置标题字体
    		chart.setTitle(new TextTitle("XX公司员工学历比例图",font));
    		// 重新设置图例字体
    		LegendTitle legend = chart.getLegend();
    		legend.setItemFont(font);
    		// 设置X轴坐标上的文字的字体
    		chart.getCategoryPlot().getDomainAxis().setTickLabelFont(font);
    		// 设置X轴坐标标题的字体
    		chart.getCategoryPlot().getDomainAxis().setLabelFont(font);
    		// 设置Y轴坐标上的文字的字体
    		chart.getCategoryPlot().getRangeAxis().setTickLabelFont(font);
    		// 设置Y轴坐标标题的字体
    		chart.getCategoryPlot().getRangeAxis().setLabelFont(font);
    		
    		return chart;
    	}
    
    	// 测试方法
    	public static void main(String[] args){
    		Mybar demo = new Mybar("XX公司员工学历比例图");
    		demo.pack();
    		RefineryUtilities.centerFrameOnScreen(demo);
    		demo.setVisible(true);
    	}
    	
    }



    以javaweb project为例,除了上述两个jar包,还需要struts2-jfreechart-plugin-2.3.24.1.jar.

    struts.xml 需要定义chart结果类型:

    <struts>
    	<package name="default" namespace="/" extends="struts-default">
    		<!-- 定义chart结果类型 -->
    		<result-types>
    			 <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"/>	
    		</result-types>
    ...
    
    result配置如下(饼状图):

    		<!-- 客户贡献统计图 -->
    		<action name="contriChart" class="statisreportAction" method="docontriChart">
    			<result type="chart">
    				<param name="chart">chart</param>
    				<param name="height">400</param>
    				<param name="width">700</param>
    			</result>
    		</action>

    具体的Action代码如下:

        // 必须加上getChart()方法
        public JFreeChart getChart() {
            return chart;
        }	
    
            // 客户贡献统计图
    	public String docontriChart(){
    		ordHistoryList = srBiz.orderHistoryList();	//获取订单数据
    		DefaultPieDataset dataset = new DefaultPieDataset();
    		// 给dataset赋值
    		for (Orderhistory history : ordHistoryList) {
    			dataset.setValue(history.getCustomerinfo().getCName(), history.getOTotalaccount());
    		}
    		// 实例化chart
    		chart = ChartFactory.createPieChart("客户贡献统计图", dataset, true, false, false);
    		// 设置字体
    		chart.setTitle(new TextTitle("客户贡献统计图",font));
    		LegendTitle legend = chart.getLegend();
    		legend.setItemFont(font);
    		PiePlot plot = (PiePlot) chart.getPlot();
    		plot.setBackgroundAlpha(0.9f);
    		plot.setLabelFont(font);
    		
    		return SUCCESS;
    	}

    JSP页面显示(通过click事件来显示统计图):

    <%--图片src中写入action名字,执行生成统计图操作 --%>
    <div><img id="chart" alt="" src="contriChart" style="display:none; "></div>

    js函数:

    <script type="text/javascript">
    // 显示统计图
    function showChart(){
    	var imgId=document.getElementById("chart");
    	if(imgId.style.display=="block"){
    		imgId.style.display="none";
    	}
    	else{
    		imgId.style.display="block";
    	}
    }


  • 相关阅读:
    各种排序算法的时间复杂度
    svn版本管理系统出现的问题解决办法
    算法时间复杂度
    js处理时间戳显示的问题
    cache缓存的BUG
    使用phpstorm提交svn代码版本管理系统遇到的问题解决办法
    20161101.20161115这两周的开发总结
    mac 上安装 redis
    终极 shell zsh
    在 mac 上利用 homebrew 安装软件
  • 原文地址:https://www.cnblogs.com/archermeng/p/7537388.html
Copyright © 2020-2023  润新知