JfreeChart折线图 CSDN-李鹏飞
Posted jhcelue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JfreeChart折线图 CSDN-李鹏飞相关的知识,希望对你有一定的参考价值。
今天公司里分配给我的工作是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 = 1;
- int day = 1;
- int ye = 2000;
- int week = 1;
- // 按天显示
- if (dayOrweekOrmonth == 0) {
- 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(0, 4));
- mon = Integer.parseInt(date.substring(5, 7));
- day = Integer.parseInt(date.substring(8, date.length()));
- Day days = new Day(day, mon, ye);
- double sys = pres.getSyspress();
- double dia = pres.getDiapress();
- if (sys != -1 && sys > 0) {
- timeseries.add(days, sys);
- } else {
- timeseries1.add(days, null);
- }
- if (sys != -1 && sys > 0) {
- timeseriedia.add(days, dia);
- } else {
- timeseriedia1.add(days, null);
- }
- }
- timesers.addSeries(timeseries);
- timesers.addSeries(timeseriedia);
- timesers.addSeries(timeseries1);
- timesers.addSeries(timeseriedia1);
- } else if (dayOrweekOrmonth == 1) {//按周显示
- 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 == 2) {
- ye = Integer.parseInt(spls[0]);
- mon = Integer.parseInt(spls[1]);
- } 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 != -1 && sys > 0) {
- timeseries.add(days, sys);
- } else {
- timeseries1.add(days, null);
- }
- if (sys != -1 && sys > 0) {
- 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 == 2) {
- ye = Integer.parseInt(spls[0]);
- mon = Integer.parseInt(spls[1]);
- } 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 != -1 && sys > 0) {
- timeseries.add(days, sys);
- } else {
- timeseries1.add(days, null);
- }
- if (sys != -1 && sys > 0) {
- 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 = 0;
- double addmax = 50;
- double min = 40;
- if (list != null && list.size() > 0) {
- 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 -= 10;
- log.info("high press value is =" + maxpress);
- }
- if (xydataset != null) {
- int counts = xydataset.getItemCount(0);
- if (counts == 0) {
- 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(102, 102, 102));
- jfreechart.setTitle(text);
- XYPlot xyplot = jfreechart.getXYPlot();
- xyplot.setBackgroundPaint(new Color(255, 253, 246));
- xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 边框粗细
- ValueAxis vaxis = xyplot.getDomainAxis();
- vaxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐标轴粗细
- vaxis.setAxisLinePaint(new Color(215, 215, 215)); // 坐标轴颜色
- xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 边框粗细
- vaxis.setLabelPaint(new Color(10, 10, 10)); // 坐标轴标题颜色
- vaxis.setTickLabelPaint(new Color(102, 102, 102)); // 坐标轴标尺值颜色
- vaxis.setLowerMargin(0.06d);// 分类轴下(左)边距
- vaxis.setUpperMargin(0.14d);// 分类轴下(右)边距,防止最后边的一个数据靠近了坐标轴。
- //X轴为日期格式。这里是专门的处理日期的类,
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
- if (weekOrmonth == 0) {//以天为刻度,时间格式为yyyy-MM-dd,如2008-02-06
- dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1, format));
- } else if (weekOrmonth == 1) {//以周为刻度,时间显示为 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, 7, format));
- } else if (weekOrmonth == 2) {//以月为刻度,时间显示为09-02 (09年2月)
- format = new SimpleDateFormat("yy-MM");
- dateaxis
- .setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1, format));
- }
- dateaxis.setVerticalTickLabels(false); // 设为true表示横坐标旋转到垂直。
- if (searchby == 6 || searchby == 3) {
- dateaxis.setAutoTickUnitSelection(true); // 因为横轴标签过多,这里设置为自己主动格式 。
- dateaxis.setDateFormatOverride(format);
- }
- dateaxis.setTickMarkPosition(DateTickMarkPosition.START);
- ValueAxis valueAxis = xyplot.getRangeAxis();
- valueAxis.setUpperBound(maxpress);
- valueAxis.setAutoRangeMinimumSize(1);
- valueAxis.setLowerBound(min);
- valueAxis.setAutoRange(false);
- valueAxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐标轴粗细
- valueAxis.setAxisLinePaint(new Color(215, 215, 215)); // 坐标轴颜色
- valueAxis.setLabelPaint(new Color(10, 10, 10)); // 坐标轴标题颜色
- valueAxis.setTickLabelPaint(new Color(102, 102, 102)); // 坐标轴标尺值颜色
- xyplot.setRangeGridlinesVisible(true);
- xyplot.setDomainGridlinesVisible(true);
- xyplot.setRangeGridlinePaint(Color.LIGHT_GRAY);
- xyplot.setDomainGridlinePaint(Color.LIGHT_GRAY);
- xyplot.setBackgroundPaint(new Color(255, 253, 246));
- xyplot.setNoDataMessage(nodatamess);//没有数据时显示的文字说明。
- xyplot.setNoDataMessageFont(new Font("", Font.BOLD, 14));//字体的大小,粗体。
- xyplot.setNoDataMessagePaint(new Color(87, 149, 117));//字体颜色
- xyplot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 5d)); //
- // add range marker(舒张压的区域marker,范围是从62到81)
- double lowpress = 62;
- double uperpress = 81;
- IntervalMarker intermarker = new IntervalMarker(lowpress, uperpress);
- intermarker.setPaint(Color.decode("#66FFCC"));// 域顏色
- intermarker.setLabelFont(new Font("SansSerif", 41, 14));
- intermarker.setLabelPaint(Color.RED);
- intermarker.setLabel(bp_shuzhang);
- if (xydataset != null) {
- xyplot.addRangeMarker(intermarker, Layer.BACKGROUND);
- }
- //(收缩压的区域marker,范围是从102到120)
- double lowpress1 = 102;
- double uperpress1 = 120;
- IntervalMarker inter = new IntervalMarker(lowpress1, uperpress1);
- inter.setLabelOffsetType(LengthAdjustmentType.EXPAND);
- inter.setPaint(Color.decode("#66FFCC"));// 域顏色
- inter.setLabelFont(new Font("SansSerif", 41, 14));
- 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(0, new Color(127, 128, 0));
- xylineandshaperenderer.setSeriesPaint(0, new Color(127, 128, 0));
- xylineandshaperenderer.setSeriesShapesVisible(0, true);
- xylineandshaperenderer.setSeriesShapesVisible(1, true);
- //第二条折线的颜色
- xylineandshaperenderer.setSeriesFillPaint(1, new Color(254, 103, 0));
- xylineandshaperenderer.setSeriesPaint(1, new Color(254, 103, 0));
- xylineandshaperenderer.setSeriesShapesVisible(1, true);
- xylineandshaperenderer.setSeriesVisible(2, false);//
- xylineandshaperenderer.setSeriesVisible(3, false);//不显示以下标题
- //折线的粗细调
- StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator();
- xylineandshaperenderer.setToolTipGenerator(xytool);
- xylineandshaperenderer.setStroke(new BasicStroke(1.5f));
- // 显示节点的值
- xylineandshaperenderer.setBaseItemLabelsVisible(true);
- xylineandshaperenderer
- .setBasePositiveItemLabelPosition(new ItemLabelPosition(
- ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
- xylineandshaperenderer
- .setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
- xylineandshaperenderer.setBaseItemLabelPaint(new Color(102, 102, 102));// 显示折点数值字体的颜色
- 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, 650, 280, 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框架不一样所以感慨道离开老师离开学校真的不习惯。
以上是关于JfreeChart折线图 CSDN-李鹏飞的主要内容,如果未能解决你的问题,请参考以下文章