Javascript examples for Chart.js:Chart Configuration
Updating a chart from chart.js
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/vue/2.2.1/vue.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js"></script> <style id="compiled-css" type="text/css"> div {// www . ja va 2s . c o m width: 400px; height: 400px; } </style> </head> <body> <div id="app"> <chart></chart> </div> <script type="text/javascript"> const chartComponent = { data() { return { getAnalyticList: [{ chartNames: [ 'Red', 'Yellow', 'Blue' ], chartData: [10, 20, 30] }, { chartNames: [ 'Red', 'Yellow', 'Blue' ], chartData: [40, 10, 20] } ], rowIndex: 0 } }, template: ` <div> <canvas id="myChart" width="200px" height="200px"></canvas> <button @click="incRow">next</button> {{rowIndex}} </div> `, methods: { startChart: function() { var ctx = document.getElementById("myChart"); var myDoughnut = this.myChart = new Chart(ctx, { type: 'doughnut', data: { labels: this.getAnalyticList[this.rowIndex].chartNames, datasets: [{ data: this.getAnalyticList[this.rowIndex].chartData, backgroundColor: [ 'rgba(255, 99, 132, 0.6)', 'rgba(54, 162, 235, 0.6)', 'rgba(255, 206, 86, 0.6)', 'rgba(75, 192, 192, 0.6)', 'rgba(153, 102, 255, 0.6)', 'red', 'yellow', 'blue', 'green', 'orange', 'teal', 'violet', 'pink', 'purple' ] }] }, options: { responsive: true, defaultFontColor: '#ffffff', } }) }, incRow () { if (this.rowIndex < this.getAnalyticList.length - 1) { this.rowIndex++; } else { this.rowIndex = 0; } this.changeData() }, changeData () { this.myChart.data.datasets[0].data = this.getAnalyticList[this.rowIndex].chartData; this.myChart.data.labels = this.getAnalyticList[this.rowIndex].chartNames; this.myChart.update(); console.log('changed', this.myChart.data) } }, mounted() { console.log('started') this.startChart(); } } new Vue({ el: '#app', components: { chart: chartComponent } }) </script> </body> </html>