Time series with multiple date ranges

A discussion forum for JFreeChart (a 2D chart library for the Java platform).

Time series with multiple date ranges

Postby clf » Wed May 16, 2007 9:11 am

Newbie here. I've been checking out the demos and searching the forums. It looks like this product has a tremendous amount of capabilities. Please excuse my ignorance, but because I haven't found an example of exactly my scenario yet, I want to make sure it's possible before I dive in.

The best way to explain is by example. So, suppose I want to show a stock's performance over the month of June, overlayed with another stock's performance over the month of January. I think this means that I would want two x-axes... each one representing a different date range.
The number of date ranges could be 1..n.

To make things even more complex, I want multiple y-axes too. So I can show disparate data with wildly different ranges of values.

The closest example I've found is in the JFreeChart Demo Collection titled "MultipleAxisDemo3.java", but it doesn't have multiple date ranges.

So my first question is, is this possible?
Secondly, if so, any tips to help me get a jump-start would be much appreciated.

Posts: 3
Joined: Wed May 16, 2007 9:01 am

Postby clf » Thu May 17, 2007 4:08 pm

Could somebody please just tell me if it's possible to make a line chart with multiple date range x-axes?

If I know it can be done then I'll spend the time to figure out how to do it. I just hate to spend days on it only to find out it's not possible.
If I'm not explaining myself well then please let me know and I'll spend some time making a mock-up of it to post here.

Posts: 3
Joined: Wed May 16, 2007 9:01 am

Postby david.gilbert » Thu May 17, 2007 4:35 pm

This is close to what you want, I think. You (or someone else) will probably ask, so I'll say in advance: no, it isn't possible (currently) to align the tick marks for the opposing axes.

Code: Select all
/* ----------------------
* MultipleAxisDemo4.java
* ----------------------
* (C) Copyright 2007, by Object Refinery Limited.

package demo;

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

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.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
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 MultipleAxisDemo4 extends ApplicationFrame {

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


     * Creates the demo chart.
     * @return The chart.
    private static JFreeChart createChart() {

        XYDataset dataset1 = createDataset("March 2007", 100.0,
                new Day(1, 3, 2007), 31);
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Multiple Axis Demo 4",

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        DateAxis xAxis1 = (DateAxis) plot.getDomainAxis();
        xAxis1.setDateFormatOverride(new SimpleDateFormat("d-MMM-yyyy"));

        XYItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.red);
        NumberAxis yAxis1 = (NumberAxis) plot.getRangeAxis();
        // DOMAIN AXIS 2
        DateAxis xAxis2 = new DateAxis("Date");
        xAxis2.setDateFormatOverride(new SimpleDateFormat("d-MMM-yyyy"));
        plot.setDomainAxis(1, xAxis2);
        plot.setDomainAxisLocation(1, AxisLocation.TOP_OR_LEFT);
        // RANGE AXIS 2
        NumberAxis yAxis2 = new NumberAxis("Value");
        plot.setRangeAxis(1, yAxis2);
        plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);

        XYDataset dataset2 = createDataset("July 2007", 1000.0,
                new Day(1, 7, 2007), 31);
        plot.setDataset(1, dataset2);   
        plot.mapDatasetToDomainAxis(1, 1);
        plot.mapDatasetToRangeAxis(1, 1);   
        XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(true,
        renderer2.setSeriesPaint(0, Color.blue);
        plot.setRenderer(1, renderer2);
        return chart;
     * Creates a sample dataset.
     * @param name  the dataset name.
     * @param base  the starting value.
     * @param start  the starting period.
     * @param count  the number of values to generate.
     * @return The dataset.
    private static XYDataset createDataset(String name, double base,
                                    RegularTimePeriod start, int count) {

        TimeSeries series = new TimeSeries(name, start.getClass());
        RegularTimePeriod period = start;
        double value = base;
        for (int i = 0; i < count; i++) {
            series.add(period, value);   
            period = period.next();
            value = value * (1 + (Math.random() - 0.495) / 10.0);

        TimeSeriesCollection dataset = new TimeSeriesCollection();

        return dataset;


     * 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) {

        MultipleAxisDemo4 demo = new MultipleAxisDemo4(
                "JFreeChart: MultipleAxisDemo4.java");


David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
JFreeChart Project Leader
Posts: 11655
Joined: Fri Mar 14, 2003 10:29 am

Postby clf » Thu May 17, 2007 4:38 pm

Wow, thanks a bunch for the code. I really appreciate it.
Posts: 3
Joined: Wed May 16, 2007 9:01 am

Return to JFreeChart

Who is online

Users browsing this forum: No registered users and 8 guests