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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值