JFreeChart 参数配置

http://developer.51cto.com/art/201112/308882.htm


 
 
  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.     } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值