how to plot day-wise chart using Jfreechart

本小妞迷上赌 提交于 2019-12-03 22:22:29
futureelite7

How to create a sample XYPlot with 15 minute intervals (shown as date)

1) Create your data.

   XYSeries dataSeries = new XYSeries("SMS Sent");

2) Add your axes. If you want the x-axis to show dates, use a DateAxis as the x-axis. Input your date data as a long (in milliseconds). jfreecharts will take care of the formatting for you.

    DateAxis dateAxis = new DateAxis(timeAxisTitle);

    DateTickUnit unit = null;
    unit = new DateTickUnit(DateTickUnit.MINUTE,15);

    DateFormat chartFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    dateAxis.setDateFormatOverride(chartFormatter);

    dateAxis.setTickUnit(unit);

    NumberAxis valueAxis = new NumberAxis(valueAxisTitle);

3) Use a DateTickUnit object to set the tick size (e.g. 15 mins.) This will plot a point every 15 mins.

4) Use a Tooltip generator to generate tooltips (optional)

    XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);

    StandardXYToolTipGenerator ttg = new StandardXYToolTipGenerator(
            "{0}: {2}", chartFormatter, NumberFormat.getInstance());


    StandardXYItemRenderer renderer = new StandardXYItemRenderer(
            StandardXYItemRenderer.SHAPES_AND_LINES, ttg, null);

    renderer.setShapesFilled(true);

    XYPlot plot = new XYPlot(xyDataset, dateAxis, valueAxis, renderer);

    JFreeChart chart = new JFreeChart(chartTitle, JFreeChart.DEFAULT_TITLE_FONT, plot, false);
    chart.setBackgroundPaint(java.awt.Color.WHITE);

5) create the chart by instantiating a new JFreeChart object. You can then save it or display it on screen. Refer to Java documentation on how to do this.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!