欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用java實(shí)現(xiàn)各種數(shù)據(jù)統(tǒng)計(jì)圖(柱形圖,餅圖,折線圖)

 更新時間:2015年10月14日 10:11:16   投稿:mrr  
用Jfree實(shí)現(xiàn)條形柱狀圖表,java代碼實(shí)現(xiàn)??山?jīng)常用于報表的制作,代碼自動生成后可以自由查看??梢宰杂膳渲脠D表的各個屬性,用來達(dá)到自己的要求和目的。本文給大家介紹使用java實(shí)現(xiàn)各種數(shù)據(jù)統(tǒng)計(jì)圖(柱形圖,餅圖,折線圖),需要的朋友可以參考下

最近在做數(shù)據(jù)挖掘的課程設(shè)計(jì),需要將數(shù)據(jù)分析的結(jié)果很直觀的展現(xiàn)給用戶,這就要用到數(shù)據(jù)統(tǒng)計(jì)圖,要實(shí)現(xiàn)這個功能就需要幾個第三方包了:

1.       jfreechart-1.0.13.jar
2.       jcommon-1.0.16.jar
3.       gnujaxp.jar

先來看一下,最終效果圖:


主要是jfreechart-1.0.13.jar,但這三個包要齊全,我已經(jīng)將所有與jfreechart有關(guān)的jar包與本文實(shí)例的工程(代碼)一同壓縮上傳了,有興趣的同學(xué)可以下載,
下載地址:http://download.csdn.net/detail/pzhtpf/4327700

接下來,我們一步步來實(shí)現(xiàn)本程序。

一,前期準(zhǔn)備工作,也就把這三個第三方包添加進(jìn)本文工程,添加過程特別簡單,前面寫過一篇博客,講的是java如何讀取Excel表格中的數(shù)據(jù)(有興趣的同學(xué)可以看一看:http://blog.csdn.net/pzhtpf/article/details/7506135),也要添加第三方包,添加過程一模一樣,這里我們在復(fù)習(xí)一遍:

1, 建,立java項(xiàng)目,在這個項(xiàng)目在建立一個新的文件夾lib;
2, 將上述三個jar包,復(fù)制到lib
3,然后右鍵點(diǎn)擊這個java項(xiàng)目,選擇Properties
4,在左側(cè)列表里選中Java Build Path,右側(cè)選中Libraries
5,點(diǎn)擊Add JARs
6, 然后去選擇這個項(xiàng)目中l(wèi)ib文件夾中的三個jar,點(diǎn)擊確定

成功后,項(xiàng)目中會多一個文件夾為:Referenced Libraries

二, 實(shí)現(xiàn)柱形圖的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( 
               "水果", // 圖表標(biāo)題 
              "水果種類", // 目錄軸的顯示標(biāo)簽 
              "數(shù)量", // 數(shù)值軸的顯示標(biāo)簽 
              dataset, // 數(shù)據(jù)集 
              PlotOrientation.VERTICAL, // 圖表方向:水平、垂直 
              true,      // 是否顯示圖例(對于簡單的柱狀圖必須是false) 
              false,     // 是否生成工具 
              false      // 是否生成URL鏈接 
              ); 
    //從這里開始 
    CategoryPlot plot=chart.getCategoryPlot();//獲取圖表區(qū)域?qū)ο?
    CategoryAxis domainAxis=plot.getDomainAxis();     //水平底部列表 
     domainAxis.setLabelFont(new Font("黑體",Font.BOLD,14));     //水平底部標(biāo)題 
     domainAxis.setTickLabelFont(new Font("宋體",Font.BOLD,12)); //垂直標(biāo)題 
     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));//設(shè)置標(biāo)題字體 
     //到這里結(jié)束,雖然代碼有點(diǎn)多,但只為一個目的,解決漢字亂碼問題 
     frame1=new ChartPanel(chart,true);    //這里也可以用chartFrame,可以直接生成一個獨(dú)立的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(){}方法中的數(shù)據(jù)變化一下后,又會形成另一種效果,比如說我們改成:

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; 
} 

效果圖如下:

 

三 實(shí)現(xiàn)餅狀圖的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("水果產(chǎn)量",data,true,false,false); 
    //設(shè)置百分比 
     PiePlot pieplot = (PiePlot) chart.getPlot(); 
     DecimalFormat df = new DecimalFormat("0.00%");//獲得一個DecimalFormat對象,主要是設(shè)置小數(shù)問題 
     NumberFormat nf = NumberFormat.getNumberInstance();//獲得一個NumberFormat對象 
     StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);//獲得StandardPieSectionLabelGenerator對象 
     pieplot.setLabelGenerator(sp1);//設(shè)置餅圖顯示百分比 
   //沒有數(shù)據(jù)的時候顯示的內(nèi)容 
     pieplot.setNoDataMessage("無數(shù)據(jù)顯示"); 
     pieplot.setCircular(false); 
     pieplot.setLabelGap(0.02D); 
     pieplot.setIgnoreNullValues(true);//設(shè)置不顯示空值 
     pieplot.setIgnoreZeroValues(true);//設(shè)置不顯示負(fù)值 
     frame1=new ChartPanel (chart,true); 
     chart.getTitle().setFont(new Font("宋體",Font.BOLD,20));//設(shè)置標(biāo)題字體 
     PiePlot piePlot= (PiePlot) chart.getPlot();//獲取圖表區(qū)域?qū)ο?
     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; 
  } 
} 

 效果圖如下:

四 實(shí)現(xiàn)折線圖的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));     //水平底部標(biāo)題 
    dateaxis.setTickLabelFont(new Font("宋體",Font.BOLD,12)); //垂直標(biāo)題 
    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));//設(shè)置標(biāo)題字體 
  }  
   private static XYDataset createDataset() { //這個數(shù)據(jù)集有點(diǎn)多,但都不難理解 
      TimeSeries timeseries = new TimeSeries("legal & general歐洲指數(shù)信任", 
          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英國指數(shù)信任", 
          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數(shù)據(jù)統(tǒng)計(jì)圖"); 
  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); 
} 
} 

五 總結(jié)

以上都是一個簡單的例子去實(shí)現(xiàn)了,想了解更深的同學(xué)可自行查詢資料,其實(shí)以上代碼都通俗易懂,只要結(jié)合自己的實(shí)際情況,便可開發(fā)出屬于自己的Application,大家可以看出我這里是在Application上實(shí)現(xiàn)的,其實(shí)更多情況數(shù)據(jù)統(tǒng)計(jì)圖在javaweb上應(yīng)用更多,大家也可自行了解。

相關(guān)文章

  • 如何使用JFrame完成動態(tài)模擬時鐘

    如何使用JFrame完成動態(tài)模擬時鐘

    本文介紹了如何使用JFrame完成動態(tài)模擬時鐘,需要的朋友可以參考下
    2015-08-08
  • Spring中@Autowired注解的原理詳解

    Spring中@Autowired注解的原理詳解

    這篇文章主要介紹了Spring中@Autowired注解的原理詳解,對于spring配置一個bean時,如果需要給該bean提供一些初始化參數(shù),則需要通過依賴注入方式,所謂的依賴注入就是通過spring將bean所需要的一些參數(shù)傳遞到bean實(shí)例對象的過程,需要的朋友可以參考下
    2023-11-11
  • IntelliJ IDEA中代碼一鍵生成方法

    IntelliJ IDEA中代碼一鍵生成方法

    EasyCode 是基于 IntelliJ IDEA 開發(fā)的代碼生成插件,支持自定義任意模板(Java,html,js,xml),這篇文章主要介紹了IntelliJ IDEA中代碼一鍵生成方法,需要的朋友可以參考下
    2020-02-02
  • java實(shí)現(xiàn)仿windows 字體設(shè)置選項(xiàng)卡實(shí)例

    java實(shí)現(xiàn)仿windows 字體設(shè)置選項(xiàng)卡實(shí)例

    本篇文章介紹了java仿windows 字體設(shè)置選項(xiàng)卡,可實(shí)現(xiàn)類似windows字體設(shè)置效果,需要的朋友可以參考下。
    2016-10-10
  • Spring中的事務(wù)管理及實(shí)現(xiàn)方式解析

    Spring中的事務(wù)管理及實(shí)現(xiàn)方式解析

    這篇文章主要介紹了Spring中的事務(wù)管理及實(shí)現(xiàn)方式解析,Spring事務(wù)管理基于底層數(shù)據(jù)庫本身的事務(wù)處理機(jī)制,數(shù)據(jù)庫事務(wù)的基礎(chǔ),是掌握Spring事務(wù)管理的基礎(chǔ),這篇總結(jié)下Spring事務(wù),需要的朋友可以參考下
    2024-01-01
  • BeanDefinitionRegistryPostProcessor如何動態(tài)注冊Bean到Spring

    BeanDefinitionRegistryPostProcessor如何動態(tài)注冊Bean到Spring

    這篇文章主要介紹了BeanDefinitionRegistryPostProcessor如何動態(tài)注冊Bean到Spring,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • MyBatis中的mapper.xml配置教程

    MyBatis中的mapper.xml配置教程

    這篇文章主要介紹了MyBatis中的mapper.xml配置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • Java三種循環(huán)求和方法

    Java三種循環(huán)求和方法

    本篇文章給大家介紹了Java三種循環(huán)求和的方法,大家在學(xué)程序的時候如果能用的到,參考下吧。
    2018-02-02
  • springboot?max-http-header-size最大長度的那些事及JVM調(diào)優(yōu)方式

    springboot?max-http-header-size最大長度的那些事及JVM調(diào)優(yōu)方式

    這篇文章主要介紹了springboot?max-http-header-size最大長度的那些事及JVM調(diào)優(yōu)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • activemq整合springboot使用方法(個人微信小程序用)

    activemq整合springboot使用方法(個人微信小程序用)

    這篇文章主要介紹了activemq整合springboot使用(個人微信小程序用),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評論