• jfreechart在jsp中画图方式


    这个问题一直困扰我好久,今天算是稍微找到一点解决思路了,在网上搜了好多列子,大部分的都是用servlet来实现画图,偶然找到一个列子用的是org.jfree.chart.servlet.ServletUtilities,来解决图片从后台传到前台的方法,下面是源码。

    1.在MyEclipse中创建一个名为jfreechart的Web工程。将下载的JFreeChart1.0.6下lib目录下的jar包拷贝到WebRoot/WEB-INF/lib目录下。另外还需要在web.xml文件中增加如下配置:

        <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>

    2.在WebRoot目录下建立bar目录,用来存放柱状图的各个例子。首先在该目录下建立一个sample1.jsp文件,让我们来看一个简单的柱状图的例子,修改后的sample1.jsp的内容如下所示:

    <%@ page contentType="text/html;charset=GBK"%>
    <%@ page import="org.jfree.chart.ChartFactory,
            org.jfree.chart.JFreeChart,
            org.jfree.chart.plot.PlotOrientation,
            org.jfree.chart.servlet.ServletUtilities,
            org.jfree.data.category.DefaultCategoryDataset,        
            java.awt.Font,  
            org.jfree.chart.ChartFrame,  
            org.jfree.chart.StandardChartTheme, 
            org.jfree.chart.axis.CategoryAxis,  
            org.jfree.chart.labels.StandardCategoryItemLabelGenerator,  
            org.jfree.chart.plot.CategoryPlot,      
            org.jfree.chart.renderer.category.BarRenderer, 
            org.jfree.data.category.CategoryDataset,  
            java.io.FileOutputStream,
            java.io.IOException,
            java.io.PrintWriter,
            org.jfree.chart.ChartUtilities,
            org.jfree.chart.axis.ValueAxis,
            org.jfree.chart.title.LegendTitle,
            org.jfree.chart.title.TextTitle"
    %>
    <%
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(610, "广州", "猪肉");
        dataset.addValue(220, "广州", "牛肉");
        dataset.addValue(530, "广州", "鸡肉");
        dataset.addValue(340, "广州", "鱼肉");
        
        JFreeChart mBarChart = ChartFactory.createBarChart3D("肉类销量统计图",
        "肉类",
        "销量",
        dataset,
        PlotOrientation.VERTICAL,
        false,
        false,
        false);    
        
                    //图表标题设置  
                TextTitle mTextTitle = mBarChart.getTitle();  
                mTextTitle.setFont(new Font("黑体", Font.BOLD, 20));  
                //mBarChart.setTitle(new TextTitle("学校人员分布图",new Font("黑体", Font.BOLD, 20)));  
                //图表图例设置  
                LegendTitle mLegend = mBarChart.getLegend();  
                if(mLegend != null)  
                    mLegend.setItemFont(new Font("宋体", Font.CENTER_BASELINE, 15));  
                //mBarChart.getLegend().setItemFont(new Font("宋体", Font.CENTER_BASELINE, 15));  
                //设置柱状图轴  
                CategoryPlot mPlot = (CategoryPlot)mBarChart.getPlot();  
                  
                //x轴  
                CategoryAxis mDomainAxis = mPlot.getDomainAxis();  
                //设置x轴标题的字体  
                mDomainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 15));  
                //设置x轴坐标字体  
                mDomainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 15));  
                //y轴  
                ValueAxis mValueAxis = mPlot.getRangeAxis();  
                //设置y轴标题字体  
                mValueAxis.setLabelFont(new Font("宋体", Font.PLAIN, 15));  
                //设置y轴坐标字体  
                mValueAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 15));  
                //柱体显示数值  
                BarRenderer mRenderer= new BarRenderer();  
                mRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());  
                mRenderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 10));  
                mRenderer.setItemLabelsVisible(true);  
                mPlot.setRenderer(mRenderer);  
        
                String filename = ServletUtilities.saveChartAsPNG(mBarChart, 800, 400, null, session);
                String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
    %>
    <img src="<%= graphURL %>" width=800 height=400 border=0 usemap="#<%= filename %>">

    浏览器端输入http://127.0.0.1:8080/jfreechart/sample1.jsp即可看到输出的图片。

  • 相关阅读:
    CDH6.2安装之离线方式
    impala
    Oracle
    性能调优之Mapping
    Informatica
    性能瓶颈之System
    性能瓶颈之Session
    本地Oracle客户端11g升级12c导致PowerCenter无法连接ODBC数据源
    性能瓶颈之Mapping
    性能瓶颈之Source
  • 原文地址:https://www.cnblogs.com/yuhuameng/p/3979608.html
Copyright © 2020-2023  润新知