JFreeChart最佳实践:散点图

开发 后端
本文将介绍作者通过Java最佳图形解决方案JFreeChart实现散点图的详细过程。

用JfreeChart画散点图,查看JfreeChart的Demo,写的都挺复杂的,关键是Demo中把简单的事情复杂化了,比如展示的例子是一个正弦曲线什么的,让初次画散点图的我们摸不着头脑。关键是他们得到数据集搞得太过复杂,后来想明白了,不就是二维数组嘛。想通了这一点,一切问题都解决了,不过,对于我们项目的特殊要求,并不是只画几个点那么简单,还要加上区域范围与文字说明,在查看文档及自己摸索下,2天时间,终于搞定。下面分享一下成果,呵,还是有点成就感的。

首先,看画图的API,参数有:

ChartFactory.createScatterPlot(),其中,有一个xydataset,那么,我们先要知道这个xydataset是什么结构的,再看所需xydataset,散点图,也就是单独画出点,也就是一个二维数据了,x ,y坐标嘛!

那么,先做好准备工作,***步,拿数据,这就不用啰嗦了,就是得到一个List也好,Set也行。

第二步,加载到数据集:

  1. /**  
  2.  *   
  3.  * @param xydatalist  
  4.  * @param bloods  
  5.  * @return  
  6.  */ 
  7.     public static XYDataset createxydataset(List<PressureBean> xydatalist,  
  8.             String bloods) {  
  9.         DefaultXYDataset xydataset = new DefaultXYDataset();  
  10.  
  11.         int size = xydatalist.size();  
  12.         double[][] datas = new double[2][size];  
  13.         for (int i = 0; i < size; i++) {  
  14.             PressureBean pres = xydatalist.get(i);  
  15.             int sys = pres.getSyspress();//收缩压  
  16.             int dia = pres.getDiapress();//舒张压  
  17.  
  18.             datas[0][i] = sys;  
  19.             datas[1][i] = dia;  
  20.         }  
  21.  
  22.         xydataset.addSeries(bloods, datas);  
  23.  
  24.         return xydataset;  
  25.  
  26.     } 

下一步,另外一个准备工作,画图方法:

  1. public static JFreeChart createChart(XYDataset xydataset,  
  2.             String bloodcattile, String shou, String shu, String nobloodData,  
  3.             String bloods, String nomal, String fore, String one, String two,  
  4.             List<PressureBean> list, Log log) {  
  5.  
  6.         // 有可能用户在后面的版本中故意输入不正常数值,但是为了保证图片画图的完整,这里先计算  
  7.         // 用户血压值的***值。  
  8.         int maxpress = 160;  
  9.         int addmax = 20;  
  10.  
  11.         if (list != null && list.size() > 0) {  
  12.  
  13.             Iterator<PressureBean> it = list.iterator();  
  14.             while (it.hasNext()) {  
  15.                 PressureBean pres = it.next();  
  16.                   
  17.                 if (maxpress < pres.getDiapress()) {  
  18.                     maxpress = pres.getDiapress();  
  19.                 }  
  20.  
  21.                 if (maxpress < pres.getSyspress()) {  
  22.                     maxpress = pres.getSyspress();  
  23.                 }  
  24.             }  
  25.  
  26.             maxpress += addmax;  
  27.  
  28.  
  29.             log.info("high press value is =" + maxpress);  
  30.  
  31.         }  
  32.  
  33.         JFreeChart jfreechart = ChartFactory.createScatterPlot(bloodcattile,  
  34.                 shou, shu, xydataset, PlotOrientation.VERTICAL, truefalse,  
  35.                 false);  
  36.         jfreechart.setBackgroundPaint(Color.white);  
  37.         jfreechart.setBorderPaint(Color.GREEN);  
  38.         jfreechart.setBorderStroke(new BasicStroke(1.5f));  
  39.         XYPlot xyplot = (XYPlot) jfreechart.getPlot();  
  40.         xyplot.setNoDataMessage(nobloodData);  
  41.         xyplot.setNoDataMessageFont(new Font("", Font.BOLD, 14));  
  42.         xyplot.setNoDataMessagePaint(new Color(87149117));  
  43.  
  44.         xyplot.setBackgroundPaint(new Color(255253246));  
  45.         ValueAxis vaaxis = xyplot.getDomainAxis();  
  46.         vaaxis.setAxisLineStroke(new BasicStroke(1.5f));  
  47.  
  48.         ValueAxis va = xyplot.getDomainAxis(0);  
  49.         va.setAxisLineStroke(new BasicStroke(1.5f));  
  50.  
  51.         va.setAxisLineStroke(new BasicStroke(1.5f)); // 坐标轴粗细  
  52.         va.setAxisLinePaint(new Color(215215215)); // 坐标轴颜色  
  53.         xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 边框粗细  
  54.         va.setLabelPaint(new Color(101010)); // 坐标轴标题颜色  
  55.         va.setTickLabelPaint(new Color(102102102)); // 坐标轴标尺值颜色  
  56.         ValueAxis axis = xyplot.getRangeAxis();  
  57.         axis.setAxisLineStroke(new BasicStroke(1.5f));  
  58.  
  59.         XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot  
  60.                 .getRenderer();  
  61.         xylineandshaperenderer.setSeriesOutlinePaint(0, Color.WHITE);  
  62.         xylineandshaperenderer.setUseOutlinePaint(true);  
  63.         NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis();  
  64.         numberaxis.setAutoRangeIncludesZero(false);  
  65.         numberaxis.setTickMarkInsideLength(2.0F);  
  66.         numberaxis.setTickMarkOutsideLength(0.0F);  
  67.         numberaxis.setAxisLineStroke(new BasicStroke(1.5f));  
  68.         numberaxis.setUpperBound(maxpress);  
  69.         numberaxis.setLowerBound(60);//最小值设置为60  
  70.         NumberAxis numberaxis1 = (NumberAxis) xyplot.getRangeAxis();  
  71.         numberaxis1.setTickMarkInsideLength(2.0F);  
  72.         numberaxis1.setTickMarkOutsideLength(0.0F);  
  73.         numberaxis1.setUpperBound(105d);  
  74.         numberaxis1.setLowerBound(35);  
  75.         numberaxis1.setAxisLineStroke(new BasicStroke(1.5f));  
  76.  
  77.         // if (xydataset != null) {  
  78.         XYBoxAnnotation box = new XYBoxAnnotation(008959); //正常血压所在区域内边界  
  79.         XYBoxAnnotation box1 = new XYBoxAnnotation(0011979);//高血压前期所在区域内边界  
  80.         XYBoxAnnotation box2 = new XYBoxAnnotation(0013989);//高血压一期所在区域内边界  
  81.         XYBoxAnnotation box3 = new XYBoxAnnotation(0015999);//高血压二期所在区域内边界  
  82.         XYTextAnnotation text1 = new XYTextAnnotation(nomal, 7062.5);//标识“正常”  
  83.         XYTextAnnotation text = new XYTextAnnotation(fore, 7082.5);//“高血压前期”  
  84.         XYTextAnnotation text2 = new XYTextAnnotation(one, 7091.5);//“高血压一期”  
  85.         XYTextAnnotation text3 = new XYTextAnnotation(two, 70101.5);//“高血压二期”  
  86.  
  87.           
  88.         //将上面的边界线条,说明文字加入到xyplot中。  
  89.         xyplot.addAnnotation(box);  
  90.         xyplot.addAnnotation(box1);  
  91.         xyplot.addAnnotation(box2);  
  92.         xyplot.addAnnotation(box3);  
  93.  
  94.         xyplot.addAnnotation(text);  
  95.         xyplot.addAnnotation(text1);  
  96.         xyplot.addAnnotation(text2);  
  97.         xyplot.addAnnotation(text3);  
  98.         // }  
  99.         return jfreechart;  
  100.     } 

***一步,返回图片URL

  1. public static void drawScatterChart(IrisIoInterface io, Log log,  
  2.             XYDataset xydataSet, String title, String shou, String shu,  
  3.             String nodata, String boolds, String nomal, String fore,  
  4.             String one, String two, List<PressureBean> list) {  
  5.  
  6.         JFreeChart chart = createChart(xydataSet, title, shou, shu, nodata,  
  7.                 boolds, nomal, fore, one, two, list, log);  
  8.  
  9.         HttpServletRequest request = io.getRequest();  
  10.         String filename = "";  
  11.         String graphURL = "";  
  12.         try {  
  13.             filename = ServletUtilities.saveChartAsPNG(chart, 400300null,  
  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.  
  23.         io.setData("filename", filename, BeanShare.BEAN_SHARE_REQUEST);  
  24.         io.setData("scatterurl", graphURL, BeanShare.BEAN_SHARE_REQUEST);  
  25.  
  26.     } 

效果图:

原文链接:http://juliana-only.iteye.com/blog/544104

【编辑推荐】

  1. JFreeChart***实践:折线图
  2. JFreeChart***实践:柱状图
  3. JFreeChart***实践:3D饼图
  4. JFreeChart***实践:时序图
  5. JFreeChart***实践:甘特图
责任编辑:林师授 来源: 远去的渡口博客
相关推荐

2011-12-21 13:52:27

JavaJFreeChart

2011-12-21 13:44:33

JavaJFreeChart

2011-12-21 13:25:33

JavaJFreeChart

2011-12-21 14:15:08

JavaJFreeChart

2011-12-21 12:58:41

JavaJFreeChart

2011-12-21 14:34:33

JavaJFreeChart

2011-12-21 12:46:43

2011-12-20 12:53:43

JavaJFreeChart

2011-08-18 11:05:21

jQuery

2023-07-21 01:12:30

Reactfalse​变量

2012-08-09 09:10:56

代码审查代码

2014-06-09 15:50:08

2014-08-19 10:06:53

IAP

2023-09-11 08:50:03

Maven工具关系管理

2015-09-23 09:08:38

java反射

2023-09-13 08:00:00

JavaScript循环语句

2010-02-04 11:55:27

ibmdwDB2

2009-12-31 10:16:49

2013-06-13 09:21:31

RESTful APIRESTfulAPI

2015-09-15 16:01:40

混合IT私有云IT架构
点赞
收藏

51CTO技术栈公众号