今天公司里分配给我的工作是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. Python paramiko模块 + 堡垒机

    paremiko SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: import paramiko    # 创建SSH对象 ssh = paramiko.SSHClient ...

  2. wp8.1 sdk preview 预览版

    http://pan.baidu.com/s/1hqyusja?qq-pf-to=pcqq.c2c#dir/path=%2FWPSDK%208.1%20DevPreview%20Installerwp ...

  3. 【LeetCode】Linked List Cycle(环形链表)

    这道题是LeetCode里的第141道题. 题目要求: 给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? 简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast, ...

  4. 一堆Offer怎么选?这样做就不纠结了

    有个朋友,工作了10年左右,春节后换工作,拿了三个Offer(西安): 通信行业的一家研究所,软件开发工程师,月薪7K,承诺有月奖金.年终奖金 一家做大数据的公司,软件开发工程师,月薪15K,13薪 ...

  5. OpenJ_Bailian——4115鸣人和佐助(带状态的A*)

    鸣人和佐助 Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status Desc ...

  6. 49道Spring面试题和答案

    49道Spring面试题和答案 Spring 概述 1. 什么是spring? Spring 是个Java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE ...

  7. Java中的自动类型转换

    Java里所有的数值型变量可以进行类型转换,这个大家都知道,应该不需要详细解释为什么. 2 在说明自动类型转换之前必须理解这样一个原则“表数范围小的可以向表数范围大的进行自动类型转换” 3 关于jav ...

  8. 【BZOJ3514】Codechef MARCH14 GERALD07加强版(LCT)

    题意:N个点M条边的无向图,q次询问保留图中编号在[l,r]的边的时候图中的联通块个数. 询问加密,强制在线 n,m,q<=200000 题意:RYZ作业 以下转载自hzwer http://h ...

  9. ADO:防止更新的数据含有单引号而出错

    原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] public void Update( string au_lname, string zip,string au ...

  10. 使用<sstream> 替代<stdio.h>

    c++ 字符串流 sstream(常用于格式转换)   使用stringstream对象简化类型转换C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更 ...