Repository navigation
Expand file tree
/
Copy pathPieChart.js
More file actions
126 lines (104 loc) · 4.01 KB
/
Copy pathPieChart.js
File metadata and controls
126 lines (104 loc) · 4.01 KB
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
/// <reference path="JsChart-vsdoc.js" />
var Charting = MindFusion.Charting;
var Controls = Charting.Controls;
var Collections = MindFusion.Common.Collections;
var Drawing = MindFusion.Drawing;
// 1. Dynamic Data Generation
var currentYear = new Date().getFullYear();
var years = [];
for (var i = 0; i < 4; i++) {
years.push((currentYear - i).toString());
}
// Map the generated years to the UI labels
years.forEach(function (year, index) {
var labelEl = document.getElementById('label' + index);
if (labelEl) {
// Keep the checkbox input but update the text node
var checkbox = labelEl.querySelector('input');
labelEl.innerHTML = '';
labelEl.appendChild(checkbox);
labelEl.appendChild(document.createTextNode(' ' + year));
}
});
// Create randomized but logical values for market share
var baseValues = [40, 30, 20, 10];
var values = new Collections.List();
baseValues.forEach(v => values.add(v));
// 2. Create the chart
var pieChartEl = document.getElementById('pieChart');
pieChartEl.width = pieChartEl.offsetParent.clientWidth;
pieChartEl.height = pieChartEl.offsetParent.clientHeight;
var pieChart = new Controls.PieChart(pieChartEl);
pieChart.startAngle = 45;
pieChart.showLegend = false;
pieChart.chartPadding = 10;
// Set the series with dynamic year labels
pieChart.series = new Charting.PieSeries(
values,
new Collections.List(["40%", "30%", "20%", "10%"]),
new Collections.List(years));
// 3. Modern Theme Styling
var brushes = new Collections.List([
new Drawing.Brush("#0EA5E9"), // Sky Blue
new Drawing.Brush("#6366F1"), // Indigo
new Drawing.Brush("#10B981"), // Emerald
new Drawing.Brush("#64748B") // Slate
]);
var seriesBrushes = new Collections.List();
seriesBrushes.add(brushes);
var strokes = new Collections.List([new Drawing.Brush("#ffffff")]);
var seriesStrokes = new Collections.List();
seriesStrokes.add(strokes);
pieChart.plot.seriesStyle = new Charting.PerElementSeriesStyle(seriesBrushes, seriesStrokes);
pieChart.theme.highlightStroke = new Drawing.Brush("#455a64");
pieChart.theme.dataLabelsFontSize = 14;
pieChart.theme.dataLabelsBrush = new Drawing.Brush("#000000");
pieChart.draw();
// 4. Handlers
document.getElementById('chartPadding').addEventListener("input", function () {
pieChart.chartPadding = parseFloat(this.value);
pieChart.draw();
});
var slice0 = document.getElementById('slice0');
slice0.checked = pieChart.detachedSlices.contains(+slice0.value);
slice0.addEventListener("change", function () { sliceChanged(slice0); });
var slice1 = document.getElementById('slice1');
slice1.checked = pieChart.detachedSlices.contains(+slice1.value);
slice1.addEventListener("change", function () { sliceChanged(slice1); });
var slice2 = document.getElementById('slice2');
slice2.checked = pieChart.detachedSlices.contains(+slice2.value);
slice2.addEventListener("change", function () { sliceChanged(slice2); });
var slice3 = document.getElementById('slice3');
slice3.checked = pieChart.detachedSlices.contains(+slice3.value);
slice3.addEventListener("change", function () { sliceChanged(slice3); });
function sliceChanged(control) {
var slices = pieChart.detachedSlices;
if (control.checked) {
if (!slices.contains(+control.value))
slices.add(+control.value);
}
if (!control.checked) {
slices.remove(+control.value);
}
pieChart.detachedSlices = slices;
pieChart.draw();
}
[0, 1, 2, 3].forEach(id => {
document.getElementById('slice' + id).addEventListener("change", () => sliceChanged(id));
});
document.getElementById('startAngle').addEventListener("input", function () {
pieChart.startAngle = parseFloat(this.value);
pieChart.draw();
});
document.getElementById('doughnut').addEventListener("change", function () {
pieChart.doughnut = this.checked;
pieChart.draw();
});
document.getElementById('allowRotate').addEventListener("change", function () {
pieChart.allowRotate = this.checked;
pieChart.draw();
});
document.getElementById('showDataLabels').addEventListener("change", function () {
pieChart.showDataLabels = this.checked ? Charting.LabelKinds.All : Charting.LabelKinds.None;
pieChart.draw();
});