應用java完成各類數據統計圖(柱形圖,餅圖,折線圖)。本站提示廣大學習愛好者:(應用java完成各類數據統計圖(柱形圖,餅圖,折線圖))文章只能為提供參考,不一定能成為您想要的結果。以下是應用java完成各類數據統計圖(柱形圖,餅圖,折線圖)正文
比來在做數據發掘的課程設計,須要將數據剖析的成果很直不雅的展示給用戶,這就要用到數據統計圖,要完成這個功效就須要幾個第三方包了:
1. jfreechart-1.0.13.jar
2. jcommon-1.0.16.jar
3. gnujaxp.jar
先來看一下,終究後果圖:
重要是jfreechart-1.0.13.jar,但這三個包要齊備,我曾經將一切與jfreechart有關的jar包與本文實例的工程(代碼)一同緊縮上傳了,有興致的同窗可以下載,
下載地址:http://download.csdn.net/detail/pzhtpf/4327700
接上去,我們一步步來完成本法式。
一,後期預備任務,也就把這三個第三方包添加進本文工程,添加進程特殊簡略,後面寫過一篇博客,講的是java若何讀取Excel表格中的數據(有興致的同窗可以看一看:http://blog.csdn.net/pzhtpf/article/details/7506135),也要添加第三方包,添加進程如出一轍,這裡我們在溫習一遍:
1, 建,立java項目,在這個項目在樹立一個新的文件夾lib;
2, 將上述三個jar包,復制到lib
3,然後右鍵點擊這個java項目,選擇Properties
4,在左邊列內外選中Java Build Path,右邊選中Libraries
5,點擊Add JARs
6, 然後去選擇這個項目中lib文件夾中的三個jar,點擊肯定
勝利後,項目中會多一個文件夾為:Referenced Libraries
二, 完成柱形圖的java代碼:
import java.awt.Font; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; 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; public class BarChart { ChartPanel frame1; public BarChart(){ CategoryDataset dataset = getDataSet(); JFreeChart chart = ChartFactory.createBarChart3D( "生果", // 圖表題目 "生果品種", // 目次軸的顯示標簽 "數目", // 數值軸的顯示標簽 dataset, // 數據集 PlotOrientation.VERTICAL, // 圖表偏向:程度、垂直 true, // 能否顯示圖例(關於簡略的柱狀圖必需是false) false, // 能否生成對象 false // 能否生成URL鏈接 ); //從這裡開端 CategoryPlot plot=chart.getCategoryPlot();//獲得圖表區域對象 CategoryAxis domainAxis=plot.getDomainAxis(); //程度底部列表 domainAxis.setLabelFont(new Font("黑體",Font.BOLD,14)); //程度底部題目 domainAxis.setTickLabelFont(new Font("宋體",Font.BOLD,12)); //垂直題目 ValueAxis rangeAxis=plot.getRangeAxis();//獲得柱狀 rangeAxis.setLabelFont(new Font("黑體",Font.BOLD,15)); chart.getLegend().setItemFont(new Font("黑體", Font.BOLD, 15)); chart.getTitle().setFont(new Font("宋體",Font.BOLD,20));//設置題目字體 //到這裡停止,固然代碼有點多,但只為一個目標,處理漢字亂碼成績 frame1=new ChartPanel(chart,true); //這裡也能夠用chartFrame,可以直接生成一個自力的Frame } private static CategoryDataset getDataSet() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "蘋果"); dataset.addValue(100, "上海", "蘋果"); dataset.addValue(100, "廣州", "蘋果"); dataset.addValue(200, "北京", "梨子"); dataset.addValue(200, "上海", "梨子"); dataset.addValue(200, "廣州", "梨子"); dataset.addValue(300, "北京", "葡萄"); dataset.addValue(300, "上海", "葡萄"); dataset.addValue(300, "廣州", "葡萄"); dataset.addValue(400, "北京", "噴鼻蕉"); dataset.addValue(400, "上海", "噴鼻蕉"); dataset.addValue(400, "廣州", "噴鼻蕉"); dataset.addValue(500, "北京", "荔枝"); dataset.addValue(500, "上海", "荔枝"); dataset.addValue(500, "廣州", "荔枝"); return dataset; } public ChartPanel getChartPanel(){ return frame1; } }
後果圖以下:
但我們把private static CategoryDataset getDataSet(){}辦法中的數據變更一下後,又會構成另外一種後果,好比說我們改成:
private static CategoryDataset getDataSet() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "蘋果", "蘋果"); dataset.addValue(200, "梨子", "梨子"); dataset.addValue(300, "葡萄", "葡萄"); dataset.addValue(400, "噴鼻蕉", "噴鼻蕉"); dataset.addValue(500, "荔枝", "荔枝"); return dataset; }
後果圖以下:
三 完成餅狀圖的java代碼:
package com.njue.testJFreeChart; import java.awt.Font; import java.text.DecimalFormat; import java.text.NumberFormat; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; 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 PieChart { ChartPanel frame1; public PieChart(){ DefaultPieDataset data = getDataSet(); JFreeChart chart = ChartFactory.createPieChart3D("生果產量",data,true,false,false); //設置百分比 PiePlot pieplot = (PiePlot) chart.getPlot(); DecimalFormat df = new DecimalFormat("0.00%");//取得一個DecimalFormat對象,重要是設置小數成績 NumberFormat nf = NumberFormat.getNumberInstance();//取得一個NumberFormat對象 StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);//取得StandardPieSectionLabelGenerator對象 pieplot.setLabelGenerator(sp1);//設置餅圖顯示百分比 //沒稀有據的時刻顯示的內容 pieplot.setNoDataMessage("有數據顯示"); pieplot.setCircular(false); pieplot.setLabelGap(0.02D); pieplot.setIgnoreNullValues(true);//設置不顯示空值 pieplot.setIgnoreZeroValues(true);//設置不顯示負值 frame1=new ChartPanel (chart,true); chart.getTitle().setFont(new Font("宋體",Font.BOLD,20));//設置題目字體 PiePlot piePlot= (PiePlot) chart.getPlot();//獲得圖表區域對象 piePlot.setLabelFont(new Font("宋體",Font.BOLD,10));//處理亂碼 chart.getLegend().setItemFont(new Font("黑體",Font.BOLD,10)); } private static DefaultPieDataset getDataSet() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("蘋果",100); dataset.setValue("梨子",200); dataset.setValue("葡萄",300); dataset.setValue("噴鼻蕉",400); dataset.setValue("荔枝",500); return dataset; } public ChartPanel getChartPanel(){ return frame1; } }
後果圖以下:
四 完成折線圖的java代碼:
package com.njue.testJFreeChart; import java.awt.Font; import java.text.SimpleDateFormat; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.data.time.Month; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; public class TimeSeriesChart { ChartPanel frame1; public TimeSeriesChart(){ XYDataset xydataset = createDataset(); JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General單元信任基金價錢", "日期", "價錢",xydataset, true, true, true); XYPlot xyplot = (XYPlot) jfreechart.getPlot(); DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis(); dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy")); frame1=new ChartPanel(jfreechart,true); dateaxis.setLabelFont(new Font("黑體",Font.BOLD,14)); //程度底部題目 dateaxis.setTickLabelFont(new Font("宋體",Font.BOLD,12)); //垂直題目 ValueAxis rangeAxis=xyplot.getRangeAxis();//獲得柱狀 rangeAxis.setLabelFont(new Font("黑體",Font.BOLD,15)); jfreechart.getLegend().setItemFont(new Font("黑體", Font.BOLD, 15)); jfreechart.getTitle().setFont(new Font("宋體",Font.BOLD,20));//設置題目字體 } private static XYDataset createDataset() { //這個數據集有點多,但都不難懂得 TimeSeries timeseries = new TimeSeries("legal & general歐洲指數信賴", org.jfree.data.time.Month.class); timeseries.add(new Month(2, 2001), 181.80000000000001D); timeseries.add(new Month(3, 2001), 167.30000000000001D); timeseries.add(new Month(4, 2001), 153.80000000000001D); timeseries.add(new Month(5, 2001), 167.59999999999999D); timeseries.add(new Month(6, 2001), 158.80000000000001D); timeseries.add(new Month(7, 2001), 148.30000000000001D); timeseries.add(new Month(8, 2001), 153.90000000000001D); timeseries.add(new Month(9, 2001), 142.69999999999999D); timeseries.add(new Month(10, 2001), 123.2D); timeseries.add(new Month(11, 2001), 131.80000000000001D); timeseries.add(new Month(12, 2001), 139.59999999999999D); timeseries.add(new Month(1, 2002), 142.90000000000001D); timeseries.add(new Month(2, 2002), 138.69999999999999D); timeseries.add(new Month(3, 2002), 137.30000000000001D); timeseries.add(new Month(4, 2002), 143.90000000000001D); timeseries.add(new Month(5, 2002), 139.80000000000001D); timeseries.add(new Month(6, 2002), 137D); timeseries.add(new Month(7, 2002), 132.80000000000001D); TimeSeries timeseries1 = new TimeSeries("legal & general英國指數信賴", org.jfree.data.time.Month.class); timeseries1.add(new Month(2, 2001), 129.59999999999999D); timeseries1.add(new Month(3, 2001), 123.2D); timeseries1.add(new Month(4, 2001), 117.2D); timeseries1.add(new Month(5, 2001), 124.09999999999999D); timeseries1.add(new Month(6, 2001), 122.59999999999999D); timeseries1.add(new Month(7, 2001), 119.2D); timeseries1.add(new Month(8, 2001), 116.5D); timeseries1.add(new Month(9, 2001), 112.7D); timeseries1.add(new Month(10, 2001), 101.5D); timeseries1.add(new Month(11, 2001), 106.09999999999999D); timeseries1.add(new Month(12, 2001), 110.3D); timeseries1.add(new Month(1, 2002), 111.7D); timeseries1.add(new Month(2, 2002), 111D); timeseries1.add(new Month(3, 2002), 109.59999999999999D); timeseries1.add(new Month(4, 2002), 113.2D); timeseries1.add(new Month(5, 2002), 111.59999999999999D); timeseries1.add(new Month(6, 2002), 108.8D); timeseries1.add(new Month(7, 2002), 101.59999999999999D); TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(); timeseriescollection.addSeries(timeseries); timeseriescollection.addSeries(timeseries1); return timeseriescollection; } public ChartPanel getChartPanel(){ return frame1; } }
後果圖以下:
再來看一下主辦法:
import java.awt.GridLayout; import javax.swing.JFrame; public class mainClass { public static void main(String args[]){ JFrame frame=new JFrame("Java數據統計圖"); frame.setLayout(new GridLayout(2,2,10,10)); frame.add(new BarChart().getChartPanel()); //添加柱形圖 frame.add(new BarChart1().getChartPanel()); //添加柱形圖的另外一種後果 frame.add(new PieChart().getChartPanel()); //添加餅狀圖 frame.add(new TimeSeriesChart().getChartPanel()); //添加折線圖 frame.setBounds(50, 50, 800, 600); frame.setVisible(true); } }
五 總結
以上都是一個簡略的例子去完成了,想懂得更深的同窗可自行查詢材料,其實以上代碼都淺顯易懂,只需聯合本身的現實情形,即可開辟出屬於本身的Application,年夜家可以看出我這裡是在Application上完成的,其實更多情形數據統計圖在javaweb上運用更多,年夜家也可自行懂得。