jfreechart在jsp中畫圖方式

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   java   ar   檔案   div   

這個問題一直困擾我好久,今天算是稍微找到一點解決思路了,在網上搜了好多列子,大部分的都是用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即可看到輸出的圖片。

 

jfreechart在jsp中畫圖方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.