Search code examples
jfreechart

JFree Chart Scatter plot Date against Time


I am trying to draw a graph using jfree chart. The requirement is to draw time values against date as shown in picture. I can't figure out how to use time on one axis and date on other since while building your data series the .add() method does not seems to provide parameters for date and time. I know i can pass date and tick values, but i dont want to display those tick vlaues on axis, i want something like following

NOTE : The image shows lines, but these are actually points plotted so close to each other making it look like line .. So basically i need to draw a scatter plot.

enter image description here


Solution

  • Just create a regular scatter plot where the x and y values are "milliseconds since 1 Jan 1970" (the encoding used by java.util.Date). Then replace the axes on your chart with DateAxis instances. Here is an example:

    package org.jfree.chart.demo;
    
    import java.awt.Color;
    
    import javax.swing.JPanel;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.DateAxis;
    import org.jfree.chart.axis.DateTickMarkPosition;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.data.time.Day;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.data.xy.XYSeries;
    import org.jfree.data.xy.XYSeriesCollection;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RectangleInsets;
    import org.jfree.ui.RefineryUtilities;
    
    public class StackOverflow26556268 extends ApplicationFrame {
    
        private static final long serialVersionUID = 1L;
    
        public StackOverflow26556268(String title) {
            super(title);
            ChartPanel chartPanel = (ChartPanel) createDemoPanel();
            chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
            setContentPane(chartPanel);
        }
    
        private static JFreeChart createChart(XYDataset dataset) {
            JFreeChart chart = ChartFactory.createScatterPlot(
                    "StackOverflow26556268", "Date", "Time", dataset);
            chart.removeLegend();
            chart.setBackgroundPaint(Color.white);
            XYPlot plot = (XYPlot) chart.getPlot();
            plot.setBackgroundPaint(Color.lightGray);
            plot.setDomainGridlinePaint(Color.white);
            plot.setRangeGridlinePaint(Color.white);
            plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
            plot.setDomainCrosshairVisible(true);
            plot.setRangeCrosshairVisible(true);
            DateAxis xAxis = new DateAxis("Date");
            xAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
            plot.setDomainAxis(xAxis);
            plot.setRangeAxis(new DateAxis("Time"));
            return chart;
        }
    
        private static XYDataset createDataset() {
            XYSeries s1 = new XYSeries("S1");
            s1.add(new Day(1, 10, 2014).getMiddleMillisecond(), 720000);
            s1.add(new Day(1, 10, 2014).getMiddleMillisecond(), 820000);
            s1.add(new Day(2, 10, 2014).getMiddleMillisecond(), 1020000);
            s1.add(new Day(2, 10, 2014).getMiddleMillisecond(), 920000);
            s1.add(new Day(3, 10, 2014).getMiddleMillisecond(), 1220000);
            s1.add(new Day(3, 10, 2014).getMiddleMillisecond(), 1320000);
            s1.add(new Day(4, 10, 2014).getMiddleMillisecond(), 1620000);
            s1.add(new Day(4, 10, 2014).getMiddleMillisecond(), 1520000);
            s1.add(new Day(5, 10, 2014).getMiddleMillisecond(), 1320000);
            s1.add(new Day(5, 10, 2014).getMiddleMillisecond(), 1820000);
            XYSeriesCollection dataset = new XYSeriesCollection();
            dataset.addSeries(s1);
            return dataset;
        }
    
        public static JPanel createDemoPanel() {
            JFreeChart chart = createChart(createDataset());
            ChartPanel panel = new ChartPanel(chart);
            panel.setFillZoomRectangle(true);
            panel.setMouseWheelEnabled(true);
            return panel;
        }
    
        public static void main(String[] args) {
            StackOverflow26556268 demo = new StackOverflow26556268(
                    "StackOverflow26556268/Example");
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);
        }
    
    }