jfreechart 是開放源代碼站點sourceforge.net 上的一個 java 項目,它主要用來各種各樣的圖表,這些圖表包括:餅圖、柱狀圖 ( 普通柱狀圖以及堆棧柱狀圖 )、線圖、區域圖、分布圖、混合圖、甘特圖以及一些儀表盤等等。
應用jfreechart來畫圖需要兩個jar包:jfreechart.jar和jcommon.jar,下載地址。
下面是一個畫折線圖的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package yuth.jfree.demo; /** * 使用 categorydataset 數據集創建折線圖 * 當數據多時,在jpanel中無法完全看到橫坐標的值,顯示為省略號。 * 解決方法: * 方法1、將報表保存為圖片時,設置圖片的寬度足夠大(2000或3000),圖片可以顯示橫坐標值。 * 這種方法治標不治本,所以有了第2種方法 * 方法2、設置x軸上的lable讓其45度傾斜。 */ import java.awt.basicstroke; import java.awt.color; import java.awt.dimension; import java.awt.font; import java.awt.geom.ellipse2d; import java.io.file; import java.io.ioexception; import java.net.url; import java.text.decimalformat; import java.util.random; import javax.swing.imageicon; import javax.swing.jpanel; import org.jfree.chart.chartfactory; import org.jfree.chart.chartpanel; import org.jfree.chart.chartutilities; import org.jfree.chart.jfreechart; import org.jfree.chart.axis.categoryaxis; import org.jfree.chart.axis.categorylabelpositions; import org.jfree.chart.axis.numberaxis; import org.jfree.chart.labels.standardcategoryitemlabelgenerator; import org.jfree.chart.plot.categoryplot; import org.jfree.chart.plot.plotorientation; import org.jfree.chart.renderer.category.lineandshaperenderer; import org.jfree.chart.title.texttitle; import org.jfree.data.category.categorydataset; import org.jfree.data.category.defaultcategorydataset; import org.jfree.ui.applicationframe; import org.jfree.ui.horizontalalignment; import org.jfree.ui.rectangleedge; import org.jfree.ui.rectangleinsets; import org.jfree.ui.refineryutilities; public class linechartdemo1 extends applicationframe { private static final long serialversionuid = -6354350604313079793l; /* synthetic */static class class$demo$linechartdemo1; public linechartdemo1(string string) { super(string); jpanel jpanel = createdemopanel(); jpanel.setpreferredsize(new dimension(500, 270)); setcontentpane(jpanel); } /** * 如何區分不同的圖例:根據defaultcategorydataset.addvalue()的第二個參數是否相同來區分。 * 同一個圖例的數據的添加順序影響最終的顯示;不同圖例的數據的添加順序不影響最終的顯示。 * @return */ private static categorydataset createdataset() { defaultcategorydataset defaultcategorydataset = new defaultcategorydataset(); //defaultcategorydataset.addvalue()的參數解析:(數值,圖例名,橫坐標值) /* //添加數據方法1 //圖例1 defaultcategorydataset.addvalue(212.0, "first", "1001.0"); defaultcategorydataset.addvalue(504.0, "first", "1001.1"); defaultcategorydataset.addvalue(1520.0, "first", "1001.2"); //圖例2 defaultcategorydataset.addvalue(712.0, "second", "1001.0"); defaultcategorydataset.addvalue(1204.0, "second", "1001.1"); defaultcategorydataset.addvalue(1820.0, "second", "1001.2"); /*/ //* //添加數據方法2 //實驗隨機數來生成測試結果 random random = new random(12345); //圖例1,40個數據 for(int i=0;i<40;i++){ defaultcategorydataset.addvalue(random.nextint(100000), "first",string.valueof(1000+i) ); } //圖例2,40個數據 for (int i = 0; i < 40; i++) { defaultcategorydataset.addvalue(random.nextint(100000), "second", string.valueof(1000 + i)); } //*/ return defaultcategorydataset; } private static jfreechart createchart(categorydataset categorydataset) { jfreechart jfreechart = chartfactory.createlinechart( "jfreechart test",// 圖表標題 "x", // 主軸標簽(x軸) "y",// 范圍軸標簽(y軸) categorydataset, // 數據集 plotorientation.vertical,// 方向 false, // 是否包含圖例 true, // 提示信息是否顯示 false);// 是否使用urls // 改變圖表的背景顏色 jfreechart.setbackgroundpaint(color.white); categoryplot categoryplot = (categoryplot) jfreechart.getplot(); categoryplot.setbackgroundpaint(color.lightgray); categoryplot.setrangegridlinepaint(color.white); categoryplot.setrangegridlinesvisible(false); //修改范圍軸。 我們將默認刻度值 (允許顯示小數) 改成只顯示整數的刻度值。 numberaxis numberaxis = (numberaxis) categoryplot.getrangeaxis(); numberaxis.setstandardtickunits(numberaxis.createintegertickunits()); // 設置x軸上的lable讓其45度傾斜 categoryaxis domainaxis = categoryplot.getdomainaxis(); domainaxis.setcategorylabelpositions(categorylabelpositions.up_45); // 設置x軸上的lable讓其45度傾斜 domainaxis.setlowermargin(0.0); // 設置距離圖片左端距離 domainaxis.setuppermargin(0.0); // 設置距離圖片右端距離 lineandshaperenderer lineandshaperenderer = (lineandshaperenderer) categoryplot .getrenderer(); lineandshaperenderer.setshapesvisible(true); lineandshaperenderer.setdrawoutlines(true); lineandshaperenderer.setusefillpaint(true); lineandshaperenderer.setbasefillpaint(color.white); lineandshaperenderer.setseriesstroke(0, new basicstroke(3.0f)); lineandshaperenderer.setseriesoutlinestroke(0, new basicstroke(2.0f)); lineandshaperenderer.setseriesshape(0, new ellipse2d.double(-5.0, -5.0, 10.0, 10.0)); lineandshaperenderer.setitemmargin(0.4); //設置x軸每個值的間距(不起作用??) // 顯示數據值 decimalformat decimalformat1 = new decimalformat("##.##");// 數據點顯示數據值的格式 lineandshaperenderer.setbaseitemlabelgenerator(new standardcategoryitemlabelgenerator( "{2}", decimalformat1));// 設置數據項標簽的生成器 lineandshaperenderer.setbaseitemlabelsvisible(true);// 基本項標簽顯示 lineandshaperenderer.setbaseshapesfilled(true);// 在數據點顯示實心的小圖標 return jfreechart; } public static jpanel createdemopanel() { jfreechart jfreechart = createchart(createdataset()); try { chartutilities.savechartasjpeg( new file("d:/linechartdemo1.png"), //文件保存物理路徑包括路徑和文件名 // 1.0f, //圖片質量 ,0.0f~1.0f jfreechart, //圖表對象 1024, //圖像寬度 ,這個將決定圖表的橫坐標值是否能完全顯示還是顯示省略號 768); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } //圖像高度 return new chartpanel(jfreechart); } public static void main(string[] strings) { linechartdemo1 linechartdemo1 = new linechartdemo1( "jfreechart - line chart demo 1"); linechartdemo1.pack(); refineryutilities.centerframeonscreen(linechartdemo1); linechartdemo1.setvisible(true); } /* synthetic */ static class class $(string string) { class var_class; try { var_class = class .forname(string); } catch (classnotfoundexception classnotfoundexception) { throw new noclassdeffounderror(classnotfoundexception.getmessage()); } return var_class; } } |
運行結果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/yuhentian/article/details/20213191