Skip to content
Advertisement

Chartsjs update datasets with dropdown

Ok, I got a chart.js on my website. Now I try to change between different datasets with a dropdown menu. I got an example of a canvas.js chart and tried to change it for my needs. But I struggle to do it because I don’t understand how to do it with charts. Could someone show me how to do it correctly?

Here is what I already got:

Dropdown:

JavaScript

Javascript:

JavaScript

Note: <?PHP echo json_encode($json1); ?> is currently used to set the data on the x-axis. Those are dates. Later I plan to build the jsonData x and y values dynamically with values from the database but for now, I would just be happy to get the dropdown working with those static values.

Here’s the canvasjs example: canvasjs

Advertisement

Answer

Use this good Tutorial: chart.js Tutorial (And change your code to the chart.js way)

the only thing when a change event is fired via dropdown change, you must call

chart.update()

to make your changes into the chart object to be rendered into the HTML.

Advertisement