溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何使用JFreeChart實(shí)現(xiàn)折線(xiàn)圖

發(fā)布時(shí)間:2022-03-31 14:42:03 來(lái)源:億速云 閱讀:887 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下如何使用JFreeChart實(shí)現(xiàn)折線(xiàn)圖,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

折線(xiàn)圖,大可分為兩種,

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

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

1、時(shí)間要連續(xù)。

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

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

下面,針對(duì)比較復(fù)雜的(2)來(lái)講解:

1、取到業(yè)務(wù)邏輯需要的數(shù)據(jù):(具體過(guò)程就不啰嗦了,就是查詢(xún)數(shù)據(jù)庫(kù),得到想要的字段的值,加載到List里面) 返回List<PressureBean>

PressureBean的包含的屬性:

int userId;      String bpDate;      String bpTime;      int syspress;  //收縮壓(mmHg)      int diapress; //舒張壓(mmHg)

2、加載數(shù)據(jù)集

public static TimeSeriesCollection createTimeSeries(              List<PressureBean> list, int dayOrweekOrmonth, Log log, String shou,String shu              ) {           TimeSeriesCollection timesers = new TimeSeriesCollection();           int mon = 1;          int day = 1;          int ye = 2000;          int week = 1;           // 按天顯示          if (dayOrweekOrmonth == 0) {               TimeSeries timeseries = new TimeSeries(shou,                      org.jfree.data.time.Day.class);              TimeSeries timeseries1 = new TimeSeries("c1",                      org.jfree.data.time.Day.class);               TimeSeries timeseriedia = new TimeSeries(shu,                      org.jfree.data.time.Day.class);              TimeSeries timeseriedia1 = new TimeSeries("d1",                      org.jfree.data.time.Day.class);               Iterator<PressureBean> it = list.iterator();              while (it.hasNext()) {                  PressureBean pres = it.next();                  String date = pres.getBpDate();                   ye = Integer.parseInt(date.substring(0, 4));                  mon = Integer.parseInt(date.substring(5, 7));                  day = Integer.parseInt(date.substring(8, date.length()));                  Day days = new Day(day, mon, ye);                   double sys = pres.getSyspress();                  double dia = pres.getDiapress();                  if (sys != -1 && sys > 0) {                      timeseries.add(days, sys);                  } else {                      timeseries1.add(days, null);                  }                  if (sys != -1 && sys > 0) {                      timeseriedia.add(days, dia);                  } else {                      timeseriedia1.add(days, null);                  }               }               timesers.addSeries(timeseries);              timesers.addSeries(timeseriedia);              timesers.addSeries(timeseries1);              timesers.addSeries(timeseriedia1);           } else if (dayOrweekOrmonth == 1) {//按周顯示              TimeSeries timeseries = new TimeSeries(shou,                      org.jfree.data.time.Week.class);              TimeSeries timeseries1 = new TimeSeries("c1",                      org.jfree.data.time.Week.class);               TimeSeries timeseriedia = new TimeSeries(shu,                      org.jfree.data.time.Week.class);              TimeSeries timeseriedia1 = new TimeSeries("d1",                      org.jfree.data.time.Week.class);               Iterator<PressureBean> it = list.iterator();              while (it.hasNext()) {                  PressureBean pres = it.next();                  String date = pres.getBpDate();                   String[] spls = date.split("-");                  if (spls.length == 2) {                      ye = Integer.parseInt(spls[0]);                      mon = Integer.parseInt(spls[1]);                  } else {                      log.error("the date of weeks is wrong");                  }                   Week days = new Week(mon, ye);                  double sys = pres.getSyspress();                  double dia = pres.getDiapress();                   if (sys != -1 && sys > 0) {                      timeseries.add(days, sys);                  } else {                      timeseries1.add(days, null);                  }                  if (sys != -1 && sys > 0) {                      timeseriedia.add(days, dia);                  } else {                      timeseriedia1.add(days, null);                  }               }               timesers.addSeries(timeseries);              timesers.addSeries(timeseriedia);              timesers.addSeries(timeseries1);                            timesers.addSeries(timeseriedia1);           } else {//按月顯示              TimeSeries timeseries = new TimeSeries(shou,                      org.jfree.data.time.Month.class);              TimeSeries timeseries1 = new TimeSeries("c1",                      org.jfree.data.time.Month.class);               TimeSeries timeseriedia = new TimeSeries(shu,                      org.jfree.data.time.Month.class);              TimeSeries timeseriedia1 = new TimeSeries("s",                      org.jfree.data.time.Month.class);               Iterator<PressureBean> it = list.iterator();              while (it.hasNext()) {                  PressureBean pres = it.next();                  String date = pres.getBpDate();                   String[] spls = date.split("-");                  if (spls.length == 2) {                      ye = Integer.parseInt(spls[0]);                      mon = Integer.parseInt(spls[1]);                  } else {                      log.error("the date of weeks is wrong");                  }                   Month days = new Month(mon, ye);                   double sys = pres.getSyspress();                  double dia = pres.getDiapress();                   if (sys != -1 && sys > 0) {                      timeseries.add(days, sys);                  } else {                      timeseries1.add(days, null);                  }                  if (sys != -1 && sys > 0) {                      timeseriedia.add(days, dia);                  } else {                      timeseriedia1.add(days, null);                  }               }              timesers.addSeries(timeseries);              timesers.addSeries(timeseriedia);              timesers.addSeries(timeseries1);                            timesers.addSeries(timeseriedia1);           }           return timesers;      }

3、畫(huà)折線(xiàn)圖,兩個(gè)數(shù)據(jù)集,收縮壓和舒張壓,并且,這兩條曲線(xiàn)還各自包含一個(gè)區(qū)域范圍,并不單單是一條基準(zhǔn)線(xiàn),而是一個(gè)基準(zhǔn)范圍。

private static JFreeChart createChartPress(XYDataset xydataset,              int weekOrmonth, String title, String y, String index, String week,              String year, int searchby, String month, String nodatamess,              List list, Log log, String bp_shou, String bp_shuzhang) {           // 有可能用戶(hù)在后面的版本中故意輸入不正常數(shù)值,但是為了保證圖片畫(huà)圖的完整,這里先計(jì)算          // 用戶(hù)血壓值的***值。            double maxpress = 0;          double addmax = 50;          double min = 40;           if (list != null && list.size() > 0) {              Iterator<PressureBean> it = list.iterator();              while (it.hasNext()) {                  PressureBean pres = it.next();                  double sys = pres.getSyspress();                  double dia = pres.getDiapress();                   if (maxpress < sys) {                      maxpress = sys;                   }                   if (maxpress < dia)                      maxpress = dia;                   if (min > sys) {                      min = sys;                  }                   if (min > dia)                      min = dia;               }               maxpress += addmax;              min -= 10;                log.info("high press value is =" + maxpress);           }                    if (xydataset != null) {              int counts = xydataset.getItemCount(0);              if (counts == 0) {                  xydataset = null;              }          }           JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title, "",                  y, xydataset, true, true, false);          jfreechart.setBackgroundPaint(Color.white);                     // 設(shè)置標(biāo)題的顏色          TextTitle text = new TextTitle(title);          text.setPaint(new Color(102, 102, 102));          jfreechart.setTitle(text);          XYPlot xyplot = jfreechart.getXYPlot();          xyplot.setBackgroundPaint(new Color(255, 253, 246));          xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細(xì)          ValueAxis vaxis = xyplot.getDomainAxis();          vaxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐標(biāo)軸粗細(xì)          vaxis.setAxisLinePaint(new Color(215, 215, 215)); // 坐標(biāo)軸顏色          xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細(xì)          vaxis.setLabelPaint(new Color(10, 10, 10)); // 坐標(biāo)軸標(biāo)題顏色          vaxis.setTickLabelPaint(new Color(102, 102, 102)); // 坐標(biāo)軸標(biāo)尺值顏色          vaxis.setLowerMargin(0.06d);// 分類(lèi)軸下(左)邊距          vaxis.setUpperMargin(0.14d);// 分類(lèi)軸下(右)邊距,防止***邊的一個(gè)數(shù)據(jù)靠近了坐標(biāo)軸。                    //X軸為日期格式,這里是專(zhuān)門(mén)的處理日期的類(lèi),          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");          DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();          if (weekOrmonth == 0) {//以天為刻度,時(shí)間格式為yyyy-MM-dd,如2008-02-06              dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1, format));          } else if (weekOrmonth == 1) {//以周為刻度,時(shí)間顯示為 2009年第4周((這里是SimpleDateFormat的用法,              //這里為了作繁體版,英文版和簡(jiǎn)體版,用了國(guó)際化處理,將這些可變的資源在文字資源里面,注意一下,這里的y,M、w是SimpleDateFormat的關(guān)鍵字,              //如英文表示09年第3周就是09W3,那么,這里的W需要用&lsquo;&rsquo;引起來(lái))              format = new SimpleDateFormat("yyyy" + year + index + "w" + week);              dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7, format));          } else if (weekOrmonth == 2) {//以月為刻度,時(shí)間顯示為09-02 (09年2月)              format = new SimpleDateFormat("yy-MM");              dateaxis                      .setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1, format));           }          dateaxis.setVerticalTickLabels(false); // 設(shè)為true表示橫坐標(biāo)旋轉(zhuǎn)到垂直。          if (searchby == 6 || searchby == 3) {              dateaxis.setAutoTickUnitSelection(true); // 由于橫軸標(biāo)簽過(guò)多,這里設(shè)置為自動(dòng)格式 。              dateaxis.setDateFormatOverride(format);          }          dateaxis.setTickMarkPosition(DateTickMarkPosition.START);           ValueAxis valueAxis = xyplot.getRangeAxis();          valueAxis.setUpperBound(maxpress);          valueAxis.setAutoRangeMinimumSize(1);          valueAxis.setLowerBound(min);          valueAxis.setAutoRange(false);           valueAxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐標(biāo)軸粗細(xì)          valueAxis.setAxisLinePaint(new Color(215, 215, 215)); // 坐標(biāo)軸顏色          valueAxis.setLabelPaint(new Color(10, 10, 10)); // 坐標(biāo)軸標(biāo)題顏色          valueAxis.setTickLabelPaint(new Color(102, 102, 102)); // 坐標(biāo)軸標(biāo)尺值顏色                    xyplot.setRangeGridlinesVisible(true);          xyplot.setDomainGridlinesVisible(true);          xyplot.setRangeGridlinePaint(Color.LIGHT_GRAY);          xyplot.setDomainGridlinePaint(Color.LIGHT_GRAY);          xyplot.setBackgroundPaint(new Color(255, 253, 246));          xyplot.setNoDataMessage(nodatamess);//沒(méi)有數(shù)據(jù)時(shí)顯示的文字說(shuō)明。          xyplot.setNoDataMessageFont(new Font("", Font.BOLD, 14));//字體的大小,粗體。          xyplot.setNoDataMessagePaint(new Color(87, 149, 117));//字體顏色          xyplot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 5d)); //           // add range marker(舒張壓的區(qū)域marker,范圍是從62到81)           double lowpress = 62;          double uperpress = 81;          IntervalMarker intermarker = new IntervalMarker(lowpress, uperpress);          intermarker.setPaint(Color.decode("#66FFCC"));// 域顏色                    intermarker.setLabelFont(new Font("SansSerif", 41, 14));          intermarker.setLabelPaint(Color.RED);          intermarker.setLabel(bp_shuzhang);           if (xydataset != null) {              xyplot.addRangeMarker(intermarker, Layer.BACKGROUND);          }      //(收縮壓的區(qū)域marker,范圍是從102到120)          double lowpress1 = 102;          double uperpress1 = 120;          IntervalMarker inter = new IntervalMarker(lowpress1, uperpress1);          inter.setLabelOffsetType(LengthAdjustmentType.EXPAND);          inter.setPaint(Color.decode("#66FFCC"));// 域顏色            inter.setLabelFont(new Font("SansSerif", 41, 14));          inter.setLabelPaint(Color.RED);          inter.setLabel(bp_shou);                    if (xydataset != null) {              xyplot.addRangeMarker(inter, Layer.BACKGROUND); // 加上Layer.BACKGROUND,將maker調(diào)到折線(xiàn)下面。          }           XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot                  .getRenderer();          //***條折線(xiàn)的顏色          xylineandshaperenderer.setBaseItemLabelsVisible(true);          xylineandshaperenderer.setSeriesFillPaint(0, new Color(127, 128, 0));          xylineandshaperenderer.setSeriesPaint(0, new Color(127, 128, 0));           xylineandshaperenderer.setSeriesShapesVisible(0, true);          xylineandshaperenderer.setSeriesShapesVisible(1, true);           //第二條折線(xiàn)的顏色          xylineandshaperenderer.setSeriesFillPaint(1, new Color(254, 103, 0));          xylineandshaperenderer.setSeriesPaint(1, new Color(254, 103, 0));          xylineandshaperenderer.setSeriesShapesVisible(1, true);          xylineandshaperenderer.setSeriesVisible(2, false);//          xylineandshaperenderer.setSeriesVisible(3, false);//不顯示下面標(biāo)題           //折線(xiàn)的粗細(xì)調(diào)          StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator();          xylineandshaperenderer.setToolTipGenerator(xytool);          xylineandshaperenderer.setStroke(new BasicStroke(1.5f));           // 顯示節(jié)點(diǎn)的值          xylineandshaperenderer.setBaseItemLabelsVisible(true);          xylineandshaperenderer                  .setBasePositiveItemLabelPosition(new ItemLabelPosition(                          ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));          xylineandshaperenderer                  .setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());          xylineandshaperenderer.setBaseItemLabelPaint(new Color(102, 102, 102));// 顯示折點(diǎn)數(shù)值字體的顏色           return jfreechart;      }

4、將圖片URL返回到頁(yè)面

public static void drawPressLineChart(IrisIoInterface io, Log log,              TimeSeriesCollection timesers, int weekormonth, String title,              String y, String index, String week, String year, int searchby,              String month, String nodatamess, List list, String bp_shou,              String bp_shuzhang) {           JFreeChart chart = createChartPress(timesers, weekormonth, title, y,                  index, week, year, searchby, month, nodatamess, list, log,                  bp_shou, bp_shuzhang);           HttpServletRequest request = io.getRequest();          String filename = "";          String graphURL = "";          try {              filename = ServletUtilities.saveChartAsPNG(chart, 650, 280, null,                      io.getSession());              graphURL = request.getContextPath() + "/displayChart?filename="                     + filename;          } catch (IOException e) {              // TODO Auto-generated catch block              e.printStackTrace();              log.error(e);          }           io.setData("filename1", filename, BeanShare.BEAN_SHARE_REQUEST);          io.setData("presslineurl", graphURL, BeanShare.BEAN_SHARE_REQUEST);       }

效果圖如下:

以天為刻度:

如何使用JFreeChart實(shí)現(xiàn)折線(xiàn)圖

以周為刻度:

如何使用JFreeChart實(shí)現(xiàn)折線(xiàn)圖

以月為刻度:

如何使用JFreeChart實(shí)現(xiàn)折線(xiàn)圖

以上是“如何使用JFreeChart實(shí)現(xiàn)折線(xiàn)圖”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI