-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd3Polygon.js
64 lines (60 loc) · 1.95 KB
/
d3Polygon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(function() {
'use strict';
angular.module('app.directives')
.directive('d3Bars', ['d3', function(d3) {
return {
scope: {
data: "=",
offset: "=",
onClick: "&" // parent execution binding
},
link: function(scope, iElement, iAttrs) {
var svg = d3.select(iElement[0]);
// console.log(svg)
// console.log(iElement[0].parentElement.clientWidth);
// console.log(iElement[0].parentElement.clientHeight);
var scaler = {
x: (iElement[0].parentElement.clientWidth - 5) / scope.offset.w,
y: (iElement[0].parentElement.clientHeight - 5) / scope.offset.h
};
console.log(scaler);
// watch for data changes and re-render
scope.$watch('data', function(newVals, oldVals) {
return scope.render(newVals);
}, true);
var lineFunction = d3.svg.line()
.x(function(d) {
// console.log(d);
return (d.x) * scaler.x;
})
.y(function(d) {
return (-d.y + (scope.offset.h + scope.offset.y)) * scaler.y;
})
.interpolate('linear');
// define render function
var fill;
// console.log(scope.offset);
scope.render = function(data) {
svg.selectAll("*").remove();
var fill;
if (data.type == 'Perimeter') {
fill = 'green';
} else {
fill = 'blue';
}
svg.append('path')
.attr('d', lineFunction(data.polygon.points))
.on("click", function(d, i) {
console.log('clicked');
return scope.onClick({
item: 'data'
});
})
.attr("stroke-width", 1)
.attr("stroke", "black")
.attr("fill", fill);
};
}
};
}]);
}());