-
Notifications
You must be signed in to change notification settings - Fork 0
/
chasingchart-latest.js
344 lines (297 loc) · 10.1 KB
/
chasingchart-latest.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
/*
Chasingchart JS v1.5.0 (2019-01-05)
(c) 2018 Teruki Shinohara
License: github.com/ts-3156/chasingchart/blob/master/LICENSE
*/
var Chasingchart = {};
Chasingchart.chart = function (_selector, _options) {
var input = null;
var inputIndex = 0;
var chart = null;
var started = false;
var stopped = false;
var duration = _options && _options.duration || 750;
var selector = _selector;
var COLORS = ["#e6194B", "#3cb44b", "#ffe119", "#4363d8", "#f58231", "#911eb4", "#42d4f4", "#f032e6", "#bfef45", "#fabebe", "#469990", "#e6beff", "#9A6324", "#fffac8", "#800000", "#aaffc3", "#808000", "#ffd8b1", "#000075"];
var formatter = function formatter(value) {
// return Highcharts.numberFormat(this.y, 0, '', ',');
var v = void 0;
if (value === 0) {
v = 0;
} else {
v = typeof value === 'number' && value || this.y;
}
if (v > 10000000000) {
v = Math.floor(v / 1000000000) + "G";
} else if (v > 10000000) {
v = Math.floor(v / 1000000) + "M";
} else if (v > 10000) {
v = Math.floor(v / 1000) + "K";
} else {
v = Math.floor(v);
}
return v;
};
var deepCopy = function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
};
var merge = function merge(src1, src2) {
// return Object.assign(deepCopy(src1), deepCopy(src2));
return Highcharts.merge(false, deepCopy(src1), deepCopy(src2));
};
var baseSeries = [{
name: 'Series 1',
animation: { duration: duration },
data: []
}];
var baseOptions = {
chart: {
type: 'bar'
},
title: {
text: null
},
subtitle: {
text: null
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
align: 'right',
formatter: formatter
}
}
},
xAxis: {
categories: [],
title: {
text: null
}
},
yAxis: {
min: 0,
endOnTick: false,
// maxPadding: 0.0,
labels: {
rotation: 0
},
title: {
text: null
}
},
tooltip: {
enabled: false
},
exporting: false,
credits: false,
legend: false,
series: null
};
var formatInputData = function formatInputData(input) {
var colors = {};
Object.keys(input[0].data).forEach(function (category, i) {
colors[category] = COLORS[i % COLORS.length];
});
var sortedInput = [];
input.forEach(function (elem, i) {
var ary = [];
Object.keys(elem.data).forEach(function (category, i) {
ary.push({ value: elem.data[category], category: category });
});
ary.sort(function (a, b) {
return b.value - a.value;
});
var sortedElem = { values: [], categories: [] };
ary.forEach(function (obj, i) {
sortedElem.values[i] = { y: obj.value, color: colors[obj.category] };
sortedElem.categories[i] = obj.category;
});
if (elem.options) {
sortedElem.options = deepCopy(elem.options);
} else {
sortedElem.options = {};
}
sortedInput[i] = sortedElem;
});
return sortedInput;
};
var countUp = function countUp(duration, startedCallback, finishedCallback) {
var values = [];
chart.series[0].data.forEach(function (d, i) {
values[i] = d.y; // Sorted by value
});
var nextValues = [];
chart.xAxis[0].categories.forEach(function (c, i) {
input[inputIndex].categories.forEach(function (cc, j) {
if (c === cc) {
nextValues[i] = input[inputIndex].values[j].y; // Sorted by current value
}
});
});
chart.series[0].setData(nextValues, true, { duration: duration });
if (startedCallback) {
startedCallback();
}
var counter = 0;
var maxSteps = 10;
var updateDataLabels = function updateDataLabels() {
chart.series[0].points.forEach(function (point, i) {
var actualValue = void 0;
if (counter === maxSteps) {
actualValue = nextValues[i];
} else {
if (values[i] === nextValues[i]) {
actualValue = nextValues[i];
} else {
var diff = parseFloat(counter + 1) * Math.abs(nextValues[i] - values[i]) / maxSteps;
if (values[i] < nextValues[i]) {
actualValue = values[i] + diff;
} else {
actualValue = values[i] - diff;
}
}
}
point.dataLabel.attr({
text: formatter(actualValue)
});
});
};
updateDataLabels();
counter++;
var timer = setInterval(function () {
updateDataLabels();
counter++;
if (counter >= maxSteps) {
clearInterval(timer);
if (finishedCallback) {
finishedCallback();
}
}
}, duration / maxSteps);
};
var rotateBars = function rotateBars(duration, callback) {
var values = [];
var categories = chart.xAxis[0].categories;
chart.series[0].data.forEach(function (d, i) {
values[i] = { value: d.y, category: categories[i] }; // Sorted by value
});
var sortedValues = [];
chart.xAxis[0].categories.forEach(function (c, i) {
input[inputIndex].categories.forEach(function (cc, j) {
if (c === cc) {
sortedValues[i] = { value: input[inputIndex].values[j].y, category: c }; // Sorted by current value
}
});
});
sortedValues.sort(function (a, b) {
return b.value - a.value;
});
// TODO Don't call rotateBars right after calling countUp.
// Because chart.series[0].setData overwrite the positions of bars.
var isChanged = false;
values.forEach(function (value, i) {
if (value.category !== sortedValues[i].category) {
isChanged = true;
}
});
if (!isChanged) {
console.log('rotateBars', 'not changed');
if (callback) {
callback();
}
return;
}
var points = chart.series[0].points;
var ticks = chart.xAxis[0].ticks;
values.forEach(function (value, i) {
sortedValues.forEach(function (sValue, j) {
if (value.category === sValue.category && i !== j) {
console.log('rotateBars', value.category, i, j);
points[i].graphic.animate({
x: points[j].shapeArgs.x
}, { duration: duration });
points[i].dataLabel.animate({
y: points[j].dataLabel.y
}, { duration: duration });
ticks[i].label.animate({
y: ticks[j].label.xy.y
}, { duration: duration });
}
});
});
if (callback) {
setTimeout(function () {
callback();
}, duration);
}
};
var reDraw = function reDraw(series, categories, options, callback) {
options.xAxis.categories = categories;
options.series = series;
chart.destroy();
chart = Highcharts.chart(selector, options);
if (callback) {
callback();
}
};
var update = function update(callback) {
if (input.length - 1 <= inputIndex) {
started = false;
if (callback) {
callback();
}
return;
}
inputIndex++;
var nextSeries = deepCopy(baseSeries);
nextSeries[0].data = input[inputIndex].values;
var options = deepCopy(baseOptions);
if (input[inputIndex].options) {
options = merge(options, input[inputIndex].options);
}
options.plotOptions.series.dataLabels.formatter = formatter;
countUp(duration, function () {
rotateBars(Math.floor(duration * 0.8), function () {});
}, function () {
nextSeries[0].animation = false;
reDraw(nextSeries, input[inputIndex].categories, options, function () {
if (stopped) {
started = false;
if (callback) {
callback();
}
} else {
update(callback);
}
});
});
};
var start = function start(callback) {
started = true;
stopped = false;
update(callback);
};
var stop = function stop() {
stopped = true;
};
return {
setData: function setData(inputData) {
input = formatInputData(inputData);
inputIndex = 0;
var series = deepCopy(baseSeries);
series[0].data = input[inputIndex].values;
var options = deepCopy(baseOptions);
if (input[inputIndex].options) {
options = merge(options, input[inputIndex].options);
}
options.plotOptions.series.dataLabels.formatter = formatter;
options.series = series;
options.xAxis.categories = input[inputIndex].categories;
chart = Highcharts.chart(selector, options);
},
update: update,
start: start,
stop: stop
};
};