4 Replies Latest reply on Mar 27, 2009 8:01 AM by lucius yu

    How to display a gantt chart with Seam??

    sandra sandra Newbie

      HI,


      I am working on a scheduling application, and I am using Seam for my first time, i want to display  a gantt chart but Seam supports only three chart types of the jfreechart library.... I tried to use tomahawk but coudn't integrate it successfully....



      Is there any way for doing that???


      I may use a scheduel component or a calendar widget, not necessary a gantt chart, what is important for me is to display something that could organize my events.......



      PLZ help...



      Thnk u

        • 1. Re: How to display a gantt chart with Seam??
          Javier Ferrero Newbie

          Try using jfreechart directly. For instance:


          SFSB:


          @Name("chartprocesser")
          @Stateful
          @Scope(ScopeType.EVENT)
          public class ChartProcesserBean implements ChartProcesserLocal {
              
              
              @Remove @Destroy
              public void remove(){}
              
              private byte[] chart; // chart image (.png) as a byte array
              
              
              @Factory(value="chart")
              public void createChart() {
                  
                  
                  IntervalBarChartDemo1 sample = new IntervalBarChartDemo1();
                  JFreeChart chart = sample.getOverlaidChart();
                  
                  try{
                      this.chart = ChartUtilities.encodeAsPNG(chart.createBufferedImage(800, 400));
                  } catch (IOException e){
                      e.printStackTrace();
                  }
                  
              }
          



          IntervalBarChartDemo1:


           
          public JFreeChart getChart() {
                  DefaultIntervalCategoryDataset data = null;
                  final Number[][] lows = {{10000, 18000, 26000, 34000, 42000},{1000, 1800, 2600, 3400, 4200}};
                  final Number[][] highs = {{20000, 28000, 36000, 44000, 52000},{20000, 28000, 36000, 44000, 52000}};
                  data = new DefaultIntervalCategoryDataset(SERIES, lows, highs);
                  data.setCategoryKeys(CATEGORIES);
                  //data.setSeriesKeys(SERIES);
                  final String title = "Salary Comparaison";
                  final String xTitle = "Bands";
                  final String yTitle = "Salary";
                  final CategoryAxis xAxis = new CategoryAxis(xTitle);
                  xAxis.setLabelFont(titleFont);
                  xAxis.setTickLabelFont(labelFont);
                  xAxis.setTickMarksVisible(false);
                  xAxis.setCategoryMargin(0.1);
                  
                  
                  final NumberAxis yAxis = new NumberAxis(yTitle);
                  yAxis.setLabelFont(titleFont);
                  yAxis.setTickLabelFont(labelFont);
                  yAxis.setRange(0, 60000);
                  //final DecimalFormat formatter = new DecimalFormat("0.##%");
                  yAxis.setTickUnit(new NumberTickUnit(5000));
                  
                  final IntervalBarRenderer renderer = new IntervalBarRenderer();
                  renderer.setSeriesPaint(0, new Color(51, 102, 153));
                  //renderer.setLabelGenerator(new IntervalCategoryLabelGenerator());
                  renderer.setItemLabelsVisible(true);
                  renderer.setItemMargin(0.04);
                  
                  renderer.setItemLabelPaint(Color.white);
                  final ItemLabelPosition p = new ItemLabelPosition(
                          ItemLabelAnchor.CENTER, TextAnchor.CENTER
                          );
                  renderer.setPositiveItemLabelPosition(p);
                  
                  IntervalCategoryItemLabelGenerator generator = new IntervalCategoryItemLabelGenerator("{3}{4}", new DecimalFormat());
                  renderer.setItemLabelGenerator(generator);
                  
                  
                  final CategoryPlot plot = new CategoryPlot(data, xAxis, yAxis, renderer);
                  plot.setBackgroundPaint(Color.lightGray);
                  plot.setOutlinePaint(Color.white);
                  plot.setOrientation(PlotOrientation.VERTICAL);
                  plot.setRangeGridlinePaint(Color.white);
                  
                  /*
                  CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator(
                          "{2}", new DecimalFormat("0.00"));
                  renderer.setItemLabelGenerator(generator);
                   **/
                  this.chart = new JFreeChart(title, titleFont, plot, false);
                  this.chart.setBackgroundPaint(Color.white);
                  return this.chart;
              }
          
          



          and the XHTML:


          
          <s:graphicImage value="#{chartprocesser.chart}" />
          
          



          Just change IntervalBarChartDemo1 to create your chart.


          Regards

          • 2. Re: How to display a gantt chart with Seam??
            David Beaumont Newbie

            Dear Javier,


            I am trying to draw a Gantt chart and have been following your guide and also that at:
            http://www.java2s.com/Code/Java/Chart/JFreeChartGanttDemo1.htm


            My code looks like:


            public byte[] getFundingGanttChart() {
                      if(!(null == this.fundingGanttChart)) return fundingGanttChart;
                      Map<OrganisationNameCcr, TaskSeries> funders = new HashMap<OrganisationNameCcr, TaskSeries>();
                      // Strong typing - I've heard of it
                      for(Object obj: this.getAllFundings()) {
                           Object[] objArray = (Object[]) obj;
                           OrganisationNameCcr funderName = (OrganisationNameCcr) objArray[2];
                           ProvisionFunding funding = (ProvisionFunding) objArray[0];
                           if(!(null == funders.get(funderName))) {
                                // We've already created a TaskSeries for this funder, so retrieve it.
                                funders.get(funderName).add(new Task(funding.getFunding().toString(), funding.getStart(), funding.getFinish()));
                           } else {
                                TaskSeries series = new TaskSeries(funderName.getName());
                                series.add(new Task(funding.getFunding().toString(), funding.getStart(), funding.getFinish()));
                                funders.put(funderName, series);
                           }
                      }
                      TaskSeriesCollection seriesCollection = new TaskSeriesCollection();
                      for(TaskSeries series: funders.values()) {
                           seriesCollection.add(series);
                      }
                      // FIXME - line below ought to accept the seriesCollection as argument but it fails on deployment
                      JFreeChart chart = ChartFactory.createGanttChart("Funding History", "Funding", "Date", seriesCollection, true, true, false);
                      chart.setBorderVisible(true);
                      try {
                           this.fundingGanttChart = ChartUtilities.encodeAsPNG(chart.createBufferedImage(600, 400));
                      } catch (IOException e) {
                           e.printStackTrace();
                      }
                      return this.fundingGanttChart;
                 }
            



            Eclipse seems to think this is ok, but when I deploy the Ear to JBoss 4.2 I get the following stack trace:


            java.lang.NoClassDefFoundError: org/jfree/data/category/IntervalCategoryDataset
                 at java.lang.Class.getDeclaredMethods0(Native Method)
                 ...
            



            If I change nothing, except for to give null as a parameter to ChartFactory.createGanttChart(), a chart with no data is drawn.
            IntervalCategoryDataset is an interface, which has a sub-interface GanttCategoryDataset which is implemented by TaskSeriesCollection which I am using. If I create another TaskSeriesCollection:


            TaskSeriesCollection seriesCollection2 = null;
            



            And pass that null collection to ChartFactory.createGanttChart(), the application deploys fine (though obviously shows a blank chart).


            What am I doing wrong? Am I simply misunderstanding how interfaces work?


            Thanks,

            Dave

            • 3. Re: How to display a gantt chart with Seam??
              richie wang Newbie

              copy jfreechart.jar ,jcommen.jar to your ear.

              • 4. Re: How to display a gantt chart with Seam??
                lucius yu Newbie

                make sure ur 'deployed-jars-ear.list' config has the jfreechart jar listed or it won't be added to ear lib