-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpie-chart.js
118 lines (107 loc) · 3.7 KB
/
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
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
/* global jQuery3 */
jQuery3(document).ready(function () {
/**
* Renders all div elements that have the class 'echarts-pie-chart' using ECharts.
*/
function renderPieCharts() {
/**
* Renders a trend chart in the a div using ECharts.
*
* @param {String} chartDivId - the ID of the div where the chart should be shown in
*/
function renderPieChart(chartDivId) {
function isEmpty(string) {
return (!string || string.length === 0);
}
/**
* Returns the title properties of the chart.
*
* @param {String} title - the title
*/
function getTitle(title) {
if (!isEmpty(title)) {
return {
text: title,
textStyle: {
fontWeight: 'normal',
fontSize: '16'
},
left: 'center'
};
}
else {
return null;
}
}
const chartPlaceHolder = jQuery3("#" + chartDivId);
const model = JSON.parse(chartPlaceHolder.attr('data-chart-model'));
const title = chartPlaceHolder.attr('data-title');
const chartDiv = chartPlaceHolder[0];
const chart = echarts.init(chartDiv);
chartDiv.echart = chart;
const textColor = getComputedStyle(document.body).getPropertyValue('--text-color') || '#333';
const options = {
title: getTitle(title),
tooltip: {
trigger: 'item',
formatter: '{b}: {c} ({d}%)'
},
legend: {
orient: 'horizontal',
x: 'center',
y: 'bottom',
type: 'scroll',
textStyle: {
color: textColor
}
},
series: [{
type: 'pie',
radius: ['30%', '70%'],
avoidLabelOverlap: false,
color: model.colors,
label: {
normal: {
show: false,
position: 'center'
},
emphasis: {
show: false
}
},
labelLine: {
normal: {
show: true
}
},
data: model.data
}
]
};
chart.setOption(options);
chart.resize();
const useLinks = chartPlaceHolder.attr('data-links');
if (useLinks && useLinks !== "false") {
chart.on('click', function (params) {
window.location.assign(params.name);
});
}
return chart;
}
const allPieCharts = jQuery3('div.echarts-pie-chart');
const pieChartInstances = [];
allPieCharts.each(function () {
const chart = jQuery3(this);
const id = chart.attr('id');
pieChartInstances.push(renderPieChart(id));
});
if (pieChartInstances.length > 0) {
jQuery3(window).resize(function () {
pieChartInstances.forEach(function (chartInstance) {
chartInstance.resize();
});
});
}
}
renderPieCharts();
});