日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

JFreeChart實例,帶詳細注釋

系統 2182 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( 0 ,? new ?Color( 133 , 210 , 38 ));
?65 ????????renderer.setSeriesPaint( 1 ,? new ?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( 0 ,? new ?Color( 0 , 150 , 240 ));
69 ????????renderer.setSeriesPaint( 1 ,? new ?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? = ? <span style=
分享到:
評論

JFreeChart實例,帶詳細注釋


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 阳曲县| 伊金霍洛旗| 安溪县| 江北区| 吴川市| 松潘县| 阿克苏市| 咸丰县| 青神县| 枝江市| 嘉禾县| 淮南市| 平遥县| 陇川县| 通河县| 浦城县| 西和县| 南漳县| 高台县| 广德县| 衡阳市| 丰台区| 三河市| 寻乌县| 徐州市| 上蔡县| 蒙山县| 新源县| 图木舒克市| 海宁市| 庄河市| 连江县| 大安市| 清苑县| 铁岭县| 定州市| 黄梅县| 星座| 来宾市| 桦甸市| 资阳市|