Search code examples
jfreechartgoogle-visualizationsproutcorebar-chart

working bar charts/tables with sproutcore


I am currently working on a task which want to display bar charts/tables on the website.

The application is using: sproutcore (1.6) as front-end, Java Restful as backend.

However, I can't find some useful library for charts in sproutcore. Are there any ideas for that?

I search on the website, I feel the google chart tools is quite good, also jFreechart as backend is also a good choice.

I am not sure how to integrate that to sproutcore.

Thanks.


Solution

  • I'm using Flot to display charts on my Sproutcore app.

    To use it, you need to create a flot directory inside the frameworks folder which will include the jquery.flot.js file (I've also include jquery.flot.pie.js file) and a core.js file with this content:

    sc_require('jquery.flot.js');
    sc_require('jquery.flot.pie.js');
    
    Flot = SC.Object.create({
      plot: $.plot
    }) ;
    

    Then, you need to add this new library to your buildfile :

    config :yourapp,  
      :required => ['flot']
    

    To display your charts in your app, you can use this custom view that I made to work with Flot:

    GX.FlotView = SC.View.extend({
      classNames: ['flot'],
    
      //ex: [{ data: [[1326366000000, 1500], [1326452400000, 600]], label: 'title of the serie' }, ...]
      data: [],
    
      /* 
      ex: {
        legend: { show: true },
        series: line, points,
        xaxis: { mode: "time", timeformat: "%d/%m/%y", }
        grid: { backgroundColor: { colors: ["#FFF", "#fefefe"]}},
      } 
      */
      options: [],
    
    
      render: function(context, firstTime) { 
        var frame = this.get('frame'),
            data = this.get('data'), 
            options = this.get('options');
    
        // To avoid an error with flot, we check if width and height > 0
        if(frame.width > 0 && frame.height > 0 && data && data.length) {  
          var layer = this.get('layer'),
              plot = Flot.plot(layer, data, options);
        }
      },
    
    
      viewDidResize: function() {
        this.invokeLast(function() {
          if (this.get('isVisibleInWindow')) this.set('layerNeedsUpdate', YES);
        });
      }.observes('data', 'data.[]'),
    
    });
    

    Then, you just have to bind the data and the option properties with your data.