How to Update Chart.js Data in Vue.js on Button Click

preview_player
Показать описание
---

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Update data when pressing submit in chartjs in vuejs

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---

The Problem

The Initial Setup

Here's what we started with:

[[See Video to Reveal this Text or Code Snippet]]

Report Component

[[See Video to Reveal this Text or Code Snippet]]

Your data method initializes data, options, and sets chart labels in the created lifecycle hook:

[[See Video to Reveal this Text or Code Snippet]]

The challenge is that the method that updates the labels (submitData) does not seem to reflect the changes on the chart.

The Solution

To successfully achieve the desired dynamic update on chart data when the button is clicked, we need to take an important step: re-render the chart when the data changes. Here’s how to do that:

Modifications

Update the <chartjs-component-line-chart> to utilize a v-if condition to control its rendering:

[[See Video to Reveal this Text or Code Snippet]]

Add a loaded Data Property: This boolean property will help manage when to render the chart:

[[See Video to Reveal this Text or Code Snippet]]

Update the created and submitData Methods:

[[See Video to Reveal this Text or Code Snippet]]

Final Notes

Conclusion

If you have any questions, don’t hesitate to ask in the comments below.
Рекомендации по теме
welcome to shbcf.ru