posts - 32,  comments - 3,  trackbacks - 0

          最近用到了JFreeChart,現將實例代碼貼出來,大家可以參考一下,代碼中如有錯誤或可以改進的地方,還請大家指正。

          通過下面的代碼,可以很清晰地看出JFreeChart的結構,核心即為chart, plot, XXXAxis, renderer,了解了它們的常用方法后,
          會發現其實JFreeChart使用起來是很簡單方便的。廢話不多說了,還是直接看示例吧。
           
          1.柱狀圖

            1/**
            2     * 生成圖像文件
            3     * 
            4     * @param session
            5     *            httpsession
            6     * @param w
            7     *            生成的圖的寬度
            8     * @param h
            9     *            生成的圖的高度
           10     * @return 
           11     *              生成的圖像文件的路徑
           12     */

           13    public String createBarChart(HttpSession session, 
           14                                 String title, // 圖片標題
           15                                 int w, // 圖片寬度
           16                                 int h, // 圖片高度
           17                                 CategoryDataset dataset //用于出圖的數據集
           18                                 ) {
           19        
           20        //通過ChartFactory的靜態方法獲取JFreeChart對象
           21        JFreeChart chart = ChartFactory.createBarChart(title,      //圖片標題
           22                                                         null,     //橫坐標標題
           23                                                         null,     //縱坐標標題
           24                                                         dataset,  //用于出圖的數據集
           25                                                         PlotOrientation.VERTICAL,  //柱子方向
           26                                                         true,     //是否包含Legend
           27                                                         false,    //是否包含tooltips
           28                                                         false);   //是否包含urls
           29        chart.setBackgroundPaint(Color.WHITE);
           30
           31        // 設置標題字體
           32        chart.getTitle().setFont(new Font("宋體", Font.CENTER_BASELINE, 12));
           33
           34        CategoryPlot plot = chart.getCategoryPlot();
           35
           36        /*//沒有數據時顯示的消息
           37        plot.setNoDataMessage("數據還未錄入!");
           38        plot.setNoDataMessageFont(new Font("宋體", Font.CENTER_BASELINE, 15));*/

           39        
           40        //橫坐標設置
           41        CategoryAxis domainAxis = plot.getDomainAxis();
           42        //設置橫坐標上顯示各個業務子項的字體 
           43        domainAxis.setTickLabelFont(new Font("宋體", Font.PLAIN, 9));
           44        plot.setDomainAxis(domainAxis);
           45
           46        //縱坐標設置
           47        ValueAxis rangeAxis = (ValueAxis)plot.getRangeAxis();
           48        // 設置最高的一個 Item 與圖片頂端的距離
           49        rangeAxis.setUpperMargin(0.15);
           50        // 設置最低的一個 Item 與圖片底端的距離
           51        rangeAxis.setLowerMargin(0.15);
           52        plot.setRangeAxis(rangeAxis);
           53
           54        BarRenderer renderer = new BarRenderer();
           55        renderer.setBaseOutlinePaint(Color.BLACK);
           56        /*// 設置圖上的文字
           57        renderer.setSeriesItemLabelFont(0, font);
           58        renderer.setSeriesItemLabelFont(1, font);*/

           59        // 設置legend的字體
           60        renderer.setBaseLegendTextFont(new Font("宋體", Font.LAYOUT_RIGHT_TO_LEFT, 10));
           61        // 設置 Wall 的顏色
           62        //renderer.setWallPaint(Color.gray);
           63        // 設置每種柱的顏色
           64        renderer.setSeriesPaint(0new Color(133,210,38));
           65        renderer.setSeriesPaint(1new Color(0,131,249));
           66        //設置柱的 Outline 顏色
           67        renderer.setSeriesOutlinePaint(0, Color.BLACK);
           68        renderer.setSeriesOutlinePaint(1, Color.BLACK);
           69        // 設置每個平行柱的之間距離
           70        renderer.setItemMargin(0.03);
           71        
           72        // 顯示每個柱的數值,并修改該數值的字體屬性
           73        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
           74        //注意:此句很關鍵,若無此句,那數字的顯示會被覆蓋,給人數字沒有顯示出來的問題 
           75        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( 
           76        ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); 
           77        // 設置柱形圖上的文字偏離值 
           78        renderer.setItemLabelAnchorOffset(10D);
           79        renderer.setBaseItemLabelsVisible(true);
           80        renderer.setBaseItemLabelFont(new Font("Times New Roman", Font.PLAIN, 9));
           81        
           82        plot.setRenderer(renderer);
           83
           84        // 設置柱的透明度
           85        plot.setForegroundAlpha(0.6f);
           86        // 設置橫坐標和縱坐標的顯示位置
           87        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
           88        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
           89
           90        // 獲取圖片路徑
           91        // 圖片存于臨時文件夾下
           92        String filename = null;
           93        try {
           94            filename = ServletUtilities.saveChartAsPNG(chart, w, h, null,
           95                    session);
           96        }
           catch (IOException e) {
           97            System.out.println("Exception - " + e.toString());
           98            e.printStackTrace(System.out);
           99            filename = "public_error_500x300.png";
          100        }

          101        return filename;
          102    }


          2.曲線圖
           1    /**
           2     * 生成圖像文件
           3     * 
           4     * @param session
           5     *            httpsession
           6     * @param data1
           7     *            IntervalXYDataset
           8     * @param w
           9     *            生成的圖的寬度
          10     * @param h
          11     *            生成的圖的高度
          12     * @return
          13     *                  生成的圖像文件的路徑
          14     */

          15    public String createStepLineChart(
          16            HttpSession session,
          17            String title, // 圖片標題
          18            IntervalXYDataset data1,//數據集
          19            int w, // 圖片寬度
          20            int h // 圖片高度
          21            ) {
          22
          23        /*
          24         * 任何類型的圖表的最終表現形式都是在JFreeChart對象進行一些屬性的定制。
          25         * JFreeChart引擎本身提供了一個工廠類用于創建不同類型的圖表對象
          26         */

          27        JFreeChart chart = ChartFactory.createXYStepChart(title, // chart title
          28                                                          null
          29                                                          null
          30                                                          data1, // data
          31                                                          PlotOrientation.VERTICAL, 
          32                                                          true// include legend
          33                                                          false
          34                                                          false);
          35
          36        // 設置背景顏色為白色
          37        chart.setBackgroundPaint(Color.white);
          38
          39        // 設置標題字體
          40        chart.getTitle().setFont(new Font("宋體", Font.CENTER_BASELINE, 12));
          41        XYPlot plot = chart.getXYPlot();
          42        
          43        plot.setBackgroundPaint(Color.lightGray);
          44        plot.setDomainGridlinePaint(Color.white);
          45        plot.setRangeGridlinePaint(Color.white);
          46
          47        DateAxis domainAxis = new DateAxis();
          48        domainAxis.setDateFormatOverride(new SimpleDateFormat("MM-dd"));
          49        domainAxis.setTickMarkPosition(DateTickMarkPosition.START);
          50        domainAxis.setLowerMargin(0);
          51        domainAxis.setUpperMargin(0);
          52        plot.setDomainAxis(domainAxis);
          53
          54        NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
          55        // 設置最高的一個 Item 與圖片頂端的距離
          56        rangeAxis.setUpperMargin(0.15);
          57        // 設置最低的一個 Item 與圖片底端的距離
          58        rangeAxis.setLowerMargin(0.15);
          59        plot.setRangeAxis(rangeAxis);
          60
          61        /*
          62         * XXXRender:負責如何顯示一個圖表對象
          63         */

          64        XYStepRenderer renderer = (XYStepRenderer)plot.getRenderer();
          65        /*
          66         * 設置線條的顏色
          67         */

          68        renderer.setSeriesPaint(0new Color(0,150,240));
          69        renderer.setSeriesPaint(1new Color(244,109,19));
          70
          71        // 設置圖上的文字字體
          72        Font font = new Font("宋體", Font.LAYOUT_RIGHT_TO_LEFT, 10);
          73        renderer.setSeriesItemLabelFont(0, font);
          74        renderer.setSeriesItemLabelFont(1, font);
          75        // 設置legend的字體
          76        renderer.setBaseLegendTextFont(font);
          77        renderer.setBaseItemLabelFont(font);
          78
          79        plot.setRenderer(renderer);
          80
          81        String filename = null;
          82        try {
          83            filename = ServletUtilities.saveChartAsPNG(chart, w, h, null,
          84                    session);
          85        }
           catch (Exception e) {
          86            System.out.println("Exception - " + e.toString());
          87            e.printStackTrace(System.out);
          88            filename = "public_error_500x300.png";
          89        }

          90        return filename;
          91    }


          另外,因為JFreeChart將生成的圖片置于臨時文件夾下,為防垃圾文件泛濫,可以寫一個監聽類,當session失效時刪除臨時圖片文件
           1public class DelTempListener implements HttpSessionListener {
           2
           3    public void sessionCreated(HttpSessionEvent se) {
           4        System.out.println("srart____________");
           5        delete(se);
           6        System.out.println("srart___over");
           7    }

           8
           9    public void sessionDestroyed(HttpSessionEvent se) {
          10        System.out.println("Destroyed____________");
          11        delete(se);
          12        System.out.println("Destroyed__over");
          13    }

          14    private void delete(HttpSessionEvent se){
          15        ChartDeleter deleter = (ChartDeleter)se.getSession().getAttribute("JFreeChart_Deleter");
          16        se.getSession().removeAttribute("JFreeChart_Deleter");
          17        se.getSession().setAttribute("JFreeChart_Deleter", deleter);
          18    }

          19}
          posted on 2010-07-25 22:44 donghang73 閱讀(4480) 評論(1)  編輯  收藏 所屬分類: 學習筆記
          主站蜘蛛池模板: 舞钢市| 房产| 佛坪县| 莱阳市| 万安县| 噶尔县| 肥城市| 大厂| 大石桥市| 积石山| 桦川县| 沂水县| 遵义市| 元朗区| 贵南县| 克拉玛依市| 英德市| 德江县| 平顶山市| 湘阴县| 仙游县| 岢岚县| 云龙县| 江阴市| 尼木县| 固始县| 曲阳县| 惠来县| 梨树县| 临泽县| 鄢陵县| 九龙城区| 东莞市| 桃园市| 榆树市| 宜兴市| 塔河县| 淳安县| 漳浦县| 神池县| 新余市|