This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathsimple-pie-chart.js
82 lines (75 loc) · 1.91 KB
/
simple-pie-chart.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
import { Pie } from '../../../../../config/vue-chartjs.js';
import { optionalChain } from '../../../../../utils/object.utils.js';
import { colorize, transparentize } from '../charts.utils.js';
const _defaultChartOptions = {
lowerIsBetter: false,
};
const _SimplePieChart = {
computed: {
chartData: function() {
const data = this.sortedData;
const backColors = data.map(x => this.normalColorize(x));
const res = {
datasets: [
{
data: data,
backgroundColor: backColors,
hoverColor: backColors.map(x => this.hoverColorize(x)),
},
],
labels: this.context.labels,
};
return res;
},
sortedData: function() {
let data = optionalChain(() => [...this.context.data], { fallbackValue: [] }).sort(
(a, b) => a - b
);
if (this.options.lowerIsBetter) {
data = data.reverse();
}
return data;
},
},
extends: Pie,
methods: {
normalColorize: function(value) {
const data = this.sortedData;
const min = Math.min(...data) || 0;
const max = Math.max(...data) || 100;
return colorize(value, { range: [min, max], lowerIsBetter: this.options.lowerIsBetter });
},
hoverColorize: function(color) {
return transparentize(color);
},
refresh: function() {
this.styles = { width: '100%', height: '100%', position: 'relative', ...this.styles };
this.renderChart(this.chartData, {
maintainAspectRatio: false,
});
},
},
mounted() {
this.refresh();
},
props: {
context: {
type: Object,
default: {
data: [],
labels: [],
},
},
options: {
type: Object,
default: _defaultChartOptions,
},
},
watch: {
context: function() {
this.refresh();
},
},
};
export const SimplePieChart = _SimplePieChart;
export default _SimplePieChart;