-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsketch.js
363 lines (339 loc) · 9.15 KB
/
sketch.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
(function() {
/** @type {Object} Default Config */
var defconfig = {
canvas: 'mycanvas',
cw: 800,
ch: 800,
fontSize: 11,
labelLineWidth: 10,
labelLineHeight: 4,
labelLineOffset: 4,
labelStrokeWeight: 1,
labelColor: '#111111',
outSideLineWidth: 10,
donutWith: 100,
radius: 100,
hoverOpacPercent: 0.1,
secondColorOpacPercent: 0.2,
values: [],
onhover: function() {}
};
/** Global Context of Canvas and config*/
var ctx;
var _config;
/**
* Constructor
* @param {Object} userConfig User config
*/
function Chart(userConfig) {
// @TODO : Extend
_config = mergeOptions(defconfig, userConfig);
// this.values = _config.values
this.init();
}
// For performance
Chart.prototype.canvas;
Chart.prototype.width;
Chart.prototype.height;
Chart.prototype.mouseX;
Chart.prototype.mouseY;
Chart.prototype.total;
Chart.prototype.config = {};
// Chart.prototype.values=[];
Chart.prototype.slices = [];
/**
* Inıtılaze Chart
* @return {Void}
*/
Chart.prototype.init = function() {
var c = _config;
this.canvas = document.getElementById(c.canvas);
ctx = this.canvas.getContext('2d');
this.canvas.width = c.cw;
this.canvas.height = c.ch;
this.width = c.cw;
this.height = c.ch;
};
Chart.prototype.draw = function() {
this.setValues();
this.attachListener();
this.render();
};
/**
* Preload from render
*/
Chart.prototype.setValues = function() {
this.slices = [];
var c = _config;
var total = getTotal(c.values);
var lastAngle = 0;
var cx = this.width / 2;
var cy = this.height / 2;
for (var i = 0; i < c.values.length; i++) {
var end = lastAngle + radians(c.values[i].max / total);
// Slice Parent
this.slices.push(
new Slice(
cx, //x
cy, //y
c.radius + c.donutWith, //radius
lastAngle, //startangle
end, //endangle
shadeColor(c.values[i].currentColor, c.secondColorOpacPercent), //color
true, //is parent
c.values[i], //data,
c.values[i].labelText
)
);
// Slice Child
this.slices.push(
new Slice(
cx,
cy,
c.values[i].current / c.values[i].max * c.radius + c.donutWith,
lastAngle,
end,
c.values[i].currentColor,
false,
c.values[i]
)
);
lastAngle += radians(c.values[i].max / total);
}
};
/** Mouse Handle */
Chart.prototype.attachListener = function() {
var self = this;
this.canvas.addEventListener('mousemove', function(e) {
var x = e.clientX - e.target.getBoundingClientRect().left;
var y = e.clientY - e.target.getBoundingClientRect().top;
this.mouseX = e.clientX;
this.mouseY = e.clientY;
for (var i = 0; i < self.slices.length; i += 2) {
self.slices[i].render(function() {
if (ctx.isPointInPath(x, y)) {
self.slices[i].highlighted = true;
self.slices[i + 1].highlighted = true;
_config.onhover({
parent: self.slices[i],
child: self.slices[i + 1],
x: x,
y: y
});
} else {
self.slices[i].highlighted = false;
self.slices[i + 1].highlighted = false;
}
ctx.closePath();
});
}
self.render();
});
};
/** Render Slice of Pie */
Chart.prototype.render = function() {
ctx.clearRect(0, 0, this.width, this.height);
var self = this;
this.slices.map(function(p) {
p.render();
});
ctx.beginPath();
ctx.arc(
this.width / 2,
this.height / 2,
_config.donutWith,
0,
2 * Math.PI,
false
);
ctx.fillStyle = 'white';
ctx.fill();
ctx.closePath();
//_requestAnimationFrame(this.render.bind(this));
};
/** Update Pie */
Chart.prototype.update = function(config) {
_config = mergeOptions(_config, config);
this.setValues();
};
/**
* Slice of Pie
* @param {Number} cx Center of slice X
* @param {Number} cy Center of slice Y
* @param {Number} r Radius
* @param {Number} pos Start Angle
* @param {Number} len End Angle
* @param {String} col Primary Color
* @param {Bollean} isparent
* @param {Object} data Data of Slice
* @param {String} labelText Label string
*/
function Slice(cx, cy, r, pos, len, col, isparent, data, labelText) {
this.isparent = isparent;
this.data = data;
this.cx = cx;
this.cy = cy;
this.radius = r;
this.start = pos;
this.end = len;
this.color = col;
this.highlightedColor = shadeColor(this.color, _config.hoverOpacPercent);
this.highlighted = false;
this.labelText = labelText;
}
/** @todo Show tooltip */
Slice.prototype.displayData = function(x, y) {};
/** Draw Slice */
Slice.prototype.render = function(cb) {
if (!this.highlighted) {
ctx.fillStyle = this.color;
} else {
ctx.fillStyle = this.highlightedColor;
}
ctx.strokeStyle = this.data.currentColor;
ctx.beginPath();
ctx.closePath();
if (this.isparent) {
// ctx.lineTo(this.cx, this.cy);
ctx.arc(this.cx, this.cy, this.radius, this.start, this.end, false);
ctx.lineWidth = _config.outSideLineWidth;
ctx.stroke();
}
ctx.moveTo(this.cx, this.cy);
ctx.lineTo(this.cx, this.cy);
ctx.arc(this.cx, this.cy, this.radius, this.start, this.end);
ctx.fill();
if (cb) cb();
if (this.highlighted) {
// Show tolltip
this.displayData();
}
if (this.isparent) {
this.showLine();
}
};
/** Draw slice label line */
Slice.prototype.showLine = function() {
if (this.labelText) {
var w = _config.cw;
var h = _config.ch;
var angle = (this.start + this.end) / 2;
var centerSliceX =
this.cx +
(this.radius + _config.outSideLineWidth / 2) * Math.cos(angle);
var centerSliceY =
this.cy +
(this.radius + _config.outSideLineWidth / 2) * Math.sin(angle);
var pos = getSteps(angle);
var elX = centerSliceX + pos.x * _config.labelLineWidth;
var elY = centerSliceY + pos.y * _config.labelLineWidth;
ctx.lineWidth = _config.labelStrokeWeight;
ctx.strokeStyle = '#111111';
ctx.beginPath();
// // Line
ctx.moveTo(centerSliceX, centerSliceY);
ctx.lineTo(elX, elY);
ctx.font = '10pt Calibri';
this.labelText = this.labelText.replace(/%m/g, this.data.max);
this.labelText = this.labelText.replace(/%c/g, this.data.current);
var text = this.labelText;
var textpos = ctx.measureText(text);
ctx.fillStyle = _config.labelColor;
ctx.textAlign = 'center';
if (centerSliceX > w / 2) {
ctx.lineTo(elX + _config.labelLineWidth, elY);
ctx.fillText(
text,
elX +
_config.labelLineOffset +
_config.labelLineWidth +
textpos.width / 2,
elY + _config.labelLineHeight
);
}
if (centerSliceX < w / 2) {
ctx.lineTo(elX - _config.labelLineWidth, elY);
ctx.fillText(
text,
elX -
_config.labelLineOffset -
_config.labelLineWidth -
textpos.width / 2,
elY + _config.labelLineHeight
);
}
ctx.stroke();
ctx.closePath();
}
};
/** @todo Handle Update */
Slice.prototype.update = function() {};
/**
* Position of slice label line
* @param {Number} angle
* @return {Number} Position x and y origin by center of pie
*/
function getSteps(angle) {
var cos = Math.cos(angle), sin = Math.sin(angle);
return {
x: cos,
y: sin
};
}
function radians(x) {
return 2 * Math.PI * x;
}
/**
* Get all max value totatl
* @param {Array} v Values fo array
* @return {Number} Total size
*/
function getTotal(v) {
return v.reduce(function(a, b) {
return (a += b.max);
}, 0);
}
/**
* Opac color
* @param {String} color
* @param {Number} percent
* @return {String}
*/
function shadeColor(color, percent) {
var f = parseInt(color.slice(1), 16),
t = percent < 0 ? 0 : 255,
p = percent < 0 ? percent * -1 : percent,
R = f >> 16,
G = (f >> 8) & 0x00ff,
B = f & 0x0000ff;
return (
'#' +
(0x1000000 +
(Math.round((t - R) * p) + R) * 0x10000 +
(Math.round((t - G) * p) + G) * 0x100 +
(Math.round((t - B) * p) + B))
.toString(16)
.slice(1)
);
}
/** @type {Native Code} Pollyfl animationframe */
var _requestAnimationFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setInterval.bind(this, callback, 1000 / 60);
};
//Merge
function mergeOptions(obj1, obj2) {
var obj3 = {};
for (var attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (var attrname in obj2) {
obj3[attrname] = obj2[attrname];
}
return obj3;
}
window.Chart = Chart;
})();