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
|
function loadChart(jsonValues) {
let
months = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aôut", "Séptembre", "Octobre", "Novembre", "Décembre"],
objMois = jsonValues.mois.replace(/'/g, "").split(","),
objNbre = jsonValues.nbre.split(','),
years = [...new Set(objMois.map(v => v.split('.')[1]))],
series = [],
data = [];
years.forEach((y, i) => {
data[y] = [];
months.forEach((m, i1) => {
objMois.forEach((v, i2) => {
let mois = v.substr(0, v.indexOf('.'));
let annee = v.substr(v.indexOf(".") + 1, v.length - 1);
if (!data[y][i1])
data[y][i1] = mois == i1 + 1 &&
annee == y ? objNbre[i2] : 0;
});
});
if (series.filter(val => val.name == y).length == 0)
series.push({
type: 'bar',
name: y,
data: data[y]
});
});
console.log(series)
var myChart = echarts.init(document.getElementById('main'));
// Specify the configuration items and data for the chart
option = {
legend: {},
tooltip: {},
xAxis: {
type: "category",
data: months,
axisLabel: {
rotate: 30
}
},
yAxis: {},
series: series
};
// Display the chart using the configuration items and data just specified.
myChart.setOption(option);
}
const objJSON = {
"mois": "'4.2018','5.2018','6.2018','10.2018','11.2018','12.2018','1.2019','2.2019','3.2019','4.2019','5.2019','6.2019','7.2019','8.2019','9.2019','10.2019','11.2019','12.2019','1.2020','2.2020','3.2020','4.2020','5.2020','6.2020','7.2020','8.2020','9.2020','10.2020','11.2020','12.2020','1.2021','2.2021','3.2021','4.2021','5.2021','6.2021','7.2021','8.2021','11.2021'",
"nbre": "1290,1818,1821,1825,1834,1911,1982,2071,2211,2265,2355,2417,2446,2491,2547,2599,2715,2766,3040,3155,3337,3365,3507,3549,3596,3636,3671,3798,3914,3949,4015,4123,4203,4321,4412,4494,4618,4691,4692",
"total": 4692
};
var myChart = echarts.init(document.getElementById('main'));
window.onresize = function() {
myChart.resize();
};
/* Exécuter la fonction */
loadChart(objJSON); |
Partager