As it says on a title, I need to increase data in a chart by one by click of a button. There are three bars/datasets, each with own button, plus a reset button. I've tried using the example in chart.js documentation, but don't know where to go from here.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div style="width: 500px;">
<h2 style="text-align: center;">Autojen lukumäärä</h2>
<button onclick="addCar">Ford</button>
<button onclick="addCar">Opel</button>
<button onclick="addCar">Toyota</button>
<button onclick="nollaa">Nollaa</button>
<canvas id="viivaTaulukko" height="300" width="500"></canvas>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3/dist/Chart.min.js"></script>
<script src="autolaskuri.js"></script>
<p id="Ford"></p>
<p id="Opel"></p>
<p id="Toyota"></p>
<p id="Nollaa"></p>
<script>
function addCar(){
barChart.data.datasets[0].data[2] += 1;
barChart.update();
}
function nollaa(){
barChart.reset();
}
</script>
</body>
</html>
Javascript:
const CHART = document.getElementById("viivaTaulukko");
let barChart = new Chart(CHART, {
type: 'bar',
data: {
labels: ["Ford", "Opel", "Toyota"],
datasets: [
{
label: "Autojen lukumäärät",
data: [0,0,0],
backgroundColor: ["#0EBFE9", "#8E388E", "#70DBDB"]
}
]
},
options:{
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});