-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchartjs-plugin-sorting.js
163 lines (139 loc) · 7.78 KB
/
chartjs-plugin-sorting.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
const ChartjsPluginSorting = {
id: 'sorting',
afterDraw: ((chart, args, plugin) => {
let container = document.querySelector('.chartjs-plugin-sorting-container');
if (container === null) {
// Create the container element and append the canvas to it
container = document.createElement('div');
container.classList.add('chartjs-plugin-sorting-container');
container.style.position = 'relative';
container.style.display = 'flex';
container.style.paddingTop = plugin.container?.padding?.top ?? '10px';
chart.canvas.parentNode.insertBefore(container, chart.canvas);
container.appendChild(chart.canvas);
const buttonAsc = document.createElement('button');
buttonAsc.classList = plugin.asc?.button?.class ?? '';
buttonAsc.innerText = plugin.asc?.button?.label ?? 'Asc';
buttonAsc.style.display = plugin.asc?.button?.display ?? true;
buttonAsc.style.position = 'absolute';
buttonAsc.style.top = (typeof plugin.asc?.button?.topPosition !== 'undefined') ? `${plugin.asc.button.topPosition}px` : '10px';
buttonAsc.style.right = (typeof plugin.asc?.button?.rightPosition !== 'undefined') ? `${plugin.asc.button.rightPosition}px` : '85px';
container.appendChild(buttonAsc);
const buttonDesc = document.createElement('button');
buttonDesc.classList = plugin.desc?.button?.class ?? '';
buttonDesc.innerText = plugin.desc?.button?.label ?? 'Desc';
buttonDesc.style.display = plugin.desc?.button?.display ?? true;
buttonDesc.style.position = 'absolute';
buttonDesc.style.top = (typeof plugin.desc?.button?.topPosition !== 'undefined') ? `${plugin.desc.button.topPosition}px` : '10px';
buttonDesc.style.right = (typeof plugin.desc?.button?.rightPosition !== 'undefined') ? `${plugin.desc.button.rightPosition}px` : '45px';
container.appendChild(buttonDesc);
const buttonReset = document.createElement('button');
buttonReset.classList = plugin.reset?.button?.class ?? '';
buttonReset.innerText = plugin.reset?.button?.label ?? 'Reset';
buttonReset.style.display = plugin.reset?.button?.display ?? true;
buttonReset.style.position = 'absolute';
buttonReset.style.top = (typeof plugin.reset?.button?.topPosition !== 'undefined') ? `${plugin.reset.button.topPosition}px` : '10px';
buttonReset.style.right = (typeof plugin.reset?.button?.rightPosition !== 'undefined') ? `${plugin.reset.button.rightPosition}px` : '0px';
container.appendChild(buttonReset);
const buttonAlphabetical = document.createElement('button');
buttonAlphabetical.classList = plugin.alphabetical?.button?.class ?? '';
buttonAlphabetical.innerText = plugin.alphabetical?.button?.label ?? 'Alphabetical';
buttonAlphabetical.style.display = plugin.alphabetical?.button?.display ?? true;
buttonAlphabetical.style.position = 'absolute';
buttonAlphabetical.style.top = (typeof plugin.alphabetical?.button?.topPosition !== 'undefined') ? `${plugin.alphabetical.button.topPosition}px` : '10px';
buttonAlphabetical.style.right = (typeof plugin.alphabetical?.button?.rightPosition !== 'undefined') ? `${plugin.alphabetical.button.rightPosition}px` : '115px';
container.appendChild(buttonAlphabetical);
// Add a click event listener to the button
buttonAsc.addEventListener('click', () => {
sortChartData(chart, (a, b) => a.data - b.data);
});
// Add a click event listener to the button
buttonDesc.addEventListener('click', () => {
sortChartData(chart, (a, b) => b.data - a.data);
});
// Add a click event listener to the reset button
buttonReset.addEventListener('click', () => {
resetChartData(chart);
});
// Add a click event listener to the button
buttonAlphabetical.addEventListener('click', () => {
sortChartAlphabetical(chart, (a, b) => a.data - b.data);
});
// Save a copy of the original data for resetting later
chart.originalData = {
datasets: [{ data: [...chart.data.datasets[0].data], borderColor: [...chart.data.datasets[0].borderColor], backgroundColor: [...chart.data.datasets[0].backgroundColor] }],
labels: [...chart.data.labels]
};
}
const sortChartData = (chart, sortFunc) => {
// Get the data, labels, and colors from the chart
const chartData = chart.data.datasets[0].data;
const chartLabels = chart.data.labels;
const chartBorders = chart.data.datasets[0].borderColor;
const chartColors = chart.data.datasets[0].backgroundColor;
// Combine the data, labels, and colors into an array of objects
const chartDataArray = chartData.map((dataPoint, index) => ({
data: dataPoint,
label: chartLabels[index],
border: chartBorders[index],
color: chartColors[index]
}));
// Sort the array of objects using the provided sort function
chartDataArray.sort(sortFunc);
// Separate the sorted data, labels, and colors back into their respective arrays
const sortedChartData = chartDataArray.map(dataObj => dataObj.data);
const sortedChartLabels = chartDataArray.map(dataObj => dataObj.label);
const sortedChartBorders = chartDataArray.map(dataObj => dataObj.border);
const sortedChartColors = chartDataArray.map(dataObj => dataObj.color);
// Update the chart with the sorted data, labels, and colors
chart.data.datasets[0].data = sortedChartData;
chart.data.labels = sortedChartLabels;
chart.data.datasets[0].backgroundColor = sortedChartColors;
chart.data.datasets[0].borderColor = sortedChartBorders;
chart.update();
chart.resize();
}
const resetChartData = (chart) => {
// Get the original data, labels, and colors from the chart
const originalChartData = chart.originalData.datasets[0].data;
const originalChartLabels = chart.originalData.labels;
const originalChartBorders = chart.originalData.datasets[0].borderColor;
const originalChartColors = chart.originalData.datasets[0].backgroundColor;
// Update the chart with the original data, labels, and colors
chart.data.datasets[0].data = originalChartData;
chart.data.labels = originalChartLabels;
chart.data.datasets[0].backgroundColor = originalChartColors;
chart.data.datasets[0].borderColor = originalChartBorders;
chart.update();
chart.resize();
};
const sortChartAlphabetical = (chart) => {
// Get the data, labels, and colors from the chart
const chartData = chart.data.datasets[0].data;
const chartLabels = chart.data.labels;
const chartBorders = chart.data.datasets[0].borderColor;
const chartColors = chart.data.datasets[0].backgroundColor;
// Combine the data, labels, and colors into an array of objects
const chartDataArray = chartData.map((dataPoint, index) => ({
data: dataPoint,
label: chartLabels[index],
border: chartBorders[index],
color: chartColors[index]
}));
// Sort the array of objects by label in alphabetical order
chartDataArray.sort((a, b) => a.label.localeCompare(b.label));
// Separate the sorted data, labels, and colors back into their respective arrays
const sortedChartData = chartDataArray.map(dataObj => dataObj.data);
const sortedChartLabels = chartDataArray.map(dataObj => dataObj.label);
const sortedChartBorders = chartDataArray.map(dataObj => dataObj.border);
const sortedChartColors = chartDataArray.map(dataObj => dataObj.color);
// Update the chart with the sorted data, labels, and colors
chart.data.datasets[0].data = sortedChartData;
chart.data.labels = sortedChartLabels;
chart.data.datasets[0].backgroundColor = sortedChartColors;
chart.data.datasets[0].borderColor = sortedChartBorders;
chart.update();
chart.resize();
};
})
};