Part III: JFreeChart “Gantt” schedule

I was contemplating whether to manually draw the schedule, to use a JTable, or use JPanels in a GridbagLayout. I did think of JFreeChart, but I’m not sure if there’s a chart suitable, and I was hoping its Gantt chart was up to the job. It was.

The below sample shows it can render multiple “tasks” in the same row, so it satisfies my needs.

TaskSeriesCollection dataset = new TaskSeriesCollection();

TaskSeries unavailable = new TaskSeries("Unavailable");
Task room1 = new Task("Meeting Room 1", 
	new GregorianCalendar(2009, Month.DECEMBER, 1, 7, 00).getTime(), 
	new GregorianCalendar(2009, Month.DECEMBER, 1, 18, 00).getTime());
unavailable.add(room1);

room1.addSubtask(new Task("Meeting 1",
	new GregorianCalendar(2009, Month.DECEMBER, 1, 9, 00).getTime(), 
	new GregorianCalendar(2009, Month.DECEMBER, 1, 16, 00).getTime()));

Task room2 = new Task("Meeting Room 2",
	new GregorianCalendar(2009, Month.DECEMBER, 1, 7, 00).getTime(), 
	new GregorianCalendar(2009, Month.DECEMBER, 1, 18, 00).getTime());
unavailable.add(room2);

room2.addSubtask(new Task("Meeting 2",
	new GregorianCalendar(2009, Month.DECEMBER, 1, 10, 00).getTime(), 
	new GregorianCalendar(2009, Month.DECEMBER, 1, 11, 00).getTime()));

room2.addSubtask(new Task("Meeting 3",
	new GregorianCalendar(2009, Month.DECEMBER, 1, 14, 00).getTime(), 
	new GregorianCalendar(2009, Month.DECEMBER, 1, 15, 00).getTime()));
room2.addSubtask(new Task("Meeting 4",
	new GregorianCalendar(2009, Month.DECEMBER, 1, 16, 00).getTime(), 
	new GregorianCalendar(2009, Month.DECEMBER, 1, 18, 00).getTime()));

dataset.add(unavailable);

JFrame frame = new JFrame("MeetNow!");	
// title, domain axis, range axis, dataset, legend, tooltip, urls
JFreeChart chart = ChartFactory.createGanttChart("", "Room", "Time", dataset, false, true, false);
ChartPanel chartPanel = new ChartPanel(chart);
frame.getContentPane().add(chartPanel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBounds(50, 50, 800, 200);
frame.setVisible(true);

The result of the above code is like this.
gantt

Using a set of models I hooked up this UI to the query code. For some chart customization I added the meeting subject onto the bar and tooltip, changed the color and added some controls on the top.
gantt2

See Part II for querying Exchange for the schedule, and Part I for setting up the Web Service.

4 thoughts to “Part III: JFreeChart “Gantt” schedule”

  1. Hi,
    How the same can be done on a jsp with the values from a database
    i will send my part of pgm please check that and suggest what all changes has to made
    in the action class
    if(action.trim().equalsIgnoreCase(“chart”))
    {
    DsChart dsChart=new DsChart();
    dsChart=bpp.getDataFromTable(fetchCompany, fetchStore,Integer.parseInt(month),Integer.parseInt(year));
    dsChart.setTitle(ap.getStoreName(storead));
    dsChart.setSubTitle(reportType);
    request.setAttribute(“chart”,dsChart);
    return mapping.findForward(“chart”);
    }

    in the persistence

    /for graph month range starts
    public DsChart getDataFromTable(String company,String store,int month,int year,int endmonth,int endyear){

    Connection con=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    DsChart dsChart=new DsChart();
    List li=new ArrayList();
    SimpleDateFormat format=new SimpleDateFormat(“yyyy-MM-dd”);
    SimpleDateFormat twoDigitYear=new SimpleDateFormat(“yy-MMM”);
    SimpleDateFormat monthOnly=new SimpleDateFormat(“MMM”);
    ArrayList ret=new ArrayList();
    int start=1;
    try
    {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.set(year, month, 01);
    cal2.set(endyear, endmonth,01);

    con=DBConnection.getConnection();
    String select=”SELECT l.receiver_date as rdate,sum(l.raw_PO_Cost) as amt,sum(l.Qty_receieved) as itmsol FROM line_item_rece_voucher l,itemmasterstore i where l.Company=i.Company and l.Store=i.Store and l.Item=i.PluId and l.Company=? and l.Store=? and l.receiver_date between ? and ? and i.DepartmentId in(SELECT Dept_Id FROM dep d where d.Category_Id=400 and d.Company=l.company and d.Store=l.store) group by month(l.receiver_date)”;
    pst=con.prepareStatement(select);
    pst.setString(1, company);
    pst.setString(2, store);
    for(Calendar c = cal1;c.compareTo(cal2)<= 0;c.add(Calendar.MONTH,1))
    {
    c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
    pst.setString(3, format.format(c.getTime()));
    c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
    pst.setString(4, format.format(c.getTime()));
    rs=pst.executeQuery();
    if(rs.next()){

    ret.add(new DsChartEntity(start, "Volume", Double.parseDouble(rs.getString(3)),"Volume[ "+rs.getString(1)+" , "+Double.parseDouble(rs.getString(3))+" ]"));
    ret.add(new DsChartEntity(start, "Amount", Double.parseDouble(rs.getString(2)),"Volume[ "+rs.getString(1)+" , "+Double.parseDouble(rs.getString(2))+" ]"));
    start++;
    }
    else{
    ret.add(new DsChartEntity(start, "Volume", 0.00,"Volume[ "+format.format(c.getTime())+" , "+0.00+" ]"));
    ret.add(new DsChartEntity(start, "Amount", 0.00,"Volume[ "+format.format(c.getTime())+" , "+0.00+" ]"));
    start++;
    }

    li.add(((year!=endyear)?twoDigitYear.format(c.getTime()):monthOnly.format(c.getTime())));
    }

    List displayInfo=new ArrayList();
    dsChart.setXAxisStartValue(1);
    dsChart.setXAxisEndValue(start-1);
    dsChart.setData(ret);
    dsChart.setShowCustomToolTip(true);
    dsChart.setTitle(“DataSoftNet”);
    dsChart.setSubTitle(“Beer Purchase Chart”);
    dsChart.setWidth(1094);//219 + 875
    dsChart.setHeight(719);//94 + 625
    dsChart.setXAxisTitle(“”);
    dsChart.setYAxistitle(“”);
    dsChart.setAction(“#”);
    dsChart.setXLabelList(li);
    dsChart.setDisplayInfo(displayInfo);
    }
    catch(Exception e)
    {
    System.out.println(“An Exception occured at BeerPurchasePersistence.getDataFromTableMonthrange”+e);
    }
    finally{
    try {

    rs.close();
    pst.close();
    con.close();
    } catch (SQLException e) {
    System.out.println(“An Exception occured in connection”);
    }
    }

    return dsChart;
    }
    //for graph month range ends

  2. Hello ,

    very good tutorial ! But for me it is a little bit unclear how you added the text to the bars in the second example (the blue bars) ?
    Can you give a short explanation/sample on howto add labels/text to those bars and how to change the colors ?

    Sorry I am not that familiar with JFreecharts.

    Cu

    Schoby

Leave a Reply