CategoryMarker help expected

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Aniket_kedari
Posts: 17
Joined: Tue Jun 24, 2008 3:03 am

CategoryMarker help expected

Post by Aniket_kedari » Thu Aug 14, 2008 8:23 am

I am trying do draw a horizontal line on my Gantt chart.
After searching a lot on Jfree.org and net I came to know this can be done using addRangeMarker() . This method takes CategoryMArker as a parameter, but I am too much confused about what to pass as parameter.
Below is my code

Code: Select all




package demo1;

import java.util.Calendar;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryMarker;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A simple demonstration application showing how to create a Gantt
 *chart.
 *
 */
public class GanttDemo extends ApplicationFrame {

/**
 * Creates a new demo.
 *
 * @param title  the frame title.
 */
public GanttDemo(final String title) {

    super(title);

    final IntervalCategoryDataset dataset = createDataset();
    final JFreeChart chart = createChart(dataset);

    final CategoryPlot plot = (CategoryPlot) chart.getPlot(); 
    
    /*Dont know what arguments I should send to create object of
     *CategoryMarker 
     */ 
     
    // CategoryMarker cm = new CategoryMarker(java.lang.Comparable key) 

 
    // plot.addRangeMarker(cm);
    
    
    // add the chart to a panel...
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(800, 470));
    setContentPane(chartPanel);

}


/**
 * Creates a sample dataset for a Gantt chart.
 *
 * @return The dataset.
 */
public static IntervalCategoryDataset createDataset() {
	
    final TaskSeries s1 = new TaskSeries("Scheduled");
    final TaskSeries s2 = new TaskSeries("Actual");
    
    Task A = new Task("8/11 A",
            new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 9,0,0),date(2008, Calendar.AUGUST, 11, 18,0,0)));
    
    Task A1 = new Task("A-1",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 10,0,0),date(2008, Calendar.AUGUST, 11, 11,0,0)));
    
    Task A2 = new Task("A-2",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 12,0,0),date(2008, Calendar.AUGUST, 11, 13,0,0)));
	    
    A.addSubtask(A1);
    A.addSubtask(A2);
   
    s1.add(A);
	    
	    
    Task B = new Task("8/11 B",
        new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 9,0,0),date(2008, Calendar.AUGUST, 11, 18,00,0)));
    
    Task B1 = new Task("B-1",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 11,0,0),date(2008, Calendar.AUGUST, 11, 12,0,0)));
    
    Task B2 = new Task("B-2",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 14,30,0),date(2008, Calendar.AUGUST, 11, 15,0,0)));
    
    B.addSubtask(B1);
    B.addSubtask(B2);	    

    s1.add(B);
    
    /* Adding new Task B which has got same task titles as that in Task A */
    
    Task Aact = new Task("8/11 A",
            new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 9,0,0),date(2008, Calendar.AUGUST, 11, 18,00,0)));
    
    Task Aact1 = new Task("A-1",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 10,0,0),date(2008, Calendar.AUGUST, 11, 11,30,0)));
    
    Task Aact2 = new Task("A-2",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 12,0,0),date(2008, Calendar.AUGUST, 11, 13,0,0)));

    Aact.addSubtask(Aact1);
    Aact.addSubtask(Aact2);	    

    s2.add(Aact);


    Task Bact = new Task("8/11 B",
            new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 9,0,0),date(2008, Calendar.AUGUST, 11, 18,00,0)));
    
    Task Bact1 = new Task("B-1",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 11,0,0),date(2008, Calendar.AUGUST, 11, 12,30,0)));
    
    Task Bact2 = new Task("B-2",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 14,30,0),date(2008, Calendar.AUGUST, 11, 15,0,0)));
    
    Bact.addSubtask(Bact1);
    Bact.addSubtask(Bact2);	    

    s2.add(Bact);


    Task C = new Task("8/11 C",
            new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 9,0,0),date(2008, Calendar.AUGUST, 11, 18,00,0)));
    
    Task C1 = new Task("c-1",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 11,0,0),date(2008, Calendar.AUGUST, 11, 12,0,0)));
    
    Task C2 = new Task("C-2",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 13,00,0),date(2008, Calendar.AUGUST, 11, 14,0,0)));
	    
	C.addSubtask(C1);
	C.addSubtask(C2);	    

	s1.add(C);
	    
   
	Task Cact = new Task("8/11 C",
            new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 9,0,0),date(2008, Calendar.AUGUST, 11, 18,00,0)));
    
    Task Cact1 = new Task("C-1",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 11,0,0),date(2008, Calendar.AUGUST, 11, 12,0,0)));    
    
    Task Cact2 = new Task("C-2",new SimpleTimePeriod(date(2008, Calendar.AUGUST, 11, 13,0,0),date(2008, Calendar.AUGUST, 11, 14,0,0)));
    
    Cact.addSubtask(Cact1);
    Cact.addSubtask(Cact2);	    

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

    return collection;
}

/**
 * Utility method for creating <code>Date</code> objects.
 *
 * @param day  the date.
 * @param month  the month.
 * @param year  the year.
 *
 * @return a date.
 */
private static Date date(final int year, final int month, final int day, final int hourOfDay, final int minute, final int second) {

    final Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day, hourOfDay, minute, second);
    final Date result = calendar.getTime();
    return result;

}
    
/**
 * Creates a chart.
 * 
 * @param dataset  the dataset.
 * 
 * @return The chart.
 */
private JFreeChart createChart(final IntervalCategoryDataset dataset) {
    final JFreeChart chart = ChartFactory.createGanttChart(
        "Gantt Chart Demo",  // chart title
        "Task",              // domain axis label
        "Date",              // range axis label
        dataset,             // data
        true,                // include legend
        true,                // tooltips
        false                // urls
    );    
    chart.getCategoryPlot().getDomainAxis().setMaximumCategoryLabelWidthRatio(10.0f);
    return chart;    
}

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

    final GanttDemo demo = new GanttDemo("Gantt Chart Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}


This code works fine and displays graph. I want to draw horizontal lines after every two tasks.
Please help me to know how this can be achieved.

RoyW
Posts: 93
Joined: Wed Apr 23, 2008 7:42 pm
Contact:

Re: CategoryMarker help expected

Post by RoyW » Fri Aug 15, 2008 2:25 pm

Aniket_kedari wrote: CategoryMarker help expected
I am hoping that English is your second language as "help expected" seems a little too demanding. I will give you the benefit of the doubt and assume you meant "help needed".

Try this.

Code: Select all

    Stroke stroke = new BasicStroke();
    plot.addDomainMarker(new CategoryMarker("8/11 A", Color.BLACK, stroke, Color.BLACK, stroke, 0.25f));
    plot.addDomainMarker(new CategoryMarker("8/11 B", Color.BLACK, stroke, Color.BLACK, stroke, 0.25f));
    plot.addDomainMarker(new CategoryMarker("8/11 C", Color.BLACK, stroke, Color.BLACK, stroke, 0.25f));
The answer does not come from thinking outside the box, rather the answer comes from realizing the truth; There is no Box. my js site

Aniket_kedari
Posts: 17
Joined: Tue Jun 24, 2008 3:03 am

Thanks a lot

Post by Aniket_kedari » Mon Aug 18, 2008 2:09 am

Hello RoyW,
thanks for the answer.
I am very sorry for using such demanding language.
I will definately take care while posting hereafter.
Once again thanks for your help and kindness.

Locked