DateAxis not synchronized with NumberAxis

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
amsel23
Posts: 1
Joined: Thu May 12, 2016 7:10 pm
antibot: No, of course not.

DateAxis not synchronized with NumberAxis

Post by amsel23 » Thu May 12, 2016 7:28 pm

Hello everbody,

please have a look at the following picture below as I try to describe my problem.
Image

My dataset consits of 3 dimensions: [measure value, measure count, timestamp]
I want to create a graph over all measure values where my main x axis shows the measure count. A seconds x axis should give a rough overview over the dates when the measure were taken.

The measure count is a simple sequence counter where the next value is always incremented by one. But, the sequence of timestamps is not uniformly incremented. It is possible that each day a measure was taken but for some time only each month (as shown in the example by the green date ticks).
If I add my second x axis in JFreechart to my plot the sequence of the dates are not uniformly "scaled" along the measure counter. ( in the example pciture the black date ticks)

Again, black date ticks is what I get with the current code and the green date ticks (which I added manually) is what it actually should look like.

Can anybody help me out on this issues? I much appreciate.
Cheers


Here is my code example to generate the example plot.

Code: Select all

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * An example of two time series for different time periods, plotted against each other using two
 * x-axes and two y-axes.
 */
public class JFreeChartEx extends ApplicationFrame {

  /**
   * A demonstration application showing how to create a time series chart with multiple axes.
   *
   * @param title the frame title.
   */
  public JFreeChartEx(String title) {
    super(title);
    JPanel chartPanel = createDemoPanel();
    chartPanel.setPreferredSize(new java.awt.Dimension(600, 270));
    setContentPane(chartPanel);

  }

  private static JFreeChart createChart() {

    int[] samples = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int[] measures = new int[] {33, 55, 56, 59, 54, 53, 49, 42, 33, 35};
    long[] timepoints = createTimepoints();


    XYSeriesCollection dataset1 = new XYSeriesCollection();
    XYSeriesCollection dataset2 = new XYSeriesCollection();
    XYSeries series1 = new XYSeries("measures");
    XYSeries series2 = new XYSeries("dates");
    dataset1.addSeries(series1);
    dataset2.addSeries(series2);

    for (int i = 0; i < samples.length; i++) {
      series1.add(samples[i], measures[i]);
      series2.add(timepoints[i], measures[i]);
    }

    JFreeChart chart =
        ChartFactory.createXYLineChart("title", "samples", "measure value", dataset1);

    chart.setBackgroundPaint(Color.white);
    XYPlot plot = (XYPlot) chart.getPlot();

    DateAxis xAxis1 = new DateAxis();
    xAxis1.setDateFormatOverride(new SimpleDateFormat("d-MMM"));
    plot.setDataset(1, dataset2);
    plot.mapDatasetToDomainAxis(1, 1);
    plot.mapDatasetToRangeAxis(1, 1);
    plot.setDomainAxis(1, xAxis1);
    plot.setDomainAxisLocation(1, AxisLocation.TOP_OR_LEFT);

    return chart;

  }

  private static long[] createTimepoints() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, 2016);
    cal.set(Calendar.MONTH, Calendar.APRIL);
    cal.set(Calendar.DAY_OF_MONTH, 1);

    // first 5 instances are same month
    long[] timepoints = new long[10];
    for (int i = 0; i < 5; i++) {
      cal.set(Calendar.DAY_OF_MONTH, i+1);
      timepoints[i] = cal.getTimeInMillis();
      System.out.println(cal.getTime());
    }
    // last 5 instances are different months
    cal.set(Calendar.DAY_OF_MONTH, 1);
    for (int i = 5; i < 10; i++) {
      cal.set(Calendar.MONTH, (i+1));
      timepoints[i] = cal.getTimeInMillis();
      System.out.println(cal.getTime());
    }
    return timepoints;
  }

  /**
   * Creates a panel for the demo (used by SuperDemo.java).
   *
   * @return A panel.
   */
  public static JPanel createDemoPanel() {
    JFreeChart chart = createChart();
    return new ChartPanel(chart);
  }

  /**
   * Starting point for the demonstration application.
   *
   * @param args ignored.
   */
  public static void main(String[] args) {

    JFreeChartEx demo = new JFreeChartEx("Dont scale the date axis?!");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);

  }

}

Strongsson
Posts: 1
Joined: Mon May 09, 2016 1:05 pm
antibot: No, of course not.

Re: DateAxis not synchronized with NumberAxis

Post by Strongsson » Sat May 14, 2016 7:43 am

I experience almost the same problem. any help would be much appreciated.
Brett Eldredge announced a new tour across the US in 206. See Brett Eldredge ticket prices.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: DateAxis not synchronized with NumberAxis

Post by paradoxoff » Sun May 15, 2016 5:36 pm

You can´t use a DateAxis since the values that you wish to display are not equally space.
You could use a SymbolAxis that allows to display "arbitrary" tick labels.
Here is an example:

Code: Select all

package jfree;
import java.util.Arrays;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.Range;
import org.jfree.data.xy.DefaultXYDataset;

/**
 *
 * @author peter
 */
public class SymbolAndNumberAxisDemo {
    
    public static void main(String[] args){
        
        DefaultXYDataset set1 = new DefaultXYDataset();
        set1.addSeries("Numbers", new double[][]{{1,2,3,4,5,6,7,8,9,10},{2,4,8,7,1,3,3,8,5,6}});
        NumberAxis xAxis = new NumberAxis("x");
        XYPlot plot = new XYPlot(set1, xAxis, new NumberAxis("y"), new XYLineAndShapeRenderer());
        JFreeChart chart = new JFreeChart(plot);
        SymbolAxis sym = new SymbolAxis("Dates",new String[]{
            "0","1 Monday","2 Tuesday","3 Wednesday","4 Thursday","5 Friday",
            "6 February","7 March","8 April","9 May","10 June"
        });
        xAxis.setRange(new Range(0.5,10.5));
        plot.setDomainAxis(1, sym);
        sym.setRange(xAxis.getRange());
        plot.setDomainAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
        plot.mapDatasetToDomainAxes(0, Arrays.asList(new Integer[]{0,1}));
        JFrame main = new JFrame("Symbol and Number Axis");
        main.setContentPane(new ChartPanel(chart));
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.pack();
        main.setVisible(true);
    }
}

Locked