Skip to content

Commit

Permalink
Merge pull request #15 from Ecodev/master
Browse files Browse the repository at this point in the history
Could not render chart without renderTo
  • Loading branch information
rootux committed Feb 28, 2014
2 parents 8c2eefa + 3d2273b commit 78e4b79
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
1 change: 1 addition & 0 deletions sample/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

angular.module('chartsExample.controllers',[]).controller('MainCtrl', ['$scope','$http',
function($scope,$http) {
$scope.chartObj; // this will contain a reference to the highcharts' chart object
$http.get("charts/basicAreaChart.json").success(function(data) {
$scope.basicAreaChart = data;
});
Expand Down
2 changes: 1 addition & 1 deletion sample/views/main.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<chart value='basicAreaChart' type="area" height="400">
<chart value='basicAreaChart' type="area" height="400" chart-obj='chartObj'>
</chart>
</div>
27 changes: 11 additions & 16 deletions src/directives/highchart.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@ angular.module('chartsExample.directives',[])
restrict: 'E',
template: '<div></div>',
scope: {
chartData: "=value"
chartData: "=value",
chartObj: "="
},
transclude:true,
replace: true,

link: function (scope, element, attrs) {
var chartsDefaults = {
chart: {
renderTo: element[0],
type: attrs.type || null,
height: attrs.height || null,
width: attrs.width || null
}
};


//Update when charts data changes
scope.$watch(function() { return scope.chartData; }, function(value) {
if(!value) return;
// We need deep copy in order to NOT override original chart object.
// This allows us to override chart data member and still the keep
// our original renderTo will be the same
var newSettings = {};
angular.extend(newSettings, chartsDefaults, scope.chartData);
var chart = new Highcharts.Chart(newSettings);

// use default values if nothing is specified in the given settings
scope.chartData.chart.renderTo = scope.chartData.chart.renderTo || element[0];
if (attrs.type) scope.chartData.chart.type = scope.chartData.chart.type || attrs.type;
if (attrs.height) scope.chartData.chart.height = scope.chartData.chart.height || attrs.height;
if (attrs.width) scope.chartData.chart.width = scope.chartData.chart.type || attrs.width;

scope.chartObj = new Highcharts.Chart(scope.chartData);
});
}
};
Expand Down

0 comments on commit 78e4b79

Please sign in to comment.