HTML
xxxxxxxxxx
1
<div class="container pt-4">
2
<section class="row mb-4">
3
<div class="col-12">
4
<div class="card">
5
<div class="card-body">
6
<div class="title-box mb-3">
7
<h2 class="title">
8
Marketing sources
9
</h2>
10
</div>
11
<div class="chartBox my-4" style="height:380px;">
12
<canvas id="chart-marketing-sources"></canvas>
13
</div>
14
</div>
15
</div>
16
</div>
17
</section>
18
</div>
CSS
1
1
JS
xxxxxxxxxx
1
const chartMarketingSourcesOption = {
2
dataLabelsPlugin: true,
3
options: {
4
maintainAspectRatio: false,
5
legend: {
6
position: "right",
7
labels: {
8
boxWidth: 10,
9
},
10
},
11
plugins: {
12
datalabels: {
13
formatter: (value, ctx) => {
14
let sum = 0;
15
let dataArr = dataChartMarketingSources.data.datasets[0].data;
16
dataArr.map((data) => {
17
sum += data;
18
});
19
let percentage = ((value * 100) / sum).toFixed(2) + "%";
20
return percentage;
21
},
22
color: "white",
23
labels: {
24
title: {
25
font: {
26
size: "13",
27
},
28
},
29
},
30
},
31
},
32
},
33
};
34
35
// Chart devices 1
36
const dataChartMarketingSources = {
37
type: "doughnut",
38
data: {
39
labels: ["Organic search", "Direct", "Social"],
40
datasets: [
41
{
42
label: "Traffic",
43
data: [8112, 5343, 3545],
44
backgroundColor: [
45
"rgba(63, 81, 181, 0.5)",
46
"rgba(77, 182, 172, 0.5)",
47
"rgba(66, 133, 244, 0.5)",
48
],
49
},
50
],
51
},
52
};
53
54
const dataChartMarketingSourcesUpdate = {
55
type: "doughnut",
56
data: {
57
labels: ["Organic search", "Direct", "Social"],
58
datasets: [
59
{
60
label: "Traffic",
61
data: [1000, 2000, 3000],
62
backgroundColor: [
63
"rgba(63, 81, 181, 0.5)",
64
"rgba(77, 182, 172, 0.5)",
65
"rgba(66, 133, 244, 0.5)",
66
],
67
},
68
],
69
},
70
};
71
72
var myChart;
73
function initializeChart() {
74
myChart = new mdb.Chart(
75
document.getElementById("chart-marketing-sources"),
76
dataChartMarketingSources,
77
chartMarketingSourcesOption
78
);
79
}
80
81
initializeChart();
82
setTimeout(() => { console.log("Waiting"); }, 5000);
83
updateChart();
84
85
function updateChart() {
86
myChart.update(dataChartMarketingSourcesUpdate);
87
}
Console errors: 0