Java建立柱狀圖及餅狀圖
阿新 • • 發佈:2018-11-13
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
Java建立圖表其實還是很方便的,但是要引入相關的jar包。如下
- jfreechart.jar
- jcommon,jar
- gnujaxp.jar
其中最主要的是jfreechart.jar。
下面就讓我們先看看建立的圖示的執行結果吧。
是不是感覺很生動形象,一目瞭然呢?
下面讓我們一一拉進行分析吧首先是柱狀圖
我已經在程式碼中做好了註釋,可以直接拿過來使用。
package mytest;import java.awt.FlowLayout;import java.awt.Font;import java.io.File;import java.io.IOException;import javax.swing.JFrame;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.ValueAxis;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.data.category .CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.data.general.Dataset;public class BarChartDemo { private ChartPanel panel; /** * 在柱狀圖中建立表格的步驟如下: 1、建立表格chart,需要注意相關引數的含義, 2、傳進去的資料集是CategoryDataset格式 * 3、獲得表格區域塊,設定橫軸,縱軸及相關字型(防止出現亂卡的狀況) * 4、設定chart的圖例legend,並設定條目的字型格式(同樣是為了防止出現亂碼) */ public BarChartDemo() { CategoryDataset dataset = (CategoryDataset) getDataset(); JFreeChart chart = ChartFactory.createBarChart3D("學歷資訊統計", "橫座標", "縱座標", dataset, PlotOrientation.VERTICAL, true, false, false); CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot(); CategoryAxis axis = plot.getDomainAxis(); axis.setLabelFont(new Font("宋體", Font.BOLD, 20)); axis.setTickLabelFont(new Font("宋體", Font.BOLD, 20)); ValueAxis rangeAxis = plot.getRangeAxis(); rangeAxis.setLabelFont(new Font("宋體", Font.BOLD, 20)); chart.getLegend().setItemFont(new Font("宋體", Font.BOLD, 20)); chart.getTitle().setFont(new Font("黑體", Font.ITALIC, 22)); panel = new ChartPanel(chart, true); //下面是王章偶然看到的生成圖表圖片的方法 File dir = new File("F:\\MyPicture\\"); if (!dir.exists()) { dir.mkdir(); } String fName = String.valueOf(System.currentTimeMillis())+"BarChart.png"; File file = new File("F:\\MyPicture\\", fName); try { ChartUtilities.saveChartAsPNG(file, chart, 400, 250); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }//生成一個png圖片 } public ChartPanel getChartPanel() { return panel; } /** * 需要注意的是在向資料集中新增資料的時候 使用的是dataset.addValue()方法,而在餅狀圖的資料集新增資料的過程中,使用的是dataset.setValue()方法 * 這一點應該尤其注意。以免出錯! * @return */ private static Dataset getDataset() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(1, "郭1", "大學"); dataset.addValue(2, "郭2", "高中"); dataset.addValue(3, "郭3", "高中"); dataset.addValue(4, "郭4", "高中"); dataset.addValue(5, "郭5", "初中"); dataset.addValue(5, "郭6", "初中"); dataset.addValue(4, "郭7", "初中"); dataset.addValue(3, "郭8", "小學"); dataset.addValue(2, "郭9", "幼兒園"); dataset.addValue(1, "張10", "幼兒園"); return dataset; } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.add(new BarChartDemo().getChartPanel()); frame.setSize(1000, 600); frame.setDefaultCloseOperation(0); frame.setLocationRelativeTo(null); frame.setVisible(true); }}
- 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
然後是餅狀圖,裡面的註釋也是很到位的進行了解釋
package mytest;import java.awt.FlowLayout;import java.awt.Font;import java.io.File;import java.io.IOException;import java.text.DecimalFormat;import java.text.NumberFormat;import javax.swing.JFrame;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartPanel;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.labels.StandardPieSectionLabelGenerator;import org.jfree.chart.plot.PiePlot;import org.jfree.data.general.DefaultPieDataset;public class PieChartDemo { ChartPanel panel; /** * 建立餅狀圖的步驟如下: 1、建立一個餅狀的例項,注意傳參的格式,還有需要注意的是此時的資料集應該是defaultPieDataset, * 而不是CategoryDataset格式 2、獲得餅狀圖的所在區域 3、設定兩個格式化的資料格式,為後面的床架餅狀圖的例項做基礎 * 4、細節方面是對無資料、零值、負值等情況的處理 5、最後就是設定在出現漢字的地方進行字型內容的設定了(同樣的,這是為了防止出現亂碼的狀況) */ public PieChartDemo() { DefaultPieDataset dataset = getDataset(); JFreeChart chart = ChartFactory.createPieChart3D("學校佔比情況", dataset, true, false, false); PiePlot piePlot = (PiePlot) chart.getPlot(); DecimalFormat df = new DecimalFormat("0.00%"); NumberFormat nf = NumberFormat.getInstance(); StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator( "{0} {2}", //獲得StandardPieSectionLabelGenerator物件,生成的格式,{0}表示section名, //{1}表示section的值,{2}表示百分比。可以自定義 nf, df); piePlot.setLabelGenerator(generator);// 設定百分比 piePlot.setLabelFont(new Font("黑體", Font.ITALIC, 20)); // 當餅狀圖內額米有資料時,作如下資料中設定 piePlot.setNoDataMessage("此時並沒有任何資料可用"); piePlot.setCircular(false); piePlot.setLabelGap(0.02D); piePlot.setIgnoreNullValues(true);// 設定不顯示空位 piePlot.setIgnoreZeroValues(true);// 設定不顯示負值或零值 panel = new ChartPanel(chart, true); chart.getTitle().setFont(new Font("宋體", Font.BOLD, 18)); chart.getLegend().setItemFont(new Font("宋體", Font.BOLD, 20)); //下面是王章偶然看到的生成圖表圖片的方法 File dir = new File("F:\\MyPicture\\"); if (!dir.exists()) { dir.mkdir(); } String fName = String.valueOf(System.currentTimeMillis())+"pie.png"; File file = new File("F:\\MyPicture\\", fName); try { ChartUtilities.saveChartAsPNG(file, chart, 400, 250); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }//生成一個png圖片 } /** * 需要注意的是在向資料集中新增資料的時候呼叫的是dataset.setvalue()方法,而不是柱狀圖中的addValue()方法 * 這一點應該尤其注意一下,以免在使用的時候出現錯誤 * @return */ private DefaultPieDataset getDataset() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("郭1", 1); dataset.setValue("郭2", 2); dataset.setValue("郭3", 3); dataset.setValue("郭4", 4); dataset.setValue("郭5", 3); dataset.setValue("郭6", 2); dataset.setValue("郭7", 1); return dataset; } public ChartPanel getPieChartPanel() { return panel; } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.add(new PieChartDemo().getPieChartPanel()); frame.setSize(1000, 700); frame.setVisible(true); }}
- 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
這裡並沒有建立折線圖,大致的步驟和這兩種圖的建立相當。詳細連結
總結:
java建立圖表來對資料進行分析,使得資料的展示形式更加生動,具體。
這裡我還得到了一個知識點,那就是利用相關方法,來實現對這種圖集的生成操作,只需指定號生成影象的儲存路徑及儲存名稱即可實現。如下
//下面是王章偶然看到的生成圖表圖片的方法 File dir = new File("F:\\MyPicture\\"); if (!dir.exists()) { dir.mkdir(); } String fName = String.valueOf(System.currentTimeMillis())+"pie.png"; File file = new File("F:\\MyPicture\\", fName); try { ChartUtilities.saveChartAsPNG(file, chart, 400, 250); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }//生成一個png圖片
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
the end!