Repository navigation
Expand file tree
/
Copy pathbzd3panel.js
More file actions
216 lines (169 loc) · 5.74 KB
/
Copy pathbzd3panel.js
File metadata and controls
216 lines (169 loc) · 5.74 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
(function() { // IIFE to avoid exposure in global scope.
'use strict';
angular
.module('bzD3', [
]);
})(); // End of IIFE
(function() { // IIFE to avoid exposure in global scope.
'use strict';
angular
.module('bzD3')
.factory('d3Service', d3Service);
d3Service.$inject = ['$document', '$q', '$rootScope'];
function d3Service($document, $q, $rootScope) {
var d = $q.defer();
function onScriptLoad() {
// Load client in the browser.
$rootScope.$apply(function() { d.resolve(window.d3); });
}
// Callback when loaded.
// @TODO: add fallback to local copy if CDN not available.
var scriptTag = $document[0].createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.async = true;
scriptTag.src = "http://d3js.org/d3.v3.min.js";
scriptTag.onload = onScriptLoad;
scriptTag.onreadystatechange = function() {
if(this.readyState == 'complete') onScriptLoad();
}
var s = $document.prop("body");
s.appendChild(scriptTag);
return {
d3: function() { return d.promise; }
};
}
})(); // End of IIFE
(function() { // IIFE to avoid exposure in global scope.
'use strict'
angular
.module("bzD3")
.directive('bzD3Panel', bzD3Panel);
bzD3Panel.$inject = ['d3Service', '$window'];
function bzD3Panel(bzD3, $window) {
return {
restrict: 'EA',
template: '<div ng-transclude></div>',
transclude: true,
scope: {
counter: '=',
bzD3Render: '=', // Internal actions.
content: '=', // Data to draw.
render: '=', // Create drawing.
redraw: '=' // Redraw drawing.
},
link: function(scope,element,attrs) {
// Keep our actions handy.
scope.bzD3Render = {
render: scope.render,
redraw: scope.redraw,
doRender: doRender,
doRedraw: doRedraw
};
// The actual drawing.
var svg;
// DEPRECATED. Location adjustments.
var margin = parseInt(attrs.margin) || 20;
var padding = parseInt(attrs.padding) || 5;
var height;
var width;
establishSize();
function establishSize() {
// Drawing height from container.
if(attrs.height) {
height = parseInt(attrs.height);
} else {
height = $(element).parent().height();
attrs.height = height;
}
// Drawing width from container.
if(attrs.width) {
width = parseInt(attrs.width);
} else {
width = $(element).parent().width();
attrs.width = width;
}
}
bzD3.d3().then(function(d3) {
// Create the drawing after we have the D3.js library available.
svg = d3.select(element[0])
.insert('svg',':first-child')
.style('height', height + 'px')
.style('width', width + 'px');
// Check for changes when the window is resized.
$window.onresize = function() {
scope.$apply;
};
// Redraw when the size changes.
scope.$watch(function() {
return element[0].firstElementChild.clientWidth;
}, function() {
if(scope.content) {
establishSize();
scope.bzD3Render.doRedraw(scope.content);
}
});
// Redraw when the data changes.
scope.$watchCollection('content', function(newVals,oldVals) {
if(scope.content) {
establishSize();
scope.bzD3Render.doRedraw(scope.content);
}
}, true);
// Render the drawing as soon as feasible.
scope.bzD3Render.doRender(scope.content);
});
// Rendering function. The fun happens here. :)
function doRender(data) {
if(!scope.bzD3Render.render) {
var novalue = [0];
defaultRender(d3, svg, novalue, element, attrs);
} else {
if(data) {
scope.bzD3Render.render(d3, svg, data, element, attrs);
}
}
if(scope.content) {
// Add the content if there is data to draw with. Otherwise
// this will be called when data becomes available.
scope.bzD3Render.doRedraw(scope.content);
}
};
// Redrawing function. The fun happens here. :)
function doRedraw(data) {
if(!scope.bzD3Render.redraw) {
var novalue = [0];
defaultRedraw(d3, svg, novalue, element, attrs);
} else {
if(data) {
scope.bzD3Render.redraw(d3, svg, data, element, attrs);
}
}
};
function defaultRender(d3, svg, data, element, attrs) {
svg.style('stroke-width', '0px')
.style('background-color', '#bf8ea6');
}
function defaultRedraw(d3, svg, data, element, attrs) {
var g = svg.selectAll("g")
.data(data).enter().append("g");
g.append("circle")
.style("fill", "red")
.style("stroke", "black")
.style("stroke-width", "2")
.attr("cx",width/2)
.attr("cy",height/2)
.attr("r",40);
g.append("text")
.attr("x",(width/2)-30)
.attr("y",(height/2))
// .style('color','ivory')
// .style('stroke','ivory')
.text("LOL WUT");
g.append("text")
.attr("x",30)
.attr("y",20)
.text('Number of insert events: ' + ++scope.counter);
}
}};
}
})(); // End of IIFE