今天公司里分配给我的工作是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 = ;
  6. int day = ;
  7. int ye = ;
  8. int week = ;
  9. // 按天显示
  10. if (dayOrweekOrmonth == ) {
  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(, ));
  24. mon = Integer.parseInt(date.substring(, ));
  25. day = Integer.parseInt(date.substring(, date.length()));
  26. Day days = new Day(day, mon, ye);
  27. double sys = pres.getSyspress();
  28. double dia = pres.getDiapress();
  29. if (sys != - && sys > ) {
  30. timeseries.add(days, sys);
  31. } else {
  32. timeseries1.add(days, null);
  33. }
  34. if (sys != - && sys > ) {
  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 == ) {//按周显示
  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 == ) {
  59. ye = Integer.parseInt(spls[]);
  60. mon = Integer.parseInt(spls[]);
  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 != - && sys > ) {
  68. timeseries.add(days, sys);
  69. } else {
  70. timeseries1.add(days, null);
  71. }
  72. if (sys != - && sys > ) {
  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 == ) {
  97. ye = Integer.parseInt(spls[]);
  98. mon = Integer.parseInt(spls[]);
  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 != - && sys > ) {
  106. timeseries.add(days, sys);
  107. } else {
  108. timeseries1.add(days, null);
  109. }
  110. if (sys != - && sys > ) {
  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 = ;
  8. double addmax = ;
  9. double min = ;
  10. if (list != null && list.size() > ) {
  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 -= ;
  29. log.info("high press value is =" + maxpress);
  30. }
  31. if (xydataset != null) {
  32. int counts = xydataset.getItemCount();
  33. if (counts == ) {
  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(, , ));
  43. jfreechart.setTitle(text);
  44. XYPlot xyplot = jfreechart.getXYPlot();
  45. xyplot.setBackgroundPaint(new Color(, , ));
  46. xyplot.setOutlineStroke(new BasicStroke(.5f)); // 边框粗细
  47. ValueAxis vaxis = xyplot.getDomainAxis();
  48. vaxis.setAxisLineStroke(new BasicStroke(.5f)); // 坐标轴粗细
  49. vaxis.setAxisLinePaint(new Color(, , )); // 坐标轴颜色
  50. xyplot.setOutlineStroke(new BasicStroke(.5f)); // 边框粗细
  51. vaxis.setLabelPaint(new Color(, , )); // 坐标轴标题颜色
  52. vaxis.setTickLabelPaint(new Color(, , )); // 坐标轴标尺值颜色
  53. vaxis.setLowerMargin(.06d);// 分类轴下(左)边距
  54. vaxis.setUpperMargin(.14d);// 分类轴下(右)边距,防止最后边的一个数据靠近了坐标轴。
  55. //X轴为日期格式,这里是专门的处理日期的类,
  56. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  57. DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
  58. if (weekOrmonth == ) {//以天为刻度。时间格式为yyyy-MM-dd,如2008-02-06
  59. dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, , format));
  60. } else if (weekOrmonth == ) {//以周为刻度。时间显示为 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, , format));
  65. } else if (weekOrmonth == ) {//以月为刻度,时间显示为09-02 (09年2月)
  66. format = new SimpleDateFormat("yy-MM");
  67. dateaxis
  68. .setTickUnit(new DateTickUnit(DateTickUnit.MONTH, , format));
  69. }
  70. dateaxis.setVerticalTickLabels(false); // 设为true表示横坐标旋转到垂直。
  71. if (searchby ==  || searchby == ) {
  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();
  79. valueAxis.setLowerBound(min);
  80. valueAxis.setAutoRange(false);
  81. valueAxis.setAxisLineStroke(new BasicStroke(.5f)); // 坐标轴粗细
  82. valueAxis.setAxisLinePaint(new Color(, , )); // 坐标轴颜色
  83. valueAxis.setLabelPaint(new Color(, , )); // 坐标轴标题颜色
  84. valueAxis.setTickLabelPaint(new Color(, , )); // 坐标轴标尺值颜色
  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(, , ));
  90. xyplot.setNoDataMessage(nodatamess);//没有数据时显示的文字说明。
  91. xyplot.setNoDataMessageFont(new Font("", Font.BOLD, ));//字体的大小。粗体。
  92. xyplot.setNoDataMessagePaint(new Color(, , ));//字体颜色
  93. xyplot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 5d)); //
  94. // add range marker(舒张压的区域marker,范围是从62到81)
  95. double lowpress = ;
  96. double uperpress = ;
  97. IntervalMarker intermarker = new IntervalMarker(lowpress, uperpress);
  98. intermarker.setPaint(Color.decode("#66FFCC"));// 域顏色
  99. intermarker.setLabelFont(new Font("SansSerif", , ));
  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 = ;
  107. double uperpress1 = ;
  108. IntervalMarker inter = new IntervalMarker(lowpress1, uperpress1);
  109. inter.setLabelOffsetType(LengthAdjustmentType.EXPAND);
  110. inter.setPaint(Color.decode("#66FFCC"));// 域顏色
  111. inter.setLabelFont(new Font("SansSerif", , ));
  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(, new Color(, , ));
  122. xylineandshaperenderer.setSeriesPaint(, new Color(, , ));
  123. xylineandshaperenderer.setSeriesShapesVisible(, true);
  124. xylineandshaperenderer.setSeriesShapesVisible(, true);
  125. //第二条折线的颜色
  126. xylineandshaperenderer.setSeriesFillPaint(, new Color(, , ));
  127. xylineandshaperenderer.setSeriesPaint(, new Color(, , ));
  128. xylineandshaperenderer.setSeriesShapesVisible(, true);
  129. xylineandshaperenderer.setSeriesVisible(, false);//
  130. xylineandshaperenderer.setSeriesVisible(, false);//不显示以下标题
  131. //折线的粗细调
  132. StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator();
  133. xylineandshaperenderer.setToolTipGenerator(xytool);
  134. xylineandshaperenderer.setStroke(new BasicStroke(.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(, , ));// 显示折点数值字体的颜色
  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, , , 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-李鹏飞的更多相关文章

  1. jfreechart折线图 demo

    public class ChartUtil { public static ChartUtil chartUtil; private RoomViewsDataService roomViewsDa ...

  2. java利用JFreeChart实现各种数据统计图(柱形图,饼图,折线图)

    最近在做数据挖掘的课程设计,需要将数据分析的结果很直观的展现给用户,这就要用到数据统计图,要实现这个功能就需要几个第三方包了: 1.       jfreechart-1.0.13.jar 2.    ...

  3. struts2整合JFreechart 饼图、折线图、柱形图

    struts2整合JFreechart 饼图.折线图.柱形图 上效果图: 当然可以将数据导出图片格式存储.具体下的链接里的文件有保存成图片的操作. 因为是strust2整合JFreechart,所以s ...

  4. JFreeChart在制作折线图

    JFreeChart在制作折线图的时候可以使用两种不同的方式 package Line; import java.awt.Color; import java.awt.Font; import org ...

  5. JFreeChart 图表生成实例(饼图、柱状图、折线图、时序图)

    import java.awt.BasicStroke; import java.awt.Color; import java.io.FileOutputStream; import java.io. ...

  6. JFreeChart绘制折线图实例

    JFreeChart是JAVA平台上的一个开放的第三方图表绘制类库.只要下载JFreeChart的类库,导入项目即可使用.下面是一个绘制折线图的实例.各处注释都已经写的比较清晰了. package c ...

  7. 使用jfreechart生成柱状图、折线图、和饼状图

    JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计.下面我就详细介绍如 ...

  8. JFreeChart绘制XY折线图(工具类设计)

    准备用Java写通信的仿真平台作为毕业设计,相比matlab绘图,Java绘图需要自己去写很多工具类,博主在这采用了JFreeChart的开源解决方案,摸索着自己写了一个XY折线图工具类,话不多说贴源 ...

  9. JFreeChart 之折线图

    JFreeChart 之折线图 一.JFreeChart 简介 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications, applets ...

随机推荐

  1. CentOS7搭建Maven的Nexus私服仓库

    1.下载nexus 打开一下链接: https://www.sonatype.com/nexus-repository-oss 下载安装包. 2.解压安装包 tar zxvf nexus-3.9.0- ...

  2. linux open()文件操作

    python程序中经常用到的读文件: f = open("___", 'r') for line in f:#这里每次读取文件的一行,line为字符串,串尾包括了'\n'!!!   ...

  3. BS4(BeautifulSoup4)的使用--find_all()篇

    可以直接参考 BS4文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all 注意的是: 1.有些 ...

  4. 在windows下使用Cygwin模拟unix环境,并安装apt-cyg,svn等工具

    在windows下使用Cygwin模拟unix环境,并安装apt-cyg,svn等工具 一.Cygwin的安装 1. 下载Cygwin,这个可以到这里下载 ,至于使用32位的还是64位的版本可以根据自 ...

  5. LA 3263 平面划分

    Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...

  6. Vxlan简介

    1.为什么需要Vxlan 1.什么是VXLAN VXLAN(Virtual eXtensible LAN可扩展虚拟局域网),是一种mac in UDP技术.传统的二层帧被封装到了UDP的报文中,通过U ...

  7. java面

    常被问到的十个 Java 面试题 每周 10 道 Java 面试题 : 面向对象, 类加载器, JDBC, Spring 基础概念 Java 面试题问与答:编译时与运行时 java面试基础1 java ...

  8. python中单引号,双引号,三引号的比较 转载

    本文转载自http://blog.sina.com.cn/s/blog_6be8928401017lwy.html 先说1双引号与3个双引号的区别,双引号所表示的字 符串通常要写成一行 如: s1 = ...

  9. python的__name__和dir()属性

    1.__name__属性 一个模块被另一个程序第一次引入时,其主程序将运行.如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身运行时执行.示例 ...

  10. python和linux的环境设置/PATH

    一.python的环境设置 1.输出path列表: pi@raspberrypi:~$ pythonPython 3.4.0 (default, Jul 1 2014, 09:37:01) [GCC ...