成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

JFreeChart最佳實踐:折線圖

開發 后端
本文將介紹作者通過Java最佳圖形解決方案JFreeChart實現折線圖的詳細過程。

在這個公司,用到了太多的JfreeChart,今天就對折線圖作一個總結,希望對大家有點幫助,我這里直接是與業務邏輯相關的,業務需要的數據加載到數據集等,不過我會作一些注釋的,呵,之前有網友建議寫注釋。

折線圖,大可分為兩種,

(1)X軸值類型為String的。

2)常用的是X軸值是日期的,并且,有時需要滿足這樣的需求:

1、時間要連續。

2、時間可以設置固定的跨度,比如,2009-02-01,2009-02-04,2009-02-07……

3、由于時間跨度較大,想要做到不同精度的圖表,如時間為10天時,以日(yyyy-MM-dd)格式為精度,時間跨度為2個月時,以周(如2009年第3周)為精度,跨度為6個月時,以月(2009年8月)為精度。

下面,針對比較復雜的(2)來講解:

1、取到業務邏輯需要的數據:(具體過程就不啰嗦了,就是查詢數據庫,得到想要的字段的值,加載到List里面) 返回List<PressureBean>

PressureBean的包含的屬性:
 

  1. int userId;  
  2.     String bpDate;  
  3.     String bpTime;  
  4.     int syspress;  //收縮壓(mmHg)  
  5.     int diapress; //舒張壓(mmHg) 

2、加載數據集

  1. public static TimeSeriesCollection createTimeSeries(  
  2.             List<PressureBean> list, int dayOrweekOrmonth, Log log, String shou,String shu  
  3.             ) {  
  4.  
  5.         TimeSeriesCollection timesers = new TimeSeriesCollection();  
  6.  
  7.         int mon = 1;  
  8.         int day = 1;  
  9.         int ye = 2000;  
  10.         int week = 1;  
  11.  
  12.         // 按天顯示  
  13.         if (dayOrweekOrmonth == 0) {  
  14.  
  15.             TimeSeries timeseries = new TimeSeries(shou,  
  16.                     org.jfree.data.time.Day.class);  
  17.             TimeSeries timeseries1 = new TimeSeries("c1",  
  18.                     org.jfree.data.time.Day.class);  
  19.  
  20.             TimeSeries timeseriedia = new TimeSeries(shu,  
  21.                     org.jfree.data.time.Day.class);  
  22.             TimeSeries timeseriedia1 = new TimeSeries("d1",  
  23.                     org.jfree.data.time.Day.class);  
  24.  
  25.             Iterator<PressureBean> it = list.iterator();  
  26.             while (it.hasNext()) {  
  27.                 PressureBean pres = it.next();  
  28.                 String date = pres.getBpDate();  
  29.  
  30.                 ye = Integer.parseInt(date.substring(04));  
  31.                 mon = Integer.parseInt(date.substring(57));  
  32.                 day = Integer.parseInt(date.substring(8, date.length()));  
  33.                 Day days = new Day(day, mon, ye);  
  34.  
  35.                 double sys = pres.getSyspress();  
  36.                 double dia = pres.getDiapress();  
  37.                 if (sys != -1 && sys > 0) {  
  38.                     timeseries.add(days, sys);  
  39.                 } else {  
  40.                     timeseries1.add(days, null);  
  41.                 }  
  42.                 if (sys != -1 && sys > 0) {  
  43.                     timeseriedia.add(days, dia);  
  44.                 } else {  
  45.                     timeseriedia1.add(days, null);  
  46.                 }  
  47.  
  48.             }  
  49.  
  50.             timesers.addSeries(timeseries);  
  51.             timesers.addSeries(timeseriedia);  
  52.             timesers.addSeries(timeseries1);  
  53.             timesers.addSeries(timeseriedia1);  
  54.  
  55.         } else if (dayOrweekOrmonth == 1) {//按周顯示  
  56.             TimeSeries timeseries = new TimeSeries(shou,  
  57.                     org.jfree.data.time.Week.class);  
  58.             TimeSeries timeseries1 = new TimeSeries("c1",  
  59.                     org.jfree.data.time.Week.class);  
  60.  
  61.             TimeSeries timeseriedia = new TimeSeries(shu,  
  62.                     org.jfree.data.time.Week.class);  
  63.             TimeSeries timeseriedia1 = new TimeSeries("d1",  
  64.                     org.jfree.data.time.Week.class);  
  65.  
  66.             Iterator<PressureBean> it = list.iterator();  
  67.             while (it.hasNext()) {  
  68.                 PressureBean pres = it.next();  
  69.                 String date = pres.getBpDate();  
  70.  
  71.                 String[] spls = date.split("-");  
  72.                 if (spls.length == 2) {  
  73.                     ye = Integer.parseInt(spls[0]);  
  74.                     mon = Integer.parseInt(spls[1]);  
  75.                 } else {  
  76.                     log.error("the date of weeks is wrong");  
  77.                 }  
  78.  
  79.                 Week days = new Week(mon, ye);  
  80.                 double sys = pres.getSyspress();  
  81.                 double dia = pres.getDiapress();  
  82.  
  83.                 if (sys != -1 && sys > 0) {  
  84.                     timeseries.add(days, sys);  
  85.                 } else {  
  86.                     timeseries1.add(days, null);  
  87.                 }  
  88.                 if (sys != -1 && sys > 0) {  
  89.                     timeseriedia.add(days, dia);  
  90.                 } else {  
  91.                     timeseriedia1.add(days, null);  
  92.                 }  
  93.  
  94.             }  
  95.  
  96.             timesers.addSeries(timeseries);  
  97.             timesers.addSeries(timeseriedia);  
  98.             timesers.addSeries(timeseries1);  
  99.               
  100.             timesers.addSeries(timeseriedia1);  
  101.  
  102.         } else {//按月顯示  
  103.             TimeSeries timeseries = new TimeSeries(shou,  
  104.                     org.jfree.data.time.Month.class);  
  105.             TimeSeries timeseries1 = new TimeSeries("c1",  
  106.                     org.jfree.data.time.Month.class);  
  107.  
  108.             TimeSeries timeseriedia = new TimeSeries(shu,  
  109.                     org.jfree.data.time.Month.class);  
  110.             TimeSeries timeseriedia1 = new TimeSeries("s",  
  111.                     org.jfree.data.time.Month.class);  
  112.  
  113.             Iterator<PressureBean> it = list.iterator();  
  114.             while (it.hasNext()) {  
  115.                 PressureBean pres = it.next();  
  116.                 String date = pres.getBpDate();  
  117.  
  118.                 String[] spls = date.split("-");  
  119.                 if (spls.length == 2) {  
  120.                     ye = Integer.parseInt(spls[0]);  
  121.                     mon = Integer.parseInt(spls[1]);  
  122.                 } else {  
  123.                     log.error("the date of weeks is wrong");  
  124.                 }  
  125.  
  126.                 Month days = new Month(mon, ye);  
  127.  
  128.                 double sys = pres.getSyspress();  
  129.                 double dia = pres.getDiapress();  
  130.  
  131.                 if (sys != -1 && sys > 0) {  
  132.                     timeseries.add(days, sys);  
  133.                 } else {  
  134.                     timeseries1.add(days, null);  
  135.                 }  
  136.                 if (sys != -1 && sys > 0) {  
  137.                     timeseriedia.add(days, dia);  
  138.                 } else {  
  139.                     timeseriedia1.add(days, null);  
  140.                 }  
  141.  
  142.             }  
  143.             timesers.addSeries(timeseries);  
  144.             timesers.addSeries(timeseriedia);  
  145.             timesers.addSeries(timeseries1);  
  146.               
  147.             timesers.addSeries(timeseriedia1);  
  148.  
  149.         }  
  150.  
  151.         return timesers;  
  152.     } 

3、畫折線圖,兩個數據集,收縮壓和舒張壓,并且,這兩條曲線還各自包含一個區域范圍,并不單單是一條基準線,而是一個基準范圍。

  1. private static JFreeChart createChartPress(XYDataset xydataset,  
  2.             int weekOrmonth, String title, String y, String index, String week,  
  3.             String year, int searchby, String month, String nodatamess,  
  4.             List list, Log log, String bp_shou, String bp_shuzhang) {  
  5.  
  6.         // 有可能用戶在后面的版本中故意輸入不正常數值,但是為了保證圖片畫圖的完整,這里先計算  
  7.         // 用戶血壓值的***值。  
  8.  
  9.  
  10.         double maxpress = 0;  
  11.         double addmax = 50;  
  12.         double min = 40;  
  13.  
  14.         if (list != null && list.size() > 0) {  
  15.             Iterator<PressureBean> it = list.iterator();  
  16.             while (it.hasNext()) {  
  17.                 PressureBean pres = it.next();  
  18.                 double sys = pres.getSyspress();  
  19.                 double dia = pres.getDiapress();  
  20.  
  21.                 if (maxpress < sys) {  
  22.                     maxpress = sys;  
  23.  
  24.                 }  
  25.  
  26.                 if (maxpress < dia)  
  27.                     maxpress = dia;  
  28.  
  29.                 if (min > sys) {  
  30.                     min = sys;  
  31.                 }  
  32.  
  33.                 if (min > dia)  
  34.                     min = dia;  
  35.  
  36.             }  
  37.  
  38.             maxpress += addmax;  
  39.             min -= 10;  
  40.  
  41.  
  42.             log.info("high press value is =" + maxpress);  
  43.  
  44.         }  
  45.           
  46.         if (xydataset != null) {  
  47.             int counts = xydataset.getItemCount(0);  
  48.             if (counts == 0) {  
  49.                 xydataset = null;  
  50.             }  
  51.         }  
  52.  
  53.         JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title, "",  
  54.                 y, xydataset, truetruefalse);  
  55.         jfreechart.setBackgroundPaint(Color.white);  
  56.           
  57.  
  58.         // 設置標題的顏色  
  59.         TextTitle text = new TextTitle(title);  
  60.         text.setPaint(new Color(102102102));  
  61.         jfreechart.setTitle(text);  
  62.         XYPlot xyplot = jfreechart.getXYPlot();  
  63.         xyplot.setBackgroundPaint(new Color(255253246));  
  64.         xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細  
  65.         ValueAxis vaxis = xyplot.getDomainAxis();  
  66.         vaxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐標軸粗細  
  67.         vaxis.setAxisLinePaint(new Color(215215215)); // 坐標軸顏色  
  68.         xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細  
  69.         vaxis.setLabelPaint(new Color(101010)); // 坐標軸標題顏色  
  70.         vaxis.setTickLabelPaint(new Color(102102102)); // 坐標軸標尺值顏色  
  71.         vaxis.setLowerMargin(0.06d);// 分類軸下(左)邊距  
  72.         vaxis.setUpperMargin(0.14d);// 分類軸下(右)邊距,防止***邊的一個數據靠近了坐標軸。  
  73.           
  74.         //X軸為日期格式,這里是專門的處理日期的類,  
  75.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  76.         DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();  
  77.         if (weekOrmonth == 0) {//以天為刻度,時間格式為yyyy-MM-dd,如2008-02-06  
  78.             dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1, format));  
  79.         } else if (weekOrmonth == 1) {//以周為刻度,時間顯示為 2009年第4周((這里是SimpleDateFormat的用法,  
  80.             //這里為了作繁體版,英文版和簡體版,用了國際化處理,將這些可變的資源在文字資源里面,注意一下,這里的y,M、w是SimpleDateFormat的關鍵字,  
  81.             //如英文表示09年第3周就是09W3,那么,這里的W需要用‘’引起來)  
  82.             format = new SimpleDateFormat("yyyy" + year + index + "w" + week);  
  83.             dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7, format));  
  84.         } else if (weekOrmonth == 2) {//以月為刻度,時間顯示為09-02 (09年2月)  
  85.             format = new SimpleDateFormat("yy-MM");  
  86.             dateaxis  
  87.                     .setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1, format));  
  88.  
  89.         }  
  90.         dateaxis.setVerticalTickLabels(false); // 設為true表示橫坐標旋轉到垂直。  
  91.         if (searchby == 6 || searchby == 3) {  
  92.             dateaxis.setAutoTickUnitSelection(true); // 由于橫軸標簽過多,這里設置為自動格式 。  
  93.             dateaxis.setDateFormatOverride(format);  
  94.         }  
  95.         dateaxis.setTickMarkPosition(DateTickMarkPosition.START);  
  96.  
  97.         ValueAxis valueAxis = xyplot.getRangeAxis();  
  98.         valueAxis.setUpperBound(maxpress);  
  99.         valueAxis.setAutoRangeMinimumSize(1);  
  100.         valueAxis.setLowerBound(min);  
  101.         valueAxis.setAutoRange(false);  
  102.  
  103.         valueAxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐標軸粗細  
  104.         valueAxis.setAxisLinePaint(new Color(215215215)); // 坐標軸顏色  
  105.         valueAxis.setLabelPaint(new Color(101010)); // 坐標軸標題顏色  
  106.         valueAxis.setTickLabelPaint(new Color(102102102)); // 坐標軸標尺值顏色  
  107.           
  108.         xyplot.setRangeGridlinesVisible(true);  
  109.         xyplot.setDomainGridlinesVisible(true);  
  110.         xyplot.setRangeGridlinePaint(Color.LIGHT_GRAY);  
  111.         xyplot.setDomainGridlinePaint(Color.LIGHT_GRAY);  
  112.         xyplot.setBackgroundPaint(new Color(255253246));  
  113.         xyplot.setNoDataMessage(nodatamess);//沒有數據時顯示的文字說明。  
  114.         xyplot.setNoDataMessageFont(new Font("", Font.BOLD, 14));//字體的大小,粗體。  
  115.         xyplot.setNoDataMessagePaint(new Color(87149117));//字體顏色  
  116.         xyplot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 5d)); //  
  117.  
  118.         // add range marker(舒張壓的區域marker,范圍是從62到81)  
  119.  
  120.         double lowpress = 62;  
  121.         double uperpress = 81;  
  122.         IntervalMarker intermarker = new IntervalMarker(lowpress, uperpress);  
  123.         intermarker.setPaint(Color.decode("#66FFCC"));// 域顏色  
  124.           
  125.         intermarker.setLabelFont(new Font("SansSerif"4114));  
  126.         intermarker.setLabelPaint(Color.RED);  
  127.         intermarker.setLabel(bp_shuzhang);  
  128.  
  129.         if (xydataset != null) {  
  130.             xyplot.addRangeMarker(intermarker, Layer.BACKGROUND);  
  131.         }  
  132.     //(收縮壓的區域marker,范圍是從102到120)  
  133.         double lowpress1 = 102;  
  134.         double uperpress1 = 120;  
  135.         IntervalMarker inter = new IntervalMarker(lowpress1, uperpress1);  
  136.         inter.setLabelOffsetType(LengthAdjustmentType.EXPAND);  
  137.         inter.setPaint(Color.decode("#66FFCC"));// 域顏色  
  138.  
  139.  
  140.         inter.setLabelFont(new Font("SansSerif"4114));  
  141.         inter.setLabelPaint(Color.RED);  
  142.         inter.setLabel(bp_shou);  
  143.           
  144.         if (xydataset != null) {  
  145.             xyplot.addRangeMarker(inter, Layer.BACKGROUND); // 加上Layer.BACKGROUND,將maker調到折線下面。  
  146.         }  
  147.  
  148.         XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot  
  149.                 .getRenderer();  
  150.         //***條折線的顏色  
  151.         xylineandshaperenderer.setBaseItemLabelsVisible(true);  
  152.         xylineandshaperenderer.setSeriesFillPaint(0new Color(1271280));  
  153.         xylineandshaperenderer.setSeriesPaint(0new Color(1271280));  
  154.  
  155.         xylineandshaperenderer.setSeriesShapesVisible(0true);  
  156.         xylineandshaperenderer.setSeriesShapesVisible(1true);  
  157.  
  158.         //第二條折線的顏色  
  159.         xylineandshaperenderer.setSeriesFillPaint(1new Color(2541030));  
  160.         xylineandshaperenderer.setSeriesPaint(1new Color(2541030));  
  161.         xylineandshaperenderer.setSeriesShapesVisible(1true);  
  162.         xylineandshaperenderer.setSeriesVisible(2false);//  
  163.         xylineandshaperenderer.setSeriesVisible(3false);//不顯示下面標題  
  164.  
  165.         //折線的粗細調  
  166.         StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator();  
  167.         xylineandshaperenderer.setToolTipGenerator(xytool);  
  168.         xylineandshaperenderer.setStroke(new BasicStroke(1.5f));  
  169.  
  170.         // 顯示節點的值  
  171.         xylineandshaperenderer.setBaseItemLabelsVisible(true);  
  172.         xylineandshaperenderer  
  173.                 .setBasePositiveItemLabelPosition(new ItemLabelPosition(  
  174.                         ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));  
  175.         xylineandshaperenderer  
  176.                 .setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());  
  177.         xylineandshaperenderer.setBaseItemLabelPaint(new Color(102102102));// 顯示折點數值字體的顏色  
  178.  
  179.         return jfreechart;  
  180.     } 

4、將圖片URL返回到頁面

  1. public static void drawPressLineChart(IrisIoInterface io, Log log,  
  2.             TimeSeriesCollection timesers, int weekormonth, String title,  
  3.             String y, String index, String week, String year, int searchby,  
  4.             String month, String nodatamess, List list, String bp_shou,  
  5.             String bp_shuzhang) {  
  6.  
  7.         JFreeChart chart = createChartPress(timesers, weekormonth, title, y,  
  8.                 index, week, year, searchby, month, nodatamess, list, log,  
  9.                 bp_shou, bp_shuzhang);  
  10.  
  11.         HttpServletRequest request = io.getRequest();  
  12.         String filename = "";  
  13.         String graphURL = "";  
  14.         try {  
  15.             filename = ServletUtilities.saveChartAsPNG(chart, 650280null,  
  16.                     io.getSession());  
  17.             graphURL = request.getContextPath() + "/displayChart?filename=" 
  18.                     + filename;  
  19.         } catch (IOException e) {  
  20.             // TODO Auto-generated catch block  
  21.             e.printStackTrace();  
  22.             log.error(e);  
  23.         }  
  24.  
  25.         io.setData("filename1", filename, BeanShare.BEAN_SHARE_REQUEST);  
  26.         io.setData("presslineurl", graphURL, BeanShare.BEAN_SHARE_REQUEST);  
  27.  
  28.     } 

效果圖如下:

以天為刻度:

以周為刻度:

以月為刻度:

原文鏈接:http://juliana-only.iteye.com/blog/393266

【編輯推薦】

  1. JFreeChart***實踐:儀表盤
  2. JFreeChart***實踐:柱狀圖
  3. JFreeChart***實踐:3D餅圖
  4. JFreeChart***實踐:時序圖
  5. JFreeChart***實踐:甘特圖
責任編輯:林師授 來源: 遠去的渡口博客
相關推薦

2011-12-21 13:35:39

JavaJFreeChart

2011-12-21 13:52:27

JavaJFreeChart

2011-12-21 13:44:33

JavaJFreeChart

2011-12-21 14:15:08

JavaJFreeChart

2011-12-21 12:58:41

JavaJFreeChart

2011-12-21 14:34:33

JavaJFreeChart

2022-02-23 15:17:04

鴻蒙OpenHarmonJacascript

2011-12-21 12:46:43

2020-05-25 15:00:41

matplotlibplot()折線圖

2021-01-08 10:32:24

Charts折線圖數據可視化

2023-06-27 13:46:20

2023-11-10 18:07:42

Python折線圖折線

2022-11-07 08:42:50

iOS 16SwiftUI

2020-04-25 20:11:23

Python熱力圖代碼

2011-12-20 12:53:43

JavaJFreeChart

2022-11-18 09:03:09

SwiftUIiOS16

2023-07-21 01:12:30

Reactfalse?變量

2011-08-18 11:05:21

jQuery

2024-12-24 12:00:00

Matplotlib可視化分析Python

2021-03-05 07:03:38

Pyecharts可視化工具復合圖
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品久久精品 | 久久蜜桃av | 国产精品欧美精品日韩精品 | 亚洲精品aⅴ| 国外成人在线视频 | 亚洲欧美综合精品久久成人 | 久久成人精品一区二区三区 | 日韩在线欧美 | 成在线人视频免费视频 | 国产伦一区二区三区久久 | 国产婷婷 | 91在线视频网址 | 中文字幕99 | av影音资源 | 在线播放国产一区二区三区 | 日本成人区| 日本不卡一区 | 户外露出一区二区三区 | 欧美亚洲国产精品 | 成人久久18免费 | 噜啊噜在线 | 黄网免费看 | 激情 亚洲 | 99精品99| 中文字幕一区二区三区四区五区 | 亚洲免费视频一区二区 | 天天影视亚洲综合网 | 久久久久久高潮国产精品视 | 日韩av黄色 | 欧美国产精品 | 国产成人自拍一区 | 在线免费观看成年人视频 | 人人色视频 | 亚洲一二三在线观看 | 999观看免费高清www | 国产草草视频 | 亚洲激情在线观看 | 久久精品欧美视频 | 欧美日韩精品一区二区三区四区 | 欧美日韩成人一区二区 | 日韩三级在线 |