CombinedDomainXYPlot with multiple domain axis

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

CombinedDomainXYPlot with multiple domain axis

Postby Raider » Wed Dec 13, 2017 2:48 pm


i want to create a CombinedDomainXPlot with two XYPlots. The first is a TimeSeries graph, the other is other is a IntervalChart for displaying several time "events".
This does work really go but now i need to add a second domain axis for the whole plot.
The first axis (which already works) is dynamically and shows different time ranges with different time units (day -> hours->minutes->...)
For the second axis I want to add a PeriodAxis which shows only the Day intervals without changing the tick units.

I can add a second axis to my subplots but this does really work well and when zoom in/out the subplot with the new axis doesn't scale. So to set an axis to one subplot is not the right solution.

Next I tried to add the axis to the combined plot but here the axis does not appear. Maybe some of you can tell me how to get this done or give me some usefull tipps.

Here is a picture of my example chart which needs the second "year axis" to show the years.

And here is the code of my example:
Code: Select all
package Plots;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.*;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.ui.*;

public class CombinedXYPlotDemo1 extends ApplicationFrame {

    public CombinedXYPlotDemo1(String title) {
        JPanel panel = createDemoPanel();
        panel.setPreferredSize(new java.awt.Dimension(800, 400));

    private static JFreeChart createCombinedChart() {

        // create plot ...
        IntervalXYDataset data1 = createDataset1();
        XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);

        DateAxis domainAxis = new DateAxis("Time");
        ValueAxis rangeAxis = new NumberAxis("");
        XYPlot plot1 = new XYPlot(data1, null, rangeAxis, renderer1);

        // add a second dataset and renderer...
        IntervalXYDataset data2 = createDataset2();
        XYBarRenderer renderer2 = new XYBarRenderer() {};

        XYPlot plot2 = new XYPlot(data2, null, new NumberAxis(""),

        CombinedDomainXYPlot cplot = new CombinedDomainXYPlot(domainAxis);
        cplot.add(plot1, 3);
        cplot.add(plot2, 2);

        // Seconds Axis
        PeriodAxis dayAxis = new PeriodAxis("Day");
        PeriodAxisLabelInfo[] info = new PeriodAxisLabelInfo[1];
        info[0] = new PeriodAxisLabelInfo(Day.class,
                new SimpleDateFormat("d"), new RectangleInsets(2, 2, 2, 2),
                new Font("SansSerif", Font.BOLD, 10),, true,
                new BasicStroke(0.0f), Color.lightGray);

        // Add Second Axis to Plot
        cplot.setDomainAxis( 1, domainAxis );           // The second Axis does not appear
        cplot.mapDatasetToDomainAxis(0, 1);

        // return a new chart containing the overlaid plot...
        JFreeChart chart = new JFreeChart("CombinedDomainXYPlot",
                JFreeChart.DEFAULT_TITLE_FONT, cplot, false);

        LegendTitle legend = new LegendTitle(cplot);
        return chart;

    private static IntervalXYDataset createDataset1() {
        // create dataset 1...
        TimeSeries series1 = new TimeSeries("Series1");
        series1.add(new Month(8, 1990), 3211.691);
        series1.add(new Month(9, 1990), 3233.313);
        series1.add(new Month(10, 1990), 3274.950);
        series1.add(new Month(11, 1990), 3330.685);
        series1.add(new Month(12, 1990), 3364.820);
        series1.add(new Month(1, 1991), 3411.409);
        series1.add(new Month(2, 1991), 3458.637);
        series1.add(new Month(3, 1991), 3441.367);
        series1.add(new Month(4, 1991), 3445.059);
        return new TimeSeriesCollection(series1);

    private static IntervalXYDataset createDataset2() {
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        TimeSeries series1 = new TimeSeries("Series2");
        series1.add(new Month(8, 1990), 375.266);
        series1.add(new Month(9, 1990), 375.882);
        series1.add(new Month(10, 1990), 373.731);
        series1.add(new Month(11, 1990), 407.096);
        series1.add(new Month(12, 1990), 411.826);
        series1.add(new Month(1, 1991), 436.825);
        series1.add(new Month(2, 1991), 464.283);
        series1.add(new Month(3, 1991), 389.411);
        series1.add(new Month(4, 1991), 384.046);
        return dataset;


    public static JPanel createDemoPanel() {
        JFreeChart chart = createCombinedChart();
        return new ChartPanel(chart);

    public static void main(String[] args) {
        CombinedXYPlotDemo1 demo = new CombinedXYPlotDemo1(
                "JFreeChart :");

Posts: 3
Joined: Fri Dec 01, 2017 9:50 am

Return to JFreeChart

Who is online

Users browsing this forum: Bing [Bot] and 6 guests