首先請大家先到下載JfreeChart的檔案,
請把這個檔案解壓縮。
使用的方法如下:
在Java Project按右鍵,選擇Properties。
到Java Build Path→Libraries→Add External JARs...
將剛剛解壓縮後的檔案
「lib\jcommon-1.0.17.jar」
「lib\jfreechart-1.0.14.jar」加進去。
#程式碼部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.setValue( 6 , "銷售量" , "茶" ); dataset.setValue( 7 , "銷售量" , "飲料" ); dataset.setValue( 8 , "銷售量" , "咖啡" ); dataset.setValue( 5 , "銷售量" , "麵包" ); dataset.setValue( 12 , "銷售量" , "糖果" ); JFreeChart chart = ChartFactory.createBarChart( "長條圖" , "產品" , "銷售量" , dataset, PlotOrientation.VERTICAL, false , true , false ); //將font設定為細明體 Font CHART_FONT = new Font( "細明體" , 12 , 15 ); chart.getTitle().setFont(CHART_FONT); CategoryPlot plot = chart.getCategoryPlot(); CategoryAxis domainAxis = plot.getDomainAxis(); //橫軸 ValueAxis rangeAxis = plot.getRangeAxis(); //縱軸 domainAxis.setLabelFont(CHART_FONT); domainAxis.setTickLabelFont(CHART_FONT); rangeAxis.setLabelFont(CHART_FONT); rangeAxis.setTickLabelFont(CHART_FONT); ChartPanel panel = new ChartPanel(chart, 200 , 53 , 200 , 70 , 600 , 395 , true , true , true , true , true , true ); panel.setBounds( 94 , 43 , 547 , 423 ); contentPane.add(panel); |
這邊要注意到的是,若不將font設定為細明體,所有文字都會變成亂碼。
執行結果:
http://www.jfree.org/jfreechart/api/javadoc/index.html
沒有留言:
張貼留言