-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchart2.html
201 lines (179 loc) · 6.93 KB
/
chart2.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spline Area & Polar Area Charts with Legends Count</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/apexcharts.min.css">
</head>
<body>
<div class="container mt-5">
<h2 class="text-center">Spline Area & Polar Area Charts with Legends Count</h2>
<!-- Buttons for switching charts -->
<div class="text-center my-4">
<button class="btn btn-primary mx-2" id="btnAll">All</button>
<button class="btn btn-primary mx-2" id="btnMonthly">Monthly</button>
<button class="btn btn-primary mx-2" id="btnQuarterly">Quarterly</button>
<button class="btn btn-primary mx-2" id="btnYearly">Yearly</button>
</div>
<!-- Spline Area Chart Container -->
<div id="splineAreaChart" class="chart-container" style="height: 400px;"></div>
<!-- Polar Area Chart Container -->
<div id="polarAreaChart" class="chart-container" style="height: 400px;"></div>
<!-- Legends Count Section -->
<div class="mt-4">
<h4>Legend Data:</h4>
<div class="row">
<div id="legendDistributor" class="col-md-4 mb-3"></div>
<div id="legendDealer" class="col-md-4 mb-3"></div>
<div id="legendRetailer" class="col-md-4 mb-3"></div>
<div id="legendSelf" class="col-md-4 mb-3"></div>
<div id="legendSister" class="col-md-4 mb-3"></div>
<div id="legendExtra" class="col-md-4 mb-3"></div>
</div>
</div>
<!-- Toggle Button for 6th Legend in Polar Chart -->
<div class="text-center mt-3">
<button class="btn btn-secondary" id="toggle6thLegend">Toggle 6th Legend</button>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/apexcharts.min.js"></script>
<script>
// Data setup
const chartData = {
series: {
distributor: [44, 55, 41, 64, 22, 30],
dealer: [53, 32, 33, 52, 13, 40],
retailer: [24, 12, 34, 61, 11, 20],
self: [15, 18, 27, 35, 20, 10],
sister: [12, 9, 21, 8, 30, 15]
},
labels: ['Distributor', 'Dealer', 'Retailer', 'Self', 'Sister', 'Extra'],
categories: ['One Year', 'Two Years', 'Three Years', 'Four Years', 'Five Years', 'Six Years']
};
let show6thLegend = true;
// Spline Area Chart
let splineAreaChart = new ApexCharts(document.querySelector("#splineAreaChart"), {
chart: {
type: 'area',
height: 400,
width: 1100,
},
series: [{
name: 'Distributor',
data: chartData.series.distributor
}, {
name: 'Dealer',
data: chartData.series.dealer
}, {
name: 'Retailer',
data: chartData.series.retailer
}, {
name: 'Self',
data: chartData.series.self
}, {
name: 'Sister',
data: chartData.series.sister
}],
xaxis: {
categories: chartData.categories
},
yaxis: {
title: {
// text: 'Values'
}
},
legend: {
position: 'bottom',
}
});
// Polar Area Chart
let polarAreaChart = new ApexCharts(document.querySelector("#polarAreaChart"), {
chart: {
type: 'polarArea',
},
series: chartData.series.distributor,
labels: chartData.labels,
responsive: [{
breakpoint: 600,
options: {
chart: {
height: 300
}
}
}],
legend: {
show: true,
}
});
splineAreaChart.render();
polarAreaChart.render();
// Render legends count with individual IDs
function renderLegendsCount(series, labels) {
const legendIds = [
'legendDistributor',
'legendDealer',
'legendRetailer',
'legendSelf',
'legendSister',
'legendExtra'
];
legendIds.forEach((id, index) => {
const legendElement = $(`#${id}`);
if (index < series.length) {
const legendHtml = `
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">${labels[index]}</h5>
<p class="card-text">Value: ${series[index]}</p>
</div>
</div>`;
legendElement.html(legendHtml).show();
} else {
legendElement.hide(); // Hide the legend if not in the current series
}
});
}
// Initial render of legends count
renderLegendsCount(chartData.series.distributor, chartData.labels);
// Button click event to switch chart data
$('#btnAll').click(() => updateCharts(chartData.series.distributor, chartData.series.dealer, chartData.series.retailer, chartData.series.self, chartData.series.sister));
$('#btnMonthly').click(() => updateCharts([44, 55, 41, 64, 22, 30], [53, 32, 33, 52, 13, 40], [24, 12, 34, 61, 11, 20], [15, 18, 27, 35, 20, 10], [12, 9, 21, 8, 30, 15]));
$('#btnQuarterly').click(() => updateCharts([41, 52, 34, 61, 29, 25], [32, 41, 55, 64, 44, 35], [11, 21, 33, 53, 24, 30], [20, 30, 27, 35, 15, 22], [9, 12, 18, 22, 13, 28]));
$('#btnYearly').click(() => updateCharts([44, 55, 41, 64, 22, 30], [53, 32, 33, 52, 13, 40], [24, 12, 34, 61, 11, 20], [15, 18, 27, 35, 20, 10], [12, 9, 21, 8, 30, 15]));
// Toggle 6th legend visibility
$('#toggle6thLegend').click(function () {
show6thLegend = !show6thLegend;
const updatedSeries = show6thLegend ? chartData.series.distributor : chartData.series.distributor.slice(0, 5);
const updatedLabels = show6thLegend ? chartData.labels : chartData.labels.slice(0, 5);
polarAreaChart.updateOptions({
series: updatedSeries,
labels: updatedLabels,
});
renderLegendsCount(updatedSeries, updatedLabels); // Update legends count
});
function updateCharts(dDistributor, dDealer, dRetailer, dSelf, dSister) {
splineAreaChart.updateSeries([{
name: 'Distributor',
data: dDistributor
}, {
name: 'Dealer',
data: dDealer
}, {
name: 'Retailer',
data: dRetailer
}, {
name: 'Self',
data: dSelf
}, {
name: 'Sister',
data: dSister
}]);
polarAreaChart.updateSeries(dDistributor);
renderLegendsCount(dDistributor, chartData.labels); // Update legends count
}
</script>
</body>
</html>