I am trying to integrate the d3js graph with my angular directive. but I am not getting the expected result.
- the graph is not properly appending to div; instead I am getting
undefined%label - Each time I click on the list ('
li), instead of updating the graph it is appending a new graph.
Please click on the list on the top.
here is my code and demo :
var myApp = angular.module("myApp", ['ngResource']);
myApp.factory("server", function($resource) {
return $resource('https://tcp.firebaseio.com/graph.json')
})
myApp.controller("main", function($scope, server) {
$scope.test = "Hellow";
$scope.data = server.query();
$scope.data.$promise.then(function(result) {
$scope.values = result;
$scope.defaultValue = $scope.values[0].value;
console.log($scope.defaultValue);
})
$scope.updateGraph = function(item) {
$scope.defaultValue = item.value;
}
});
var planVsActual = function($timeout) {
return {
replace: true,
template: "<div id='pieGraph'></div>",
link: function(scope, element, attr) {
$timeout(function() {
scope.$watch("defaultValue", function(newVal, oldVal) {
var phraseValue = [newVal, 100 - newVal];
drawPie(phraseValue);
function drawPie(array) {
console.log(element.width)
var width = element.width(), height = element.height(),
radius = Math.min(width, height) / 1.2, data = array;
if (!array.length) return;
var color = d3.scale.ordinal()
.domain(array)
.range(["#ffff00", "#1ebfc5"]);
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d });
var arc = d3.svg.arc()
.outerRadius(radius - 90)
.innerRadius(radius - 85);
var svg = d3.select("#pieGraph")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var g = svg.selectAll(".arc")
.data(pie(array))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function(d,i) { return color(d.data); });
g.append("text")
.text(array[0]+'%')
.attr("class", "designVal")
.style("text-anchor", "middle")
}
})
}, 100)
}
}
}
angular.module("myApp")
.directive("planVsActual", planVsActual);