<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 32,  comments - 3,  trackbacks - 0

    最近用到了JFreeChart,現(xiàn)將實(shí)例代碼貼出來(lái),大家可以參考一下,代碼中如有錯(cuò)誤或可以改進(jìn)的地方,還請(qǐng)大家指正。

    通過(guò)下面的代碼,可以很清晰地看出JFreeChart的結(jié)構(gòu),核心即為chart, plot, XXXAxis, renderer,了解了它們的常用方法后,
    會(huì)發(fā)現(xiàn)其實(shí)JFreeChart使用起來(lái)是很簡(jiǎn)單方便的。廢話不多說(shuō)了,還是直接看示例吧。
     
    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, // 圖片標(biāo)題
     15                                 int w, // 圖片寬度
     16                                 int h, // 圖片高度
     17                                 CategoryDataset dataset //用于出圖的數(shù)據(jù)集
     18                                 ) {
     19        
     20        //通過(guò)ChartFactory的靜態(tài)方法獲取JFreeChart對(duì)象
     21        JFreeChart chart = ChartFactory.createBarChart(title,      //圖片標(biāo)題
     22                                                         null,     //橫坐標(biāo)標(biāo)題
     23                                                         null,     //縱坐標(biāo)標(biāo)題
     24                                                         dataset,  //用于出圖的數(shù)據(jù)集
     25                                                         PlotOrientation.VERTICAL,  //柱子方向
     26                                                         true,     //是否包含Legend
     27                                                         false,    //是否包含tooltips
     28                                                         false);   //是否包含urls
     29        chart.setBackgroundPaint(Color.WHITE);
     30
     31        // 設(shè)置標(biāo)題字體
     32        chart.getTitle().setFont(new Font("宋體", Font.CENTER_BASELINE, 12));
     33
     34        CategoryPlot plot = chart.getCategoryPlot();
     35
     36        /*//沒(méi)有數(shù)據(jù)時(shí)顯示的消息
     37        plot.setNoDataMessage("數(shù)據(jù)還未錄入!");
     38        plot.setNoDataMessageFont(new Font("宋體", Font.CENTER_BASELINE, 15));*/

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

     59        // 設(shè)置legend的字體
     60        renderer.setBaseLegendTextFont(new Font("宋體", Font.LAYOUT_RIGHT_TO_LEFT, 10));
     61        // 設(shè)置 Wall 的顏色
     62        //renderer.setWallPaint(Color.gray);
     63        // 設(shè)置每種柱的顏色
     64        renderer.setSeriesPaint(0new Color(133,210,38));
     65        renderer.setSeriesPaint(1new Color(0,131,249));
     66        //設(shè)置柱的 Outline 顏色
     67        renderer.setSeriesOutlinePaint(0, Color.BLACK);
     68        renderer.setSeriesOutlinePaint(1, Color.BLACK);
     69        // 設(shè)置每個(gè)平行柱的之間距離
     70        renderer.setItemMargin(0.03);
     71        
     72        // 顯示每個(gè)柱的數(shù)值,并修改該數(shù)值的字體屬性
     73        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
     74        //注意:此句很關(guān)鍵,若無(wú)此句,那數(shù)字的顯示會(huì)被覆蓋,給人數(shù)字沒(méi)有顯示出來(lái)的問(wèn)題 
     75        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( 
     76        ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); 
     77        // 設(shè)置柱形圖上的文字偏離值 
     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        // 設(shè)置柱的透明度
     85        plot.setForegroundAlpha(0.6f);
     86        // 設(shè)置橫坐標(biāo)和縱坐標(biāo)的顯示位置
     87        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
     88        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
     89
     90        // 獲取圖片路徑
     91        // 圖片存于臨時(shí)文件夾下
     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, // 圖片標(biāo)題
    18            IntervalXYDataset data1,//數(shù)據(jù)集
    19            int w, // 圖片寬度
    20            int h // 圖片高度
    21            ) {
    22
    23        /*
    24         * 任何類(lèi)型的圖表的最終表現(xiàn)形式都是在JFreeChart對(duì)象進(jìn)行一些屬性的定制。
    25         * JFreeChart引擎本身提供了一個(gè)工廠類(lèi)用于創(chuàng)建不同類(lèi)型的圖表對(duì)象
    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        // 設(shè)置背景顏色為白色
    37        chart.setBackgroundPaint(Color.white);
    38
    39        // 設(shè)置標(biāo)題字體
    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        // 設(shè)置最高的一個(gè) Item 與圖片頂端的距離
    56        rangeAxis.setUpperMargin(0.15);
    57        // 設(shè)置最低的一個(gè) Item 與圖片底端的距離
    58        rangeAxis.setLowerMargin(0.15);
    59        plot.setRangeAxis(rangeAxis);
    60
    61        /*
    62         * XXXRender:負(fù)責(zé)如何顯示一個(gè)圖表對(duì)象
    63         */

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

    68        renderer.setSeriesPaint(0new Color(0,150,240));
    69        renderer.setSeriesPaint(1new Color(244,109,19));
    70
    71        // 設(shè)置圖上的文字字體
    72        Font font = new Font("宋體", Font.LAYOUT_RIGHT_TO_LEFT, 10);
    73        renderer.setSeriesItemLabelFont(0, font);
    74        renderer.setSeriesItemLabelFont(1, font);
    75        // 設(shè)置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    }


    另外,因?yàn)镴FreeChart將生成的圖片置于臨時(shí)文件夾下,為防垃圾文件泛濫,可以寫(xiě)一個(gè)監(jiān)聽(tīng)類(lèi),當(dāng)session失效時(shí)刪除臨時(shí)圖片文件
     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 閱讀(4478) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): 學(xué)習(xí)筆記
    主站蜘蛛池模板: 久久久www成人免费毛片 | 久久一本岛在免费线观看2020| 91香蕉在线观看免费高清| 搡女人真爽免费视频大全| 久久久久亚洲精品天堂久久久久久 | 九九久久精品国产免费看小说| 99精品在线免费观看| 在线观看亚洲免费| 久久精品国产精品亚洲色婷婷| 亚洲av永久无码| 久久久久久一品道精品免费看| 又大又硬又爽免费视频| 久久亚洲精品人成综合网| 国产91成人精品亚洲精品| 国产大片91精品免费观看不卡| 亚洲国产日韩成人综合天堂| 亚洲国产综合精品| aaa毛片免费观看| 全免费一级毛片在线播放| 久久久综合亚洲色一区二区三区 | 亚洲国产精品无码久久98 | 成人免费视频一区二区三区| 精品亚洲综合久久中文字幕| 亚洲AV无码资源在线观看| 99精品视频在线观看免费专区| 亚洲一本大道无码av天堂| 色天使亚洲综合在线观看| 久久免费高清视频| 四虎国产精品免费久久影院| 亚洲日本在线观看网址| 国产在线国偷精品免费看| 国产精品极品美女免费观看 | 亚洲国产美女精品久久久久| 99在线免费视频| 亚洲高清成人一区二区三区| 亚洲中文字幕久久精品无码VA| 91精品国产免费久久国语蜜臀 | 亚洲一区二区在线免费观看| A毛片毛片看免费| 亚洲国产婷婷综合在线精品| 亚洲乱理伦片在线观看中字|