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

2022-02-23 15:17:04

鸿蒙OpenHarmonJacascript

2011-12-21 14:34:33

JavaJFreeChart

2020-05-25 15:00:41

matplotlibplot()折线图

2011-12-21 12:46:43

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热力图代码

2022-11-18 09:03:09

SwiftUIiOS16

2011-12-20 12:53:43

JavaJFreeChart

2011-08-18 11:05:21

jQuery

2023-07-21 01:12:30

Reactfalse​变量

2021-03-05 07:03:38

Pyecharts可视化工具复合图

2009-07-15 16:18:08

JSci.swing包
点赞
收藏

51CTO技术栈公众号