JFreeChart 入門

要使用JFreeChart ,首先要將JFreeChart-版本號.jar,jcommon-版本號.jar放入CLASSPATH,對於web工程,則放入WEB-INF/lib下,並在CLASSPATH加入這兩個jar包。web

JFreeChart的使用分爲三個步驟,一,準備數據集,即要在圖形中顯示的數據;2,經過ChartFactory產生JFreeChart對象;3,對產生的JFreeChart對象進行修飾。如產生餅圖的示例代碼以下:apache

   // 數據準備網絡

   DefaultPieDataset data = new DefaultPieDataset();
        data.setValue("Java", new Double(43.2));
        data.setValue("Visual Basic", new Double(1.0));
        data.setValue("C/C++", new Double(17.5));
        data.setValue("Python", new Double(40.0));

        // 生成JFreeChart對象
        JFreeChart chart = ChartFactory.createPieChart("", data, true, true,
                false);

        // 對圖形進行修飾ide

         // 增長標題和副標題,要對字體進行設置,否則會亂碼         Font font = new Font("宋體", Font.BOLD, 16);
        TextTitle title = new TextTitle("主標題", font);
        TextTitle subtitle = new TextTitle("副標題", new Font("黑體", Font.BOLD, 12));
        chart.addSubtitle(subtitle);
        chart.setTitle(title);字體

        // 設置圖中標籤的字體
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("宋體", Font.BOLD, 16));
        this

        // 設置圖片說明/圖例的字體
        chart.getLegend().setItemFont(new Font("宋體", Font.BOLD, 16));spa

        //將圖片寫入輸出流,能夠是文件、網絡輸出流xml

   OutputStream out = new FileOutputStream(new File("E://test.jpg"));對象

        ChartUtilities.writeChartAsJPEG(out, chart, 400, 300);圖片

在servlet中使用JFreeChart

       在servlet中使用JFreeChart,只需將生成JFreeChart對象經過ChartUtilities寫入到HttpServletResponse便可。完整的示例代碼以下:

   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

       // 數據準備

   DefaultPieDataset data = new DefaultPieDataset();
        data.setValue("Java", new Double(43.2));
        data.setValue("Visual Basic", new Double(1.0));
        data.setValue("C/C++", new Double(17.5));
        data.setValue("Python", new Double(40.0));

        // 生成JFreeChart對象
        JFreeChart chart = ChartFactory.createPieChart("", data, true, true,
                false);

        // 對圖形進行修飾

         // 增長標題和副標題,要對字體進行設置,否則會亂碼        

         Font font = new Font("宋體", Font.BOLD, 16);
        TextTitle title = new TextTitle("主標題", font);
        TextTitle subtitle = new TextTitle("副標題", new Font("黑體", Font.BOLD, 12));
        chart.addSubtitle(subtitle);
        chart.setTitle(title);

        // 設置圖中標籤的字體
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("宋體", Font.BOLD, 16));
       

        // 設置圖片說明/圖例的字體
        chart.getLegend().setItemFont(new Font("宋體", Font.BOLD, 16));

        //將圖片寫入輸出流

   ChartUtilities.writeChartAsJPEG(resp.getOutputStream(), chart, 400, 300);

  }

JFreeChart與struts2集成

     JFreeChart與struts2除了要將JFreeChart-版本號.jar,jcommon-版本號.jar放入lib和CLASSPATH外,還要將struts2-jfreechart-plugin-版本號.jar加入lib和CLASSPATH.

     編寫Action,示例代碼以下:

     public class JFreeChartAction extends ActionSupport {

    /**
     *
     */
    private static final long serialVersionUID = 5602329386267968880L;

    // 這個是寫死的,名字必須爲chart
    private JFreeChart chart;

    public JFreeChart getChart() {
        return chart;
    }

    public void setChart(JFreeChart chart) {
        this.chart = chart;
    }

    @Override
    public String execute() throws Exception {
       // 數據準備

   DefaultPieDataset data = new DefaultPieDataset();
        data.setValue("Java", new Double(43.2));
        data.setValue("Visual Basic", new Double(1.0));
        data.setValue("C/C++", new Double(17.5));
        data.setValue("Python", new Double(40.0));

        // 生成JFreeChart對象
        JFreeChart chart = ChartFactory.createPieChart("", data, true, true,
                false);

        // 對圖形進行修飾

         // 增長標題和副標題,要對字體進行設置,否則會亂碼        

         Font font = new Font("宋體", Font.BOLD, 16);
        TextTitle title = new TextTitle("主標題", font);
        TextTitle subtitle = new TextTitle("副標題", new Font("黑體", Font.BOLD, 12));
        chart.addSubtitle(subtitle);
        chart.setTitle(title);

        // 設置圖中標籤的字體
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("宋體", Font.BOLD, 16));
       

        // 設置圖片說明/圖例的字體
        chart.getLegend().setItemFont(new Font("宋體", Font.BOLD, 16));
        return SUCCESS;
    }

     在struts.xml中增長以下配置

    <package name="jFreeChartDemonstration" extends="struts-default"
        namespace="/jfreechart">
        <result-types>
            <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
        </result-types>
        <action name="JFreeChartAction" class="org.test.jfreechart.action.JFreeChartAction">
              <result type="chart">

                   // 圖片的寬和高
                   <param name="width">400</param>
                   <param name="height">300</param>

                    // 這裏支持png,和jpg兩種

                   <param name="type">jpg</param>
            </result>
        </action>
    </package>

    也能夠將action和result-types放到其餘package中,這個根據具體項目狀況來定。

   而後就能夠經過地址http://127.0.0.1:8080/{webpath}/jfreechart/JFreeChartAction.action訪問到生成的圖片了。

相關文章
相關標籤/搜索