中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

JFreeChart 圖表生成實(shí)例(餅圖、柱狀圖、折線圖、時(shí)序圖)

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
import java.awt.BasicStroke;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
 
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
 
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import com.lowagie.text.Font;
 
 
import demo.BarChart3DDemo1;
 
 
/**
 * Servlet implementation class ChartDemoServlet
 */
public class ChartDemoServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
    public ChartDemoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException{
     
     response.setContentType("image/jpeg");
     DefaultPieDataset data = getDataSet();
     CategoryDataset dataset = getDataSet2();
     DefaultCategoryDataset linedataset = createDataset();
     
  //JFreeChart chart = ChartFactory.createPieChart3D("水果產(chǎn)量圖", data, true, false, false);
  
     /**
  JFreeChart chart = ChartFactory.createBarChart3D(
                "水果產(chǎn)量圖", // 圖表標(biāo)題
                "水果", // 目錄軸的顯示標(biāo)簽
                "產(chǎn)量", // 數(shù)值軸的顯示標(biāo)簽
                dataset, // 數(shù)據(jù)集
    PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
    true,  // 是否顯示圖例(對(duì)于簡(jiǎn)單的柱狀圖必須是 false)
    false, // 是否生成工具
    false  // 是否生成 URL 鏈接
    );
  **/
     
  JFreeChart chart = ChartFactory.createLineChart(
    "折線圖", // 圖表標(biāo)題
       "時(shí)間", // 橫軸顯示標(biāo)簽
       "銷售額(百萬(wàn))", // 縱軸顯示標(biāo)簽
       linedataset, // 數(shù)據(jù)集
       PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
       true, // 是否顯示圖例標(biāo)識(shí)(對(duì)于簡(jiǎn)單的柱狀圖必須是 false)
       true, // 是否生成工具
       false // 是否生成 URL 鏈接
       );
  
     /*
  JFreeChart chart=ChartFactory.createTimeSeriesChart(
    "編程詞典全國(guó)銷量統(tǒng)計(jì)"
    , "銷售月份"
    , "銷量(份)"
    , createtimedata()
    , false
    , false
    , false
    );
  */
  /*
   * jfreechart1.0.13生成的圖片中,中文都以方框形式顯示,中文亂碼解決如下(包括:餅圖、柱狀圖、時(shí)序圖、折線圖)。
   */
  /***設(shè)置餅圖相關(guān)屬性***/
  /*
  PiePlot pieplot = (PiePlot) chart.getPlot();//獲取餅圖區(qū)域?qū)ο蟆?  pieplot.setLabelFont(new java.awt.Font("宋體",0,12));
  pieplot.setNoDataMessage("無(wú)數(shù)據(jù)顯示"); //沒(méi)有數(shù)據(jù)的時(shí)候顯示的內(nèi)容
  pieplot.setCircular(false);
  pieplot.setLabelGap(0.02D);
  TextTitle txtTitle = null;
  txtTitle = chart.getTitle();
  java.awt.Font font = new java.awt.Font("宋體", Font.BOLD, 16);
  txtTitle.setFont(font);//解決標(biāo)題中文亂碼
  chart.getLegend().setItemFont(font);
  chart.setBackgroundPaint(new Color(232,232,232));//設(shè)定背景
  pieplot.setBackgroundPaint(Color.white); //設(shè)定圖表數(shù)據(jù)顯示部分背景色
  */
  
  /***設(shè)置柱狀圖、折線圖相關(guān)屬性***/
  
  CategoryPlot catplot=chart.getCategoryPlot();  //獲取柱狀圖、折線圖區(qū)域?qū)ο蟆?  CategoryAxis domainAxis=catplot.getDomainAxis();
  catplot.setNoDataMessage("無(wú)數(shù)據(jù)顯示");//沒(méi)有數(shù)據(jù)的時(shí)候顯示的內(nèi)容
  //列表標(biāo)題
  TextTitle txtTitle = null;
  txtTitle = chart.getTitle();
  txtTitle.setFont(new java.awt.Font("黑體",Font.BOLD,14));
  //水平底部列表
     domainAxis.setLabelFont(new java.awt.Font("黑體",Font.BOLD,14));
     //水平底部標(biāo)題
     domainAxis.setTickLabelFont(new java.awt.Font("宋體",Font.BOLD,12));
     //垂直標(biāo)題
     ValueAxis rangeAxis=catplot.getRangeAxis();//獲取柱狀
     rangeAxis.setLabelFont(new java.awt.Font("黑體",Font.BOLD,15));
     chart.getLegend().setItemFont(new java.awt.Font("黑體", Font.BOLD, 15));
     //獲得renderer
     LineAndShapeRenderer lineAndShapeRenderer =(LineAndShapeRenderer)catplot.getRenderer();
     lineAndShapeRenderer.setShapesVisible(true); //series 點(diǎn)(即數(shù)據(jù)點(diǎn))可見(jiàn)
     /*
     lineAndShapeRenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
       10F, 6F
       }, 0.0F)); //這里是虛線,默認(rèn)是直線
     */
 
     
  
  /***設(shè)置時(shí)序圖相關(guān)屬性***/
  /**
     XYPlot xyplot = (XYPlot) chart.getPlot();//獲取時(shí)序圖區(qū)域?qū)ο蟆?     //列表標(biāo)題
  TextTitle txtTitle = null;
  txtTitle = chart.getTitle();
  txtTitle.setFont(new java.awt.Font("黑體",Font.BOLD,15));
     //縱軸字體
     xyplot.getRangeAxis().setLabelFont(new java.awt.Font("宋體", Font.BOLD, 12));
     //橫軸框里的標(biāo)題字體:顯示圖例
     //chart.getLegend().setItemFont(new java.awt.Font("宋體", Font.ITALIC, 12));
     //橫軸列表字體
     xyplot.getDomainAxis().setTickLabelFont(new java.awt.Font("新宋體", 1, 12));
     //橫軸小標(biāo)題字體
     xyplot.getDomainAxis().setLabelFont(new java.awt.Font("新宋體", 1, 12));
     chart.setBackgroundPaint(new Color(252,175,134)); //設(shè)置背景色
     xyplot.setDomainGridlinesVisible(false);  //設(shè)置網(wǎng)格不顯示
     
     //獲取時(shí)間軸對(duì)象
     DateAxis dateAxis = (DateAxis) xyplot.getDomainAxis();
     //dateAxis.setLabelFont(new java.awt.Font("黑體", Font.ITALIC , 18));   //設(shè)置時(shí)間軸字體
     dateAxis.setLowerMargin(0.0);//設(shè)置時(shí)間軸上顯示的最小值
     DateFormat format = new SimpleDateFormat("MM月份");   //創(chuàng)建日期格式對(duì)象
     //創(chuàng)建DateTickUnit對(duì)象
     DateTickUnit dtu = new DateTickUnit(DateTickUnitType.DAY,29,format);
     dateAxis.setTickUnit(dtu);//設(shè)置日期軸的日期標(biāo)簽
     **/
     
     //chart.getTitle().setFont(new java.awt.Font("宋體", Font.BOLD, 15));
     //chart.getLegend().setItemFont(new java.awt.Font("黑體", Font.BOLD, 15));
     //CategoryAxis domainAxis = plot.getDomainAxis();  
     /*------設(shè)置X軸坐標(biāo)上的文字-----------*/
     //domainAxis.setTickLabelFont(new java.awt.Font("黑體", Font.PLAIN, 11));  
     /*------設(shè)置X軸的標(biāo)題文字------------*/
     //domainAxis.setLabelFont(new java.awt.Font("宋體", Font.PLAIN, 12));  
     //NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();  
     /*------設(shè)置Y軸坐標(biāo)上的文字-----------*/
     //numberaxis.setTickLabelFont(new java.awt.Font("黑體", Font.PLAIN, 12));  
     /*------設(shè)置Y軸的標(biāo)題文字------------*/
     //numberaxis.setLabelFont(new java.awt.Font("黑體", Font.PLAIN, 12));
  
     
  FileOutputStream fos_jpg=null;
  try{
   fos_jpg=new FileOutputStream("D:\\ok_bing.jpg");
   /*
    * 第二個(gè)參數(shù)如果為100,會(huì)報(bào)異常:
    * java.lang.IllegalArgumentException: The 'quality' must be in the range 0.0f to 1.0f
    * 限制quality必須小于等于1,把100改成 0.1f。
    */
   ChartUtilities.writeChartAsJPEG(fos_jpg, 0.99f, chart, 600, 300, null);
   
   
  }catch(Exception e){
   System.out.println("[e]"+e);
  }finally{
   try{
    fos_jpg.close();
   }catch(Exception e){
    
   }
  } 
     
    }
  
 /**
  * 獲取一個(gè)演示用的簡(jiǎn)單數(shù)據(jù)集對(duì)象
  * @return
  */
    //生成餅圖數(shù)據(jù)
 private static DefaultPieDataset getDataSet() {
  DefaultPieDataset dataset = new DefaultPieDataset();
  dataset.setValue("蘋(píng)果",100);
  dataset.setValue("梨子",200);
  dataset.setValue("葡萄",300);
  dataset.setValue("香蕉",400);
  dataset.setValue("荔枝",500);
  return dataset;
 }
 //生成柱狀圖數(shù)據(jù)
 private static CategoryDataset getDataSet2() {
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(100, "北京", "蘋(píng)果");
        dataset.addValue(100, "上海", "蘋(píng)果");
        dataset.addValue(100, "廣州", "蘋(píng)果");
        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;
 }
  
 //生成折線圖數(shù)據(jù)
  public static DefaultCategoryDataset createDataset() {
   DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
   //  各曲線名稱
   String series1 = "冰箱";
   String series2 = "彩電";
   String series3 = "洗衣機(jī)";
 
   //橫軸名稱(列名稱)
   String type1 = "1月";
   String type2 = "2月";
   String type3 = "3月";
   String type4 = "4月";
   String type5 = "5月";
 
   linedataset.addValue(0.0, series1, type1);
   linedataset.addValue(4.2, series1, type2);
   linedataset.addValue(3.9, series1, type3);
   linedataset.addValue(3.2, series1, type4);
   linedataset.addValue(3.0, series1, type5);
 
   linedataset.addValue(1.0, series2, type1);
   linedataset.addValue(5.2, series2, type2);
   linedataset.addValue(7.9, series2, type3);
   linedataset.addValue(8.9, series2, type4);
   linedataset.addValue(9.2, series2, type5);
 
   linedataset.addValue(2.0, series3, type1);
   linedataset.addValue(9.2, series3, type2);
   linedataset.addValue(8.9, series3, type3);
   linedataset.addValue(9.9, series3, type4);
   linedataset.addValue(10.9, series3, type5);
 
   return linedataset;
  }
  
 //生成時(shí)序圖數(shù)據(jù)
  public static XYDataset createtimedata() {
  //實(shí)例化TimeSeries對(duì)象
   TimeSeries timeseries = new TimeSeries("Data");
   Day day = new Day(1, 1, 2008);  //實(shí)例化Day
   double d = 3000D;//添加一年365天的數(shù)據(jù)
   for (int i = 0; i < 365; i++) {
    d = d + (Math.random() - 0.5) * 10; //創(chuàng)建隨機(jī)數(shù)據(jù)
    timeseries.add(day, d); //向數(shù)據(jù)集合中添加數(shù)據(jù)
    day = (Day) day.next();
   }
   //創(chuàng)建TimeSeriesCollection集合對(duì)象
   TimeSeriesCollection timeSeriesCollection =new TimeSeriesCollection(timeseries);
   //返回?cái)?shù)據(jù)集合對(duì)象
   return timeSeriesCollection;
  }
  
}

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:JAVA生成短8位UUID

下一篇: js獲取屏幕大小