AngularJS and Flotr2 (or any other charting library)

2,219 views
Skip to first unread message

m48u

unread,
Jan 31, 2013, 6:03:39 AM1/31/13
to ang...@googlegroups.com
Hi,

I'm trying to combine AngularJS with a chart plotting library. I already tried jqplot, d3 flot and flotr2. But none of them work the way I expect.

By drawing the chart directly from my controller it works fine, but as soon as I put it into a directive the console blames about "The target container must be visible".

Any ideas for that?
Are there any projects that implement any of the charting libraries?  What are best practices by working with charts?

Thanks in advance
m48u

m48u

unread,
Feb 1, 2013, 8:20:35 AM2/1/13
to ang...@googlegroups.com
Hi,

now i managed to extract it into a directive without error but the chart doesn't display any data.


Any ideas to fix that?
Thanks

Christopher Sexton

unread,
Mar 1, 2013, 3:07:07 PM3/1/13
to ang...@googlegroups.com

Did you ever have any success here? I am battling the same situation. Flotr2 does not like it when you pass in the elem provided by the directive.

//this results in an error "The target container must be visible"
//Flotr.draw(elem, [flot.data], flot.options); 

//without a isolated scope everthing is working
//Flotr.draw(document.getElementById(flot.id), [scope.flotr.data], scope.flotr.options);

E_lexy

unread,
Mar 1, 2013, 5:56:33 PM3/1/13
to ang...@googlegroups.com
I am succesfully using Highcharts.
Here is my directive:
directive('appVersion', ['version', function(version) {
  return function(scope, elm, attrs) {
    elm.text(version);
  };
}]).directive('chart', function() {
  return {
    restrict: 'A',
    scope: {
      readings: '=',
      latest: '='
    },
    replace: true,

    controller: function($scope, $element, $attrs) {
      console.log(2);

    },
    template: '<div id="container" style="margin: 0 auto">not working</div>',
    link: function(scope, element, attrs) {
      console.log(3);
      var chart = new Highcharts.Chart({
        chart: {
          renderTo: 'container',
          zoomType: 'x',
          spacingRight: 20,
          alignTicks: false
        },
        title: {
          text: 'Heating overview SeretaLabs, Camarles'
        },
        tooltip: {
          shared: true
        },
        plotOptions: {
          series: {
            marker: {
              enabled: false
            }
          }
        },
        yAxis: [{ // Primary yAxis
          height: 200,
          labels: {
            formatter: function() {
              return this.value + '°C';
            }
          },
          title: {
            text: 'Temperature',
          }
        }, { // Secondary yAxis
          title: {
            text: 'Pump on/off',
            style: {
              color: '#4572A7'
            }
          },
          labels: {
            style: {
              color: '#4572A7'
            }
          },
          top: 270,
          height: 50,
          offset: 1,
          lineWidth: 2,
          max: 1,
          min: 0
          //opposite: true
        }],
        xAxis: {
          type: 'datetime',
          maxZoom: 60,
          title: {
            text: null
          }
        },
        legend: {
          // layout: 'vertical',
          // align: 'left',
          // x: 80,
          verticalAlign: 'top',
          y: 20,
          floating: true
        },
        series: [{
          type: 'spline',
          name: 'Top Tank',
          data: null,
        }, {
          type: 'spline',
          name: 'After heater',
          data: null,
        }, {
          type: 'spline',
          name: 'Panel out',
          data: null,
        }, {
          type: 'spline',
          name: 'Panel in',
          data: null,
        }, {
          type: 'spline',
          name: 'Temp Outside',
          data: null,
        }, {
          type: 'spline',
          name: 'Xchanger out',
          data: null,
        }, {
          type: 'spline',
          name: 'Floor in',
          data: null,
        }, {
          name: 'Gas heater',
          //color: '#4572A7',
          type: 'line',
          yAxis: 1,
          data: null
        }, {
          name: 'Floorpump',
          //color: '#4572A7',
          type: 'line',
          yAxis: 1,
          data: null
        }, {
          name: 'Solarpump',
          //color: '#4572A7',
          type: 'line',
          yAxis: 1,
          data: null

        }, ]
      });
      scope.$watch("readings", function(values) {
        var series = {};
        var arrBool = ['sp', 'fp', 'hp']
        angular.forEach(values, function(data, key) {
          var date = data.key;
          angular.forEach(data.value[0], function(value, key) {
            if(!Array.isArray(this[key])) this[key] = [];
            var divider = arrBool.indexOf(key) !== -1 ? 1 : 10;
            this[key].push([date, value / divider]);
            var tes;
          }, series);
        }, series);
        chart.series[0].setData(series.tt);
        chart.series[1].setData(series.ah);
        chart.series[2].setData(series.po);
        chart.series[3].setData(series.pi);
        chart.series[4].setData(series.pa);
        chart.series[5].setData(series.xo);
        chart.series[6].setData(series.fi);
        chart.series[7].setData(series.hp);
        chart.series[8].setData(series.fp);
        chart.series[9].setData(series.sp, true);

      }, true);

      scope.$watch("latest", function(reading) {
        if(reading) {
          var series = {};
          var arrBool = ['sp', 'fp', 'hp']

          var date = reading.key;
          angular.forEach(reading.value[0], function(value, key) {
            if(!Array.isArray(this[key])) this[key] = [];
            var divider = arrBool.indexOf(key) !== -1 ? 1 : 10;
            this[key] = [date, value / divider];
            var tes;
          }, series);

          chart.series[0].addPoint(series.tt, false, true);
          chart.series[1].addPoint(series.ah, false, true);
          chart.series[2].addPoint(series.po, false, true);
          chart.series[3].addPoint(series.pi, false, true);
          chart.series[4].addPoint(series.pa, false, true);
          chart.series[5].addPoint(series.xo, false, true);
          chart.series[6].addPoint(series.fi, false, true);
          chart.series[7].addPoint(series.hp, false, true);
          chart.series[8].addPoint(series.fp, false, true);
          chart.series[9].addPoint(series.sp, false, true);
          chart.redraw();
        }

      }, true);
    }
  }
})

Also here is some documentation on using nd3js

I see he has now changed to use flotr2. Just check JPs repo here: https://github.com/jcw/housemon
It is all coffeescript...

E_lexy

unread,
Mar 1, 2013, 5:58:27 PM3/1/13
to ang...@googlegroups.com
Oh, my repo is here: https://bitbucket.org/E_lexy/jmonitor
It's based on the node angularJS seed app.


On Thursday, January 31, 2013 12:03:39 PM UTC+1, m48u wrote:

Dan Kang

unread,
Mar 2, 2013, 1:50:46 AM3/2/13
to ang...@googlegroups.com
Hi there,

I'm curious as to what problems you had with d3.js?  I've been looking into using d3.js in conjunction with AngularJS though I haven't done a whole lot of exploration yet.  Anyone else has experience using both the same project?

Dan

Thomas Stapleton

unread,
Mar 4, 2013, 12:49:24 PM3/4/13
to ang...@googlegroups.com
Brian Ford has a great write up on using D3.js with Angular - http://briantford.com/blog/angular-d3.html.

I built something pretty simple with D3.js and Angular - https://github.com/tstapleton/cbm. There were enough resources on the web to figure it out.

Dan Kang

unread,
Mar 4, 2013, 2:50:06 PM3/4/13
to ang...@googlegroups.com
Great stuff, thanks Thomas!
Message has been deleted
Message has been deleted

kurt.t....@gmail.com

unread,
Apr 11, 2013, 6:25:20 AM4/11/13
to ang...@googlegroups.com
Hi Christopher, I am not sure if you are still interested in this, but I have managed to successfully get
directives working with angular.

However, I was restricted to attribute directives. Also, the element should be referenced by elem[0]
i.e. //Flotr.draw(elem[0], [flot.data], flot.options);

an example bar chart can be seen below -- inspired by a google chart integration I saw a couple months ago.
http://jsfiddle.net/kurtteichman/KDYSN/

Arnold Bockenbauer

unread,
Oct 11, 2013, 10:39:51 AM10/11/13
to ang...@googlegroups.com
Hi guys ! We've built a directive that uses d3.js to provide versatile, ready out-of-the-box charts in AngularJS !

Reply all
Reply to author
Forward
0 new messages