今天公司里分配给我的工作是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;
- int diapress;
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);
-
-
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
- if (weekOrmonth == 0) {
- dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1, format));
- } else if (weekOrmonth == 1) {
-
-
- format = new SimpleDateFormat("yyyy" + year + index + "w" + week);
- dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7, format));
- } else if (weekOrmonth == 2) {
- format = new SimpleDateFormat("yy-MM");
- dateaxis
- .setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1, format));
-
- }
- dateaxis.setVerticalTickLabels(false);
- 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));
-
-
-
- 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);
- }
-
- 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);
- }
-
- 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) {
-
- 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框架不一样所以感慨道离开老师离开学校真的不习惯。
??