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
|
<script>
'use strict';
console.log(labels, count);
function showGraph(id, type, labels, label, data, text) {
return new Chart($('#' + id), {
type: type,
data: {
labels: labels,
datasets: [{
label: label,
backgroundColor: ["#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9", "#c45850"],
data: data
}]
},
options: {
legend: {
display: false
},
title: {
display: true,
text: text
}
}
});
}
/**
* @param {string} idChart - L'ID du graphe à afficher.
* @param {string} idSelect - L'ID de <select></select> associé au graphe - .
* @param {array} labels - Les données récupérées en format JSON.
* @param {int} count - Le Nbr totale de chaque donnée récupérée $count.
* @param {string} text - Étiquette .
* @param {string} defaultChart -Le type de premier graphe affichéé.
*/
function displayChart(idChart, idSelect, labels, count, label, text, defaultChart = "line") {
var myChart = showGraph(idChart, defaultChart, labels, label, count, text);
$("#" + idSelect).change(function() {
myChart.destroy(); //détruire l'ancien Chart
let dataChart = $(this)
.find('option:selected').data('class');
console.log(dataChart);
myChart = showGraph(idChart, dataChart, labels, label, count, text);
});
}
//Chart 1
var labels = <?= $label ?>,
count = <?= $count ?>;
$(function() {
displayChart("chart", "select1", labels, count, "NBR PI", "Etat des lieux des titres de propriétés intellectuelles")
});
//Chart 2
var labels2 = <?php echo json_encode([1, 2, 3, 4, 5, 6]); ?>,
count2 = <?php echo json_encode([10, 20, 30, 40, 50]); ?>;
$(function() {
displayChart("chart2", "select2", labels2, count2, "Label 2", "example de titre", "pie")
});
</script> |
Partager