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
|
<script>
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", "#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9"],
data: data
}]
},
options: {
legend: {
display: false
},
title: {
display: true,
text: text
}
}
});
}
$(function() {
/* var labels1 = <?= $label ?>,
count1 = <?= $count ?>;*/
var labels1 = <?php echo json_encode([1, 2, 3, 4, 5, 6]); ?>,
count1 = <?php echo json_encode([25, 35, 40, 10, 70,5]); ?>;
var labels2 = <?php echo json_encode([1, 2, 3, 4, 5, 6]); ?>,
count2 = <?php echo json_encode([10, 20, 30, 40, 50,60]); ?>;
var dataOfChart1 = [labels1, "label1", count1, "Graphe_Title_1"];
var dataOfChart2 = [labels2, "label2", count2, "Graphe_Title_2"];
var AllDataOfCharts = [dataOfChart1, dataOfChart2];
var allCharts = [];
//au chargement de la page, on affiche le Chart dans #chart et #chart2
$("#chart-1,#chart-2").each(function(index) {
let defaulChart = $(this).prev('select').find('option:selected').attr("data-class"); //le premier graphe à afficher
allCharts[index] = showGraph($(this).attr('id'), defaulChart, AllDataOfCharts[index][0], AllDataOfCharts[index][1], AllDataOfCharts[index][2], AllDataOfCharts[index][3]);
});
$('select.select-graphe').change(function() {
let index = $(this).index('.select-graphe');
let dataChart = $(this).find('option:selected').attr("data-class");
if (allCharts[index]) allCharts[index].destroy();
console.log(dataChart);
allCharts[$(this).index('.select-graphe')] = showGraph($(this).next('canvas').attr('id'), dataChart, AllDataOfCharts[index][0], AllDataOfCharts[index][1], AllDataOfCharts[index][2], AllDataOfCharts[index][3]);
});
});
</script> |