let shopping_result; let shopping_chart = new Chart(document.getElementById("shoping_times_chart"), { type: 'bar', data: { labels: [1, 2, 3, 4, 5], datasets: [{ label: 'Anzahl verkaufter Artikel', data: [0, 0, 0, 0, 0], backgroundColor: [ 'rgba(143, 170, 220, 0.9)', 'rgba(143, 170, 220, 0.9)', 'rgba(143, 170, 220, 0.9)', 'rgba(143, 170, 220, 0.9)', 'rgba(143, 170, 220, 0.9)', 'rgba(143, 170, 220, 0.9)', ], borderColor: [ 'rgba(143, 170, 220, 1)', 'rgba(143, 170, 220, 1)', 'rgba(143, 170, 220, 1)', 'rgba(143, 170, 220, 1)', 'rgba(143, 170, 220, 1)', 'rgba(143, 170, 220, 1)', ], borderWidth: 1 }] }, options: { responsive: true, title: { display: false, text: 'Einkaufszeiten' }, legend: { display: false }, scales: { yAxes: [{ categoryPercentage: 1.0, barPercentage: 0.5, ticks: { beginAtZero: true } }], xAxes: [{ gridLines: { display: false }, }] } } }); $.ajax({ url: 'shopping_times', dataType: 'json' }).done(function (results) { shopping_result = results; updateDays(); }); function updateDays() { let e = document.getElementById("day"); let value = e.options[e.selectedIndex].text; shopping_chart.data.labels = shopping_result.labels; shopping_chart.data.datasets[0].data = shopping_result[value]; let color = shopping_result[value].map(x => 'rgba(143, 170, 220, 0.9)'); console.log("max"); argMax(shopping_result[value]).slice().forEach(function (item) { console.log(item); color[item] = 'green'; }); /*console.log("min"); argMin(shopping_result[value]).forEach(function(item) { console.log(item); color[item] = 'red'; });*/ shopping_chart.data.datasets[0].backgroundColor = color; shopping_chart.update(); } function argMax(array) { let result = []; console.log(Math.max.apply(null, array)); array.forEach(function(item) { console.log(item); if(item == Math.max.apply(null, array)) { console.log(true); result.push(array.indexOf(item)); } }); console.log(result); return result; } function argMin(array) { let arr = array.slice().sort(); console.log(arr[0]); return arr }