JFreeChart實(shí)例,帶詳細(xì)注釋
系統(tǒng)
2110 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(
0
,?
new
?Color(
133
,
210
,
38
));
?65
????????renderer.setSeriesPaint(
1
,?
new
?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
?????????*?任何類型的圖表的最終表現(xiàn)形式都是在JFreeChart對(duì)象進(jìn)行一些屬性的定制。
25
?????????*?JFreeChart引擎本身提供了一個(gè)工廠類用于創(chuàng)建不同類型的圖表對(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(
0
,?
new
?Color(
0
,
150
,
240
));
69
????????renderer.setSeriesPaint(
1
,?
new
?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?
=
?
<span style=
分享到:
JFreeChart實(shí)例,帶詳細(xì)注釋
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
評(píng)論