1. 程式人生 > >JfreeChart折線圖 CSDN-李鵬飛

JfreeChart折線圖 CSDN-李鵬飛

.net comment ans minimum sage 日期格式 .info n) 學校


今天公司裏分配給我的工作是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的包括的屬性:

Java代碼

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

2、載入數據集

Java代碼

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

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

Java代碼

  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. double maxpress = 0;
  8. double addmax = 50;
  9. double min = 40;
  10. if (list != null && list.size() > 0) {
  11. Iterator<PressureBean> it = list.iterator();
  12. while (it.hasNext()) {
  13. PressureBean pres = it.next();
  14. double sys = pres.getSyspress();
  15. double dia = pres.getDiapress();
  16. if (maxpress < sys) {
  17. maxpress = sys;
  18. }
  19. if (maxpress < dia)
  20. maxpress = dia;
  21. if (min > sys) {
  22. min = sys;
  23. }
  24. if (min > dia)
  25. min = dia;
  26. }
  27. maxpress += addmax;
  28. min -= 10;
  29. log.info("high press value is =" + maxpress);
  30. }
  31. if (xydataset != null) {
  32. int counts = xydataset.getItemCount(0);
  33. if (counts == 0) {
  34. xydataset = null;
  35. }
  36. }
  37. JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title, "",
  38. y, xydataset, true, true, false);
  39. jfreechart.setBackgroundPaint(Color.white);
  40. // 設置標題的顏色
  41. TextTitle text = new TextTitle(title);
  42. text.setPaint(new Color(102, 102, 102));
  43. jfreechart.setTitle(text);
  44. XYPlot xyplot = jfreechart.getXYPlot();
  45. xyplot.setBackgroundPaint(new Color(255, 253, 246));
  46. xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細
  47. ValueAxis vaxis = xyplot.getDomainAxis();
  48. vaxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐標軸粗細
  49. vaxis.setAxisLinePaint(new Color(215, 215, 215)); // 坐標軸顏色
  50. xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細
  51. vaxis.setLabelPaint(new Color(10, 10, 10)); // 坐標軸標題顏色
  52. vaxis.setTickLabelPaint(new Color(102, 102, 102)); // 坐標軸標尺值顏色
  53. vaxis.setLowerMargin(0.06d);// 分類軸下(左)邊距
  54. vaxis.setUpperMargin(0.14d);// 分類軸下(右)邊距,防止最後邊的一個數據靠近了坐標軸。
  55. //X軸為日期格式,這裏是專門的處理日期的類,
  56. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  57. DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
  58. if (weekOrmonth == 0) {//以天為刻度。時間格式為yyyy-MM-dd,如2008-02-06
  59. dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1, format));
  60. } else if (weekOrmonth == 1) {//以周為刻度。時間顯示為 2009年第4周((這裏是SimpleDateFormat的使用方法,
  61. //這裏為了作繁體版,英文版和簡體版,用了國際化處理,將這些可變的資源在文字資源裏面,註意一下。這裏的y。M、w是SimpleDateFormat的關鍵字。
  62. //如英文表示09年第3周就是09W3,那麽,這裏的W須要用‘’引起來)
  63. format = new SimpleDateFormat("yyyy" + year + index + "w" + week);
  64. dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7, format));
  65. } else if (weekOrmonth == 2) {//以月為刻度,時間顯示為09-02 (09年2月)
  66. format = new SimpleDateFormat("yy-MM");
  67. dateaxis
  68. .setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1, format));
  69. }
  70. dateaxis.setVerticalTickLabels(false); // 設為true表示橫坐標旋轉到垂直。
  71. if (searchby == 6 || searchby == 3) {
  72. dateaxis.setAutoTickUnitSelection(true); // 因為橫軸標簽過多,這裏設置為自己主動格式 。
  73. dateaxis.setDateFormatOverride(format);
  74. }
  75. dateaxis.setTickMarkPosition(DateTickMarkPosition.START);
  76. ValueAxis valueAxis = xyplot.getRangeAxis();
  77. valueAxis.setUpperBound(maxpress);
  78. valueAxis.setAutoRangeMinimumSize(1);
  79. valueAxis.setLowerBound(min);
  80. valueAxis.setAutoRange(false);
  81. valueAxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐標軸粗細
  82. valueAxis.setAxisLinePaint(new Color(215, 215, 215)); // 坐標軸顏色
  83. valueAxis.setLabelPaint(new Color(10, 10, 10)); // 坐標軸標題顏色
  84. valueAxis.setTickLabelPaint(new Color(102, 102, 102)); // 坐標軸標尺值顏色
  85. xyplot.setRangeGridlinesVisible(true);
  86. xyplot.setDomainGridlinesVisible(true);
  87. xyplot.setRangeGridlinePaint(Color.LIGHT_GRAY);
  88. xyplot.setDomainGridlinePaint(Color.LIGHT_GRAY);
  89. xyplot.setBackgroundPaint(new Color(255, 253, 246));
  90. xyplot.setNoDataMessage(nodatamess);//沒有數據時顯示的文字說明。
  91. xyplot.setNoDataMessageFont(new Font("", Font.BOLD, 14));//字體的大小。粗體。
  92. xyplot.setNoDataMessagePaint(new Color(87, 149, 117));//字體顏色
  93. xyplot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 5d)); //
  94. // add range marker(舒張壓的區域marker,範圍是從62到81)
  95. double lowpress = 62;
  96. double uperpress = 81;
  97. IntervalMarker intermarker = new IntervalMarker(lowpress, uperpress);
  98. intermarker.setPaint(Color.decode("#66FFCC"));// 域顏色
  99. intermarker.setLabelFont(new Font("SansSerif", 41, 14));
  100. intermarker.setLabelPaint(Color.RED);
  101. intermarker.setLabel(bp_shuzhang);
  102. if (xydataset != null) {
  103. xyplot.addRangeMarker(intermarker, Layer.BACKGROUND);
  104. }
  105. //(收縮壓的區域marker。範圍是從102到120)
  106. double lowpress1 = 102;
  107. double uperpress1 = 120;
  108. IntervalMarker inter = new IntervalMarker(lowpress1, uperpress1);
  109. inter.setLabelOffsetType(LengthAdjustmentType.EXPAND);
  110. inter.setPaint(Color.decode("#66FFCC"));// 域顏色
  111. inter.setLabelFont(new Font("SansSerif", 41, 14));
  112. inter.setLabelPaint(Color.RED);
  113. inter.setLabel(bp_shou);
  114. if (xydataset != null) {
  115. xyplot.addRangeMarker(inter, Layer.BACKGROUND); // 加上Layer.BACKGROUND,將maker調到折線以下。

  116. }
  117. XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
  118. .getRenderer();
  119. //第一條折線的顏色
  120. xylineandshaperenderer.setBaseItemLabelsVisible(true);
  121. xylineandshaperenderer.setSeriesFillPaint(0, new Color(127, 128, 0));
  122. xylineandshaperenderer.setSeriesPaint(0, new Color(127, 128, 0));
  123. xylineandshaperenderer.setSeriesShapesVisible(0, true);
  124. xylineandshaperenderer.setSeriesShapesVisible(1, true);
  125. //第二條折線的顏色
  126. xylineandshaperenderer.setSeriesFillPaint(1, new Color(254, 103, 0));
  127. xylineandshaperenderer.setSeriesPaint(1, new Color(254, 103, 0));
  128. xylineandshaperenderer.setSeriesShapesVisible(1, true);
  129. xylineandshaperenderer.setSeriesVisible(2, false);//
  130. xylineandshaperenderer.setSeriesVisible(3, false);//不顯示以下標題
  131. //折線的粗細調
  132. StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator();
  133. xylineandshaperenderer.setToolTipGenerator(xytool);
  134. xylineandshaperenderer.setStroke(new BasicStroke(1.5f));
  135. // 顯示節點的值
  136. xylineandshaperenderer.setBaseItemLabelsVisible(true);
  137. xylineandshaperenderer
  138. .setBasePositiveItemLabelPosition(new ItemLabelPosition(
  139. ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
  140. xylineandshaperenderer
  141. .setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
  142. xylineandshaperenderer.setBaseItemLabelPaint(new Color(102, 102, 102));// 顯示折點數值字體的顏色
  143. return jfreechart;
  144. }

4、將圖片URL返回到頁面

Java代碼

  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. JFreeChart chart = createChartPress(timesers, weekormonth, title, y,
  7. index, week, year, searchby, month, nodatamess, list, log,
  8. bp_shou, bp_shuzhang);
  9. HttpServletRequest request = io.getRequest();
  10. String filename = "";
  11. String graphURL = "";
  12. try {
  13. filename = ServletUtilities.saveChartAsPNG(chart, 650, 280, null,
  14. io.getSession());
  15. graphURL = request.getContextPath() + "/displayChart?filename="
  16. + filename;
  17. } catch (IOException e) {
  18. // TODO Auto-generated catch block
  19. e.printStackTrace();
  20. log.error(e);
  21. }
  22. io.setData("filename1", filename, BeanShare.BEAN_SHARE_REQUEST);
  23. io.setData("presslineurl", graphURL, BeanShare.BEAN_SHARE_REQUEST);
  24. }

技術分享

技術分享

技術分享



如有轉載請註明出處:版權全部---李鵬飛 2014/6/3日

心情日誌:聽著歌寫著博客!今天第一天進公司開始興趣昂昂的走進公司!今天第一天上班公司經理就分配了工作但是框架都沒熟悉又和在CSDN樂知學院學到的SSH框架不一樣所以感慨道離開老師離開學校真的不習慣。

??

JfreeChart折線圖 CSDN-李鵬飛