Example usage for org.jfree.data.gantt TaskSeriesCollection add

List of usage examples for org.jfree.data.gantt TaskSeriesCollection add

Introduction

In this page you can find the example usage for org.jfree.data.gantt TaskSeriesCollection add.

Prototype

public void add(TaskSeries series) 

Source Link

Document

Adds a series to the dataset and sends a org.jfree.data.general.DatasetChangeEvent to all registered listeners.

Usage

From source file:org.jfree.chart.demo.JFreeChartDemoBase.java

/**
 * Creates a sample dataset for a Gantt chart.
 *
 * @return The dataset./*from  w  ww .j a  v a 2  s.  c  o m*/
 *
 * @deprecated Moved to the demo applications that require it.
 */
private static IntervalCategoryDataset createGanttDataset1() {

    final TaskSeries s1 = new TaskSeries("Scheduled");
    s1.add(new Task("Write Proposal",
            new SimpleTimePeriod(date(1, Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001))));
    s1.add(new Task("Obtain Approval",
            new SimpleTimePeriod(date(9, Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001))));
    s1.add(new Task("Requirements Analysis",
            new SimpleTimePeriod(date(10, Calendar.APRIL, 2001), date(5, Calendar.MAY, 2001))));
    s1.add(new Task("Design Phase",
            new SimpleTimePeriod(date(6, Calendar.MAY, 2001), date(30, Calendar.MAY, 2001))));
    s1.add(new Task("Design Signoff",
            new SimpleTimePeriod(date(2, Calendar.JUNE, 2001), date(2, Calendar.JUNE, 2001))));
    s1.add(new Task("Alpha Implementation",
            new SimpleTimePeriod(date(3, Calendar.JUNE, 2001), date(31, Calendar.JULY, 2001))));
    s1.add(new Task("Design Review",
            new SimpleTimePeriod(date(1, Calendar.AUGUST, 2001), date(8, Calendar.AUGUST, 2001))));
    s1.add(new Task("Revised Design Signoff",
            new SimpleTimePeriod(date(10, Calendar.AUGUST, 2001), date(10, Calendar.AUGUST, 2001))));
    s1.add(new Task("Beta Implementation",
            new SimpleTimePeriod(date(12, Calendar.AUGUST, 2001), date(12, Calendar.SEPTEMBER, 2001))));
    s1.add(new Task("Testing",
            new SimpleTimePeriod(date(13, Calendar.SEPTEMBER, 2001), date(31, Calendar.OCTOBER, 2001))));
    s1.add(new Task("Final Implementation",
            new SimpleTimePeriod(date(1, Calendar.NOVEMBER, 2001), date(15, Calendar.NOVEMBER, 2001))));
    s1.add(new Task("Signoff",
            new SimpleTimePeriod(date(28, Calendar.NOVEMBER, 2001), date(30, Calendar.NOVEMBER, 2001))));

    final TaskSeries s2 = new TaskSeries("Actual");
    s2.add(new Task("Write Proposal",
            new SimpleTimePeriod(date(1, Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001))));
    s2.add(new Task("Obtain Approval",
            new SimpleTimePeriod(date(9, Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001))));
    s2.add(new Task("Requirements Analysis",
            new SimpleTimePeriod(date(10, Calendar.APRIL, 2001), date(15, Calendar.MAY, 2001))));
    s2.add(new Task("Design Phase",
            new SimpleTimePeriod(date(15, Calendar.MAY, 2001), date(17, Calendar.JUNE, 2001))));
    s2.add(new Task("Design Signoff",
            new SimpleTimePeriod(date(30, Calendar.JUNE, 2001), date(30, Calendar.JUNE, 2001))));
    s2.add(new Task("Alpha Implementation",
            new SimpleTimePeriod(date(1, Calendar.JULY, 2001), date(12, Calendar.SEPTEMBER, 2001))));
    s2.add(new Task("Design Review",
            new SimpleTimePeriod(date(12, Calendar.SEPTEMBER, 2001), date(22, Calendar.SEPTEMBER, 2001))));
    s2.add(new Task("Revised Design Signoff",
            new SimpleTimePeriod(date(25, Calendar.SEPTEMBER, 2001), date(27, Calendar.SEPTEMBER, 2001))));
    s2.add(new Task("Beta Implementation",
            new SimpleTimePeriod(date(27, Calendar.SEPTEMBER, 2001), date(30, Calendar.OCTOBER, 2001))));
    s2.add(new Task("Testing",
            new SimpleTimePeriod(date(31, Calendar.OCTOBER, 2001), date(17, Calendar.NOVEMBER, 2001))));
    s2.add(new Task("Final Implementation",
            new SimpleTimePeriod(date(18, Calendar.NOVEMBER, 2001), date(5, Calendar.DECEMBER, 2001))));
    s2.add(new Task("Signoff",
            new SimpleTimePeriod(date(10, Calendar.DECEMBER, 2001), date(11, Calendar.DECEMBER, 2001))));

    final TaskSeriesCollection collection = new TaskSeriesCollection();
    collection.add(s1);
    collection.add(s2);

    return collection;
}

From source file:com.opendoorlogistics.components.gantt.GanttChartComponent.java

@Override
public void execute(final ComponentExecutionApi api, int mode, Object configuration,
        ODLDatastore<? extends ODLTable> ioDs, ODLDatastoreAlterable<? extends ODLTableAlterable> outputDs) {
    // Get items and sort by resource then date
    final StringConventions sc = api.getApi().stringConventions();
    List<GanttItem> items = GanttItem.beanMapping.getTableMapping(0).readObjectsFromTable(ioDs.getTableAt(0));

    // Rounding doubles to longs can create small errors where a start time is 1 millisecond after an end.
    // Set all start times to be <= end time
    for (GanttItem item : items) {
        if (item.getStart() == null || item.getEnd() == null) {
            throw new RuntimeException("Found Gannt item with null start or end time.");
        }//ww w . j a v a  2  s .  com

        if (item.getStart().getValue() > item.getEnd().getValue()) {
            item.setStart(item.getEnd());
        }
    }

    Collections.sort(items, new Comparator<GanttItem>() {

        @Override
        public int compare(GanttItem o1, GanttItem o2) {
            int diff = sc.compareStandardised(o1.getResourceId(), o2.getResourceId());
            if (diff == 0) {
                diff = o1.getStart().compareTo(o2.getStart());
            }
            if (diff == 0) {
                diff = o1.getEnd().compareTo(o2.getEnd());
            }
            if (diff == 0) {
                diff = sc.compareStandardised(o1.getActivityId(), o2.getActivityId());
            }
            if (diff == 0) {
                diff = Colours.compare(o1.getColor(), o2.getColor());
            }
            return diff;
        }
    });

    // Filter any zero duration items
    Iterator<GanttItem> it = items.iterator();
    while (it.hasNext()) {
        GanttItem item = it.next();
        if (item.getStart().compareTo(item.getEnd()) == 0) {
            it.remove();
        }
    }

    // Get average colour for each activity type
    Map<String, CalculateAverageColour> calcColourMap = api.getApi().stringConventions()
            .createStandardisedMap();
    for (GanttItem item : items) {
        CalculateAverageColour calc = calcColourMap.get(item.getActivityId());
        if (calc == null) {
            calc = new CalculateAverageColour();
            calcColourMap.put(item.getActivityId(), calc);
        }
        calc.add(item.getColor());
    }

    // Put into colour map
    Map<String, Color> colourMap = api.getApi().stringConventions().createStandardisedMap();
    for (Map.Entry<String, CalculateAverageColour> entry : calcColourMap.entrySet()) {
        colourMap.put(entry.getKey(), entry.getValue().getAverage());
    }

    // Split items by resource
    ArrayList<ArrayList<GanttItem>> splitByResource = new ArrayList<>();
    ArrayList<GanttItem> current = null;
    for (GanttItem item : items) {
        if (current == null
                || sc.compareStandardised(current.get(0).getResourceId(), item.getResourceId()) != 0) {
            current = new ArrayList<>();
            splitByResource.add(current);
        }
        current.add(item);
    }

    // put into jfreechart's task data structure
    TaskSeries ts = new TaskSeries("Resources");
    for (ArrayList<GanttItem> resource : splitByResource) {
        // get earliest and latest time (last time may not be in the last item)
        ODLTime earliest = resource.get(0).getStart();
        ODLTime latest = null;
        for (GanttItem item : resource) {
            if (latest == null || latest.compareTo(item.getEnd()) < 0) {
                latest = item.getEnd();
            }
        }

        Task task = new Task(resource.get(0).getResourceId(), new Date(earliest.getTotalMilliseconds()),
                new Date(latest.getTotalMilliseconds()));

        // add all items as subtasks
        for (GanttItem item : resource) {
            task.addSubtask(new MySubtask(item, new Date(item.getStart().getTotalMilliseconds()),
                    new Date(item.getEnd().getTotalMilliseconds())));
        }

        ts.add(task);
    }
    TaskSeriesCollection collection = new TaskSeriesCollection();
    collection.add(ts);

    // Create the plot
    CategoryAxis categoryAxis = new CategoryAxis(null);
    DateAxis dateAxis = new DateAxis("Time");
    CategoryItemRenderer renderer = new MyRenderer(collection, colourMap);
    final CategoryPlot plot = new CategoryPlot(collection, categoryAxis, dateAxis, renderer);
    plot.setOrientation(PlotOrientation.HORIZONTAL);
    plot.getDomainAxis().setLabel(null);

    ((DateAxis) plot.getRangeAxis()).setDateFormatOverride(new DateFormat() {

        @Override
        public Date parse(String source, ParsePosition pos) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
            toAppendTo.append(new ODLTime(date.getTime()).toString());
            return toAppendTo;
        }
    });

    // Create the chart and apply the standard theme without shadows to it
    api.submitControlLauncher(new ControlLauncherCallback() {

        @Override
        public void launchControls(ComponentControlLauncherApi launcherApi) {
            JFreeChart chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
            StandardChartTheme theme = new StandardChartTheme("standard theme", false);
            theme.setBarPainter(new StandardBarPainter());
            theme.apply(chart);

            class MyPanel extends ChartPanel implements Disposable {

                public MyPanel(JFreeChart chart) {
                    super(chart);
                }

                @Override
                public void dispose() {
                    // TODO Auto-generated method stub

                }

            }
            MyPanel chartPanel = new MyPanel(chart);
            launcherApi.registerPanel("Resource Gantt", null, chartPanel, true);
        }
    });

}

From source file:entropy.plan.visualization.GanttVisualizer.java

/**
 * Build the plan agenda/*  w  w  w  .  j a  va  2s  .co  m*/
 *
 * @param plan the plan to visualize
 * @return {@code true} if the generation succeeds
 */
@Override
public boolean buildVisualization(TimedReconfigurationPlan plan) {
    File parent = new File(out).getParentFile();
    if (parent != null && !parent.exists() && !parent.mkdirs()) {
        Plan.logger.error("Unable to create '" + out + "'");
        return false;
    }
    final TaskSeriesCollection collection = new TaskSeriesCollection();
    TaskSeries ts = new TaskSeries("actions");
    for (Action action : plan) {
        Task t = new Task(action.toString(),
                new SimpleTimePeriod(action.getStartMoment(), action.getFinishMoment()));
        ts.add(t);
    }
    collection.add(ts);
    ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());

    final JFreeChart chart = ChartFactory.createGanttChart(null, // chart title
            "Actions", // domain axis label
            "Time", // range axis label
            collection, // data
            false, // include legend
            true, // tooltips
            false // urls
    );
    CategoryPlot plot = chart.getCategoryPlot();
    DateAxis da = (DateAxis) plot.getRangeAxis();
    SimpleDateFormat sdfmt = new SimpleDateFormat();
    sdfmt.applyPattern("S");
    da.setDateFormatOverride(sdfmt);
    ((GanttRenderer) plot.getRenderer()).setShadowVisible(false);
    int width = 500 + 10 * plan.getDuration();
    int height = 50 + 20 * plan.size();
    try {
        switch (fmt) {
        case png:
            ChartUtilities.saveChartAsPNG(new File(out), chart, width, height);
            break;
        case jpg:
            ChartUtilities.saveChartAsJPEG(new File(out), chart, width, height);
            break;
        }
    } catch (IOException e) {
        Plan.logger.error(e.getMessage(), e);
        return false;
    }
    return true;
}

From source file:oscar.oscarEncounter.oscarMeasurements.pageUtil.MeasurementGraphAction2.java

private static XYTaskDataset getDrugDataSet(String demographic, String[] dins) {

    TaskSeriesCollection datasetDrug = new TaskSeriesCollection();
    oscar.oscarRx.data.RxPrescriptionData prescriptData = new oscar.oscarRx.data.RxPrescriptionData();

    for (String din : dins) {
        oscar.oscarRx.data.RxPrescriptionData.Prescription[] arr = prescriptData
                .getPrescriptionScriptsByPatientRegionalIdentifier(Integer.parseInt(demographic), din);
        TaskSeries ts = new TaskSeries(arr[0].getBrandName());
        for (oscar.oscarRx.data.RxPrescriptionData.Prescription pres : arr) {
            ts.add(new Task(pres.getBrandName(), pres.getRxDate(), pres.getEndDate()));
        }/*  w w w . ja v a  2  s  .  co m*/
        datasetDrug.add(ts);
    }

    XYTaskDataset dataset = new XYTaskDataset(datasetDrug);
    dataset.setTransposed(true);
    dataset.setSeriesWidth(0.6);
    return dataset;
}

From source file:org.jfree.data.gantt.TaskSeriesCollectionTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *//*w  w  w . j  a  v a  2 s .c  o  m*/
@Test
public void testSerialization() {
    TaskSeries s1 = new TaskSeries("S");
    s1.add(new Task("T1", new Date(1), new Date(2)));
    s1.add(new Task("T2", new Date(11), new Date(22)));
    TaskSeries s2 = new TaskSeries("S");
    s2.add(new Task("T1", new Date(1), new Date(2)));
    s2.add(new Task("T2", new Date(11), new Date(22)));
    TaskSeriesCollection c1 = new TaskSeriesCollection();
    c1.add(s1);
    c1.add(s2);
    TaskSeriesCollection c2 = (TaskSeriesCollection) TestUtilities.serialised(c1);
    assertEquals(c1, c2);
}

From source file:org.jfree.data.gantt.TaskSeriesCollectionTest.java

/**
 * Some basic tests for the getSeries() methods.
 *///from  www  .j  ava  2 s.co  m
@Test
public void testGetSeries() {
    TaskSeries s1 = new TaskSeries("S1");
    TaskSeries s2 = new TaskSeries("S2");
    TaskSeriesCollection c = new TaskSeriesCollection();
    c.add(s1);

    assertEquals(c.getSeries(0), s1);
    assertEquals(c.getSeries("S1"), s1);
    assertEquals(c.getSeries("XX"), null);

    c.add(s2);
    assertEquals(c.getSeries(1), s2);
    assertEquals(c.getSeries("S2"), s2);

    boolean pass = false;
    try {
        c.getSeries(null);
    } catch (NullPointerException e) {
        pass = true;
    }
    assertTrue(pass);
}

From source file:org.jfree.data.gantt.TaskSeriesCollectionTest.java

/**
 * Confirm that cloning works.//from   ww w  .  j  av a 2s.  c o  m
 */
@Test
public void testCloning() throws CloneNotSupportedException {
    TaskSeries s1 = new TaskSeries("S1");
    s1.add(new Task("T1", new Date(1), new Date(2)));
    s1.add(new Task("T2", new Date(11), new Date(22)));
    TaskSeries s2 = new TaskSeries("S2");
    s2.add(new Task("T1", new Date(33), new Date(44)));
    s2.add(new Task("T2", new Date(55), new Date(66)));
    TaskSeriesCollection c1 = new TaskSeriesCollection();
    c1.add(s1);
    c1.add(s2);

    TaskSeriesCollection c2 = (TaskSeriesCollection) c1.clone();
    assertTrue(c1 != c2);
    assertTrue(c1.getClass() == c2.getClass());
    assertTrue(c1.equals(c2));

    // basic check for independence
    s1.add(new Task("T3", new Date(21), new Date(33)));
    assertFalse(c1.equals(c2));
    TaskSeries series = c2.getSeries("S1");
    series.add(new Task("T3", new Date(21), new Date(33)));
    assertTrue(c1.equals(c2));

}

From source file:org.jfree.data.gantt.TaskSeriesCollectionTest.java

/**
 * A test for bug report 697153.//from  ww  w .j  av  a2s.  c o m
 */
@Test
public void test697153() {

    TaskSeries s1 = new TaskSeries("S1");
    s1.add(new Task("Task 1", new SimpleTimePeriod(new Date(), new Date())));
    s1.add(new Task("Task 2", new SimpleTimePeriod(new Date(), new Date())));
    s1.add(new Task("Task 3", new SimpleTimePeriod(new Date(), new Date())));

    TaskSeries s2 = new TaskSeries("S2");
    s2.add(new Task("Task 2", new SimpleTimePeriod(new Date(), new Date())));
    s2.add(new Task("Task 3", new SimpleTimePeriod(new Date(), new Date())));
    s2.add(new Task("Task 4", new SimpleTimePeriod(new Date(), new Date())));

    TaskSeriesCollection tsc = new TaskSeriesCollection();
    tsc.add(s1);
    tsc.add(s2);

    s1.removeAll();

    int taskCount = tsc.getColumnCount();

    assertEquals(3, taskCount);

}

From source file:org.jfree.data.gantt.TaskSeriesCollectionTest.java

/**
 * Confirm that the equals method can distinguish all the required fields.
 *///  w w w.jav a  2s.com
@Test
public void testEquals() {

    TaskSeries s1 = new TaskSeries("S");
    s1.add(new Task("T1", new Date(1), new Date(2)));
    s1.add(new Task("T2", new Date(11), new Date(22)));
    TaskSeries s2 = new TaskSeries("S");
    s2.add(new Task("T1", new Date(1), new Date(2)));
    s2.add(new Task("T2", new Date(11), new Date(22)));
    TaskSeriesCollection c1 = new TaskSeriesCollection();
    c1.add(s1);
    c1.add(s2);

    TaskSeries s1b = new TaskSeries("S");
    s1b.add(new Task("T1", new Date(1), new Date(2)));
    s1b.add(new Task("T2", new Date(11), new Date(22)));
    TaskSeries s2b = new TaskSeries("S");
    s2b.add(new Task("T1", new Date(1), new Date(2)));
    s2b.add(new Task("T2", new Date(11), new Date(22)));
    TaskSeriesCollection c2 = new TaskSeriesCollection();
    c2.add(s1b);
    c2.add(s2b);

    assertTrue(c1.equals(c2));
    assertTrue(c2.equals(c1));

}

From source file:org.jfree.data.gantt.TaskSeriesCollectionTest.java

/**
 * A test for bug report 800324.//from  ww  w .  j a  v  a  2  s.  c  om
 */
@Test
public void test800324() {
    TaskSeries s1 = new TaskSeries("S1");
    s1.add(new Task("Task 1", new SimpleTimePeriod(new Date(), new Date())));
    s1.add(new Task("Task 2", new SimpleTimePeriod(new Date(), new Date())));
    s1.add(new Task("Task 3", new SimpleTimePeriod(new Date(), new Date())));

    TaskSeriesCollection tsc = new TaskSeriesCollection();
    tsc.add(s1);

    // these methods should throw an IndexOutOfBoundsException since the
    // column is too high...
    try {
        /* Number start = */ tsc.getStartValue(0, 3);
        assertTrue(false);
    } catch (IndexOutOfBoundsException e) {
        // expected
    }
    try {
        /* Number end = */ tsc.getEndValue(0, 3);
        assertTrue(false);
    } catch (IndexOutOfBoundsException e) {
        // expected
    }
    try {
        /* int count = */ tsc.getSubIntervalCount(0, 3);
        assertTrue(false);
    } catch (IndexOutOfBoundsException e) {
        // expected
    }
}