TaskSeriesCollection in one row

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
Posts: 3
Joined: Fri Dec 01, 2017 9:50 am
antibot: No, of course not.

TaskSeriesCollection in one row

Post by Raider » Fri Dec 01, 2017 10:14 am


i want to create a Plot with two subplots, which schow values over a given time and on the second subplot serveral task should be visible.
Therefore I use the Demo XYTaskDatasetDemo2 which works really fine, but now I want to show all tasks in one row, because there can't be two tasks at the same time.

Here I found a similar topic: viewtopic.php?f=3&t=117356
But I just get it done to show the task on my timeAxis on the current date. All tasks are located in 1970 (think it's because milliseconds since epoche) but i can't set the offset to de actual date.

I hope you can give me some help or other usefull recommendations.

Here is a screenshot of my result:

And here is my code:

Code: Select all

import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.chart.util.DefaultShadowGenerator;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.gantt.XYTaskDataset;
import org.jfree.data.general.SeriesException;
import org.jfree.data.time.*;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYIntervalSeries;
import org.jfree.data.xy.XYIntervalSeriesCollection;
import org.jfree.ui.Layer;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.io.File;
import java.util.Date;
import java.util.Random;

public class TimeSeriesAndBlockExample extends JPanel {

    public TimeSeriesAndBlockExample() {
        setLayout( new GridBagLayout(  ) );
        setBackground( Color.DARK_GRAY );

        ChartFactory.setChartTheme( StandardChartTheme.createLegacyTheme( ) );
        GridBagConstraints c = new GridBagConstraints( );
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 100.0;
        c.weighty = 100.0;
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets( 50, 50, 50, 50 );
        add(new ChartPanel(createChart()),c);
    private static XYDataset createDataset() {
        TimeSeriesCollection dataset = new TimeSeriesCollection();

        TimeSeries s1 = new TimeSeries("Series1");

        for(int i=1; i<60; i++) {
            Random rand = new Random();

            int  n = rand.nextInt(18000) + 1;
            s1.add(new Minute(i, new Hour(0, new Day())), n);
        return dataset;
    private static XYPlot createSubplot1(XYDataset dataset) {
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesShapesVisible(0, false );
        renderer.setBaseShape(new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0));
        renderer.setBaseStroke(new BasicStroke(4));
        NumberAxis yAxis = new NumberAxis("");
        XYPlot plot = new XYPlot(dataset, new DateAxis("Time"), yAxis,
        return plot;
    private static XYPlot createSubplot2(IntervalXYDataset dataset) {
        DateAxis xAxis = new DateAxis("Date/Time");
        SymbolAxis yAxis = new SymbolAxis("", new String[] {"Task1",
        XYBarRenderer renderer = new XYBarRenderer();
        renderer.setShadowVisible( false );
        XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
        return plot;

    private static JFreeChart createChart() {
        CombinedDomainXYPlot plot = new CombinedDomainXYPlot(
                new DateAxis(""));
        plot.add(createSubplot1(createDataset()),1 );
        plot.add(createSubplot2(createDataset2()), 1);

        JFreeChart chart = new JFreeChart("Combined Plot", plot);
        chart.setRenderingHints( new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ) );
        return chart;

    private static XYIntervalSeriesCollection createDataset2() {
        XYIntervalSeriesCollection dataset = new XYIntervalSeriesCollection();

        int taskTypes = 4;
        int rowCount = 1;
        String[] courseNames= new String[]{"Task1","Task2","Task3","Task4"};
        int totalCourseCount = 10;

        XYIntervalSeries[] series = new XYIntervalSeries[taskTypes];
        for(int i = 0; i < taskTypes; i++){
            series[i] = new XYIntervalSeries(courseNames[i]);
        // DOES NOT WORK
        double time = System.currentTimeMillis();
        series[0].add(0, -0.3, 0.3, time, time - 150000, time + 150000);

        return dataset;

Posts: 1624
Joined: Sat Feb 17, 2007 1:51 pm

Re: TaskSeriesCollection in one row

Post by paradoxoff » Fri Dec 01, 2017 5:05 pm

Raider wrote:

Code: Select all

        // DOES NOT WORK
        double time = System.currentTimeMillis();
        series[0].add(0, -0.3, 0.3, time, time - 150000, time + 150000);
        return dataset;
Here you are saying that the range of x values goes from -0.3 to 3, but your x Axis is a DateAxis. You need to revert the x and y coordinates:
series[0].add(time, time - 150000, time + 150000, 0, -0.3, 0.3);

Posts: 3
Joined: Fri Dec 01, 2017 9:50 am
antibot: No, of course not.

Re: TaskSeriesCollection in one row

Post by Raider » Tue Dec 05, 2017 8:21 am

Thank you, worked for me :)

Post Reply