JfreeChart折线图 CSDN-李鹏飞
2024-09-29 12:43:59
今天公司里分配给我的工作是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代码
- int userId;
- String bpDate;
- String bpTime;
- int syspress; //收缩压(mmHg)
- int diapress; //舒张压(mmHg)
2、载入数据集
Java代码
- public static TimeSeriesCollection createTimeSeries(
- List<PressureBean> list, int dayOrweekOrmonth, Log log, String shou,String shu
- ) {
- TimeSeriesCollection timesers = new TimeSeriesCollection();
- int mon = ;
- int day = ;
- int ye = ;
- int week = ;
- // 按天显示
- if (dayOrweekOrmonth == ) {
- TimeSeries timeseries = new TimeSeries(shou,
- org.jfree.data.time.Day.class);
- TimeSeries timeseries1 = new TimeSeries("c1",
- org.jfree.data.time.Day.class);
- TimeSeries timeseriedia = new TimeSeries(shu,
- org.jfree.data.time.Day.class);
- TimeSeries timeseriedia1 = new TimeSeries("d1",
- org.jfree.data.time.Day.class);
- Iterator<PressureBean> it = list.iterator();
- while (it.hasNext()) {
- PressureBean pres = it.next();
- String date = pres.getBpDate();
- ye = Integer.parseInt(date.substring(, ));
- mon = Integer.parseInt(date.substring(, ));
- day = Integer.parseInt(date.substring(, date.length()));
- Day days = new Day(day, mon, ye);
- double sys = pres.getSyspress();
- double dia = pres.getDiapress();
- if (sys != - && sys > ) {
- timeseries.add(days, sys);
- } else {
- timeseries1.add(days, null);
- }
- if (sys != - && sys > ) {
- timeseriedia.add(days, dia);
- } else {
- timeseriedia1.add(days, null);
- }
- }
- timesers.addSeries(timeseries);
- timesers.addSeries(timeseriedia);
- timesers.addSeries(timeseries1);
- timesers.addSeries(timeseriedia1);
- } else if (dayOrweekOrmonth == ) {//按周显示
- TimeSeries timeseries = new TimeSeries(shou,
- org.jfree.data.time.Week.class);
- TimeSeries timeseries1 = new TimeSeries("c1",
- org.jfree.data.time.Week.class);
- TimeSeries timeseriedia = new TimeSeries(shu,
- org.jfree.data.time.Week.class);
- TimeSeries timeseriedia1 = new TimeSeries("d1",
- org.jfree.data.time.Week.class);
- Iterator<PressureBean> it = list.iterator();
- while (it.hasNext()) {
- PressureBean pres = it.next();
- String date = pres.getBpDate();
- String[] spls = date.split("-");
- if (spls.length == ) {
- ye = Integer.parseInt(spls[]);
- mon = Integer.parseInt(spls[]);
- } else {
- log.error("the date of weeks is wrong");
- }
- Week days = new Week(mon, ye);
- double sys = pres.getSyspress();
- double dia = pres.getDiapress();
- if (sys != - && sys > ) {
- timeseries.add(days, sys);
- } else {
- timeseries1.add(days, null);
- }
- if (sys != - && sys > ) {
- timeseriedia.add(days, dia);
- } else {
- timeseriedia1.add(days, null);
- }
- }
- timesers.addSeries(timeseries);
- timesers.addSeries(timeseriedia);
- timesers.addSeries(timeseries1);
- timesers.addSeries(timeseriedia1);
- } else {//按月显示
- TimeSeries timeseries = new TimeSeries(shou,
- org.jfree.data.time.Month.class);
- TimeSeries timeseries1 = new TimeSeries("c1",
- org.jfree.data.time.Month.class);
- TimeSeries timeseriedia = new TimeSeries(shu,
- org.jfree.data.time.Month.class);
- TimeSeries timeseriedia1 = new TimeSeries("s",
- org.jfree.data.time.Month.class);
- Iterator<PressureBean> it = list.iterator();
- while (it.hasNext()) {
- PressureBean pres = it.next();
- String date = pres.getBpDate();
- String[] spls = date.split("-");
- if (spls.length == ) {
- ye = Integer.parseInt(spls[]);
- mon = Integer.parseInt(spls[]);
- } else {
- log.error("the date of weeks is wrong");
- }
- Month days = new Month(mon, ye);
- double sys = pres.getSyspress();
- double dia = pres.getDiapress();
- if (sys != - && sys > ) {
- timeseries.add(days, sys);
- } else {
- timeseries1.add(days, null);
- }
- if (sys != - && sys > ) {
- timeseriedia.add(days, dia);
- } else {
- timeseriedia1.add(days, null);
- }
- }
- timesers.addSeries(timeseries);
- timesers.addSeries(timeseriedia);
- timesers.addSeries(timeseries1);
- timesers.addSeries(timeseriedia1);
- }
- return timesers;
- }
3、画折线图,两个数据集,收缩压和舒张压。而且,这两条曲线还各自包括一个区域范围,并不单单是一条基准线,而是一个基准范围
Java代码
- private static JFreeChart createChartPress(XYDataset xydataset,
- int weekOrmonth, String title, String y, String index, String week,
- String year, int searchby, String month, String nodatamess,
- List list, Log log, String bp_shou, String bp_shuzhang) {
- // 有可能用户在后面的版本号中有益输入不正常数值,可是为了保证图片绘图的完整,这里先计算
- // 用户血压值的最大值。
- double maxpress = ;
- double addmax = ;
- double min = ;
- if (list != null && list.size() > ) {
- Iterator<PressureBean> it = list.iterator();
- while (it.hasNext()) {
- PressureBean pres = it.next();
- double sys = pres.getSyspress();
- double dia = pres.getDiapress();
- if (maxpress < sys) {
- maxpress = sys;
- }
- if (maxpress < dia)
- maxpress = dia;
- if (min > sys) {
- min = sys;
- }
- if (min > dia)
- min = dia;
- }
- maxpress += addmax;
- min -= ;
- log.info("high press value is =" + maxpress);
- }
- if (xydataset != null) {
- int counts = xydataset.getItemCount();
- if (counts == ) {
- xydataset = null;
- }
- }
- JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title, "",
- y, xydataset, true, true, false);
- jfreechart.setBackgroundPaint(Color.white);
- // 设置标题的颜色
- TextTitle text = new TextTitle(title);
- text.setPaint(new Color(, , ));
- jfreechart.setTitle(text);
- XYPlot xyplot = jfreechart.getXYPlot();
- xyplot.setBackgroundPaint(new Color(, , ));
- xyplot.setOutlineStroke(new BasicStroke(.5f)); // 边框粗细
- ValueAxis vaxis = xyplot.getDomainAxis();
- vaxis.setAxisLineStroke(new BasicStroke(.5f)); // 坐标轴粗细
- vaxis.setAxisLinePaint(new Color(, , )); // 坐标轴颜色
- xyplot.setOutlineStroke(new BasicStroke(.5f)); // 边框粗细
- vaxis.setLabelPaint(new Color(, , )); // 坐标轴标题颜色
- vaxis.setTickLabelPaint(new Color(, , )); // 坐标轴标尺值颜色
- vaxis.setLowerMargin(.06d);// 分类轴下(左)边距
- vaxis.setUpperMargin(.14d);// 分类轴下(右)边距,防止最后边的一个数据靠近了坐标轴。
- //X轴为日期格式,这里是专门的处理日期的类,
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
- if (weekOrmonth == ) {//以天为刻度。时间格式为yyyy-MM-dd,如2008-02-06
- dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, , format));
- } else if (weekOrmonth == ) {//以周为刻度。时间显示为 2009年第4周((这里是SimpleDateFormat的使用方法,
- //这里为了作繁体版,英文版和简体版,用了国际化处理,将这些可变的资源在文字资源里面,注意一下。这里的y。M、w是SimpleDateFormat的关键字。
- //如英文表示09年第3周就是09W3,那么,这里的W须要用‘’引起来)
- format = new SimpleDateFormat("yyyy" + year + index + "w" + week);
- dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, , format));
- } else if (weekOrmonth == ) {//以月为刻度,时间显示为09-02 (09年2月)
- format = new SimpleDateFormat("yy-MM");
- dateaxis
- .setTickUnit(new DateTickUnit(DateTickUnit.MONTH, , format));
- }
- dateaxis.setVerticalTickLabels(false); // 设为true表示横坐标旋转到垂直。
- if (searchby == || searchby == ) {
- dateaxis.setAutoTickUnitSelection(true); // 因为横轴标签过多,这里设置为自己主动格式 。
- dateaxis.setDateFormatOverride(format);
- }
- dateaxis.setTickMarkPosition(DateTickMarkPosition.START);
- ValueAxis valueAxis = xyplot.getRangeAxis();
- valueAxis.setUpperBound(maxpress);
- valueAxis.setAutoRangeMinimumSize();
- valueAxis.setLowerBound(min);
- valueAxis.setAutoRange(false);
- valueAxis.setAxisLineStroke(new BasicStroke(.5f)); // 坐标轴粗细
- valueAxis.setAxisLinePaint(new Color(, , )); // 坐标轴颜色
- valueAxis.setLabelPaint(new Color(, , )); // 坐标轴标题颜色
- valueAxis.setTickLabelPaint(new Color(, , )); // 坐标轴标尺值颜色
- xyplot.setRangeGridlinesVisible(true);
- xyplot.setDomainGridlinesVisible(true);
- xyplot.setRangeGridlinePaint(Color.LIGHT_GRAY);
- xyplot.setDomainGridlinePaint(Color.LIGHT_GRAY);
- xyplot.setBackgroundPaint(new Color(, , ));
- xyplot.setNoDataMessage(nodatamess);//没有数据时显示的文字说明。
- xyplot.setNoDataMessageFont(new Font("", Font.BOLD, ));//字体的大小。粗体。
- xyplot.setNoDataMessagePaint(new Color(, , ));//字体颜色
- xyplot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 5d)); //
- // add range marker(舒张压的区域marker,范围是从62到81)
- double lowpress = ;
- double uperpress = ;
- IntervalMarker intermarker = new IntervalMarker(lowpress, uperpress);
- intermarker.setPaint(Color.decode("#66FFCC"));// 域顏色
- intermarker.setLabelFont(new Font("SansSerif", , ));
- intermarker.setLabelPaint(Color.RED);
- intermarker.setLabel(bp_shuzhang);
- if (xydataset != null) {
- xyplot.addRangeMarker(intermarker, Layer.BACKGROUND);
- }
- //(收缩压的区域marker。范围是从102到120)
- double lowpress1 = ;
- double uperpress1 = ;
- IntervalMarker inter = new IntervalMarker(lowpress1, uperpress1);
- inter.setLabelOffsetType(LengthAdjustmentType.EXPAND);
- inter.setPaint(Color.decode("#66FFCC"));// 域顏色
- inter.setLabelFont(new Font("SansSerif", , ));
- inter.setLabelPaint(Color.RED);
- inter.setLabel(bp_shou);
- if (xydataset != null) {
- xyplot.addRangeMarker(inter, Layer.BACKGROUND); // 加上Layer.BACKGROUND,将maker调到折线以下。
- }
- XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
- .getRenderer();
- //第一条折线的颜色
- xylineandshaperenderer.setBaseItemLabelsVisible(true);
- xylineandshaperenderer.setSeriesFillPaint(, new Color(, , ));
- xylineandshaperenderer.setSeriesPaint(, new Color(, , ));
- xylineandshaperenderer.setSeriesShapesVisible(, true);
- xylineandshaperenderer.setSeriesShapesVisible(, true);
- //第二条折线的颜色
- xylineandshaperenderer.setSeriesFillPaint(, new Color(, , ));
- xylineandshaperenderer.setSeriesPaint(, new Color(, , ));
- xylineandshaperenderer.setSeriesShapesVisible(, true);
- xylineandshaperenderer.setSeriesVisible(, false);//
- xylineandshaperenderer.setSeriesVisible(, false);//不显示以下标题
- //折线的粗细调
- StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator();
- xylineandshaperenderer.setToolTipGenerator(xytool);
- xylineandshaperenderer.setStroke(new BasicStroke(.5f));
- // 显示节点的值
- xylineandshaperenderer.setBaseItemLabelsVisible(true);
- xylineandshaperenderer
- .setBasePositiveItemLabelPosition(new ItemLabelPosition(
- ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
- xylineandshaperenderer
- .setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
- xylineandshaperenderer.setBaseItemLabelPaint(new Color(, , ));// 显示折点数值字体的颜色
- return jfreechart;
- }
4、将图片URL返回到页面
Java代码
- public static void drawPressLineChart(IrisIoInterface io, Log log,
- TimeSeriesCollection timesers, int weekormonth, String title,
- String y, String index, String week, String year, int searchby,
- String month, String nodatamess, List list, String bp_shou,
- String bp_shuzhang) {
- JFreeChart chart = createChartPress(timesers, weekormonth, title, y,
- index, week, year, searchby, month, nodatamess, list, log,
- bp_shou, bp_shuzhang);
- HttpServletRequest request = io.getRequest();
- String filename = "";
- String graphURL = "";
- try {
- filename = ServletUtilities.saveChartAsPNG(chart, , , null,
- io.getSession());
- graphURL = request.getContextPath() + "/displayChart?filename="
- + filename;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- log.error(e);
- }
- io.setData("filename1", filename, BeanShare.BEAN_SHARE_REQUEST);
- io.setData("presslineurl", graphURL, BeanShare.BEAN_SHARE_REQUEST);
- }
如有转载请注明出处:版权全部---李鹏飞 2014/6/3日
心情日志:听着歌写着博客!今天第一天进公司開始兴趣昂昂的走进公司!今天第一天上班公司经理就分配了工作但是框架都没熟悉又和在CSDN乐知学院学到的SSH框架不一样所以感慨道离开老师离开学校真的不习惯。
最新文章
- C#+无unsafe的非托管大数组(large unmanaged array in c# without &#39;unsafe&#39; keyword)
- JavaI/O系统
- ubuntu: 环境搭建
- CheckBoxList 获取与设置选中的值
- mac使用初级
- Java程序员需要注意的五大Docker误区
- js数组,字符串常用方法汇总(面试必备)
- vue中引入swiper(vue中的滑块组件vue-awesome-swiper)
- GOF 23种设计模式
- SQL注入攻击三部曲之入门篇
- mysql 架构篇系列 4 复制架构一主一从搭建(半同步复制)
- 多选框取值checkbox
- HTTPS深入理解
- JavaScript 之 ScriptManager.RegisterStartupScript的应用
- CStatic的透明背景方法
- centos7安装MySQL5.7无法设置密码问题
- poll--wait for some event on a file descriptor
- G.Finding the Radius for an Inserted Circle 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- docker 加速
- Atitit.基于时间戳的农历日历历法日期计算
热门文章
- nginx的详解(三)
- Android点击两次返回退出程序
- PAT天梯赛练习题——L3-003. 社交集群(并查集按秩合并)
- P1736 创意吃鱼法 (动态规划)
- 【kmp或扩展kmp】HDU 6153 A Secret
- ajax同步导致ajax上面的代码不执行?
- spring中quartz的使用。【转http://www.cnblogs.com/kay/archive/2007/11/02/947372.html】
- 高通android7.0刷机工具使用介绍
- StoryBoard中使用xib
- ftrace 提供的工具函数