JavaScript
var chartType = "donut";
var axisSeries = [{
data: [44, 55, 57, 56, 61, 58, 63, 60, 66]
},
{
data: [76, 85, 101, 98, 87, 105, 91, 114, 94]
},
{
data: [35, 41, 36, 26, 45, 48, 52, 53, 41]
}];
function random() {
return Math.floor(Math.random() * (100 - 1 + 1)) + 1;
}
function randomizeDonut() {
return chart.w.globals.series.map(function() {
return random();
});
}
function getNewSeries() {
function getData(data) {
return data.map(function() {
return random();
});
}
return axisSeries.map(function(s) {
var data = getData(s.data);
return {
data: data
};
});
}
function getOptions(type) {
var series = axisSeries;
if (type === "donut") {
series = [34, 55, 44, 23, 19];
}
return {
chart: {
height: type === "donut" ? 300 : 380,
type: type === "column" ? "bar": type,
stacked: type === "line"
},
plotOptions: {
bar: {
horizontal: type === "column" ? false: true,
endingShape: "rounded",
columnWidth: "55%"
}
},
dataLabels: {
enabled: false
},
stroke: {
show: true,
width: type === "line" ? 4 : 0
},
series: series,
xaxis: {
categories: ["Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct"]
},
yaxis: {
title: {
text: "$ (thousands)"
}
},
fill: {
opacity: 1
},
tooltip: {
followCursor: false,
value: {
formatter: function formatter(val) {
return "$ " + val + " thousands";
}
}
}
};
}
var options = getOptions("donut");
chart = new ApexCharts(document.querySelector(".animated-chart"), options);
chart.render();
$("#animations .update").click(function() {
if (chartType === "donut") {
chart.updateSeries(randomizeDonut());
} else {
var newSeries = getNewSeries();
chart.updateSeries(newSeries);
}
});
$("#animations input[name=charttype]").click(function(animRadio) {
chartType = $(this).val();
options = getOptions(chartType);
chart.destroy();
chart = new ApexCharts(document.querySelector(".animated-chart"), options);
chart.render();
});