これはjsonレスポンスの外観です。[[61,57,34],[1,1,3]]
ラベルの最初の配列とデータの2番目の配列を使いたいとします。データをフェッチした後にグラフを描画しないvueを持つCharts.js
labels and data
をapp
の中に手動で設定すると、それはうまく動作します。ページの
例えば labels: ["q", "w", "e"]
data: [1, 5, 10]
Vue.component('chart', {
props: ['labels', 'data', 'type'],
template: `
<canvas style="width: 512px; height: 256px"></canvas>
`,
mounted: function() {
new Chart(this.$el, {
type: this.type,
data: {
labels: this.labels,
datasets: [{
label: '# of Votes',
data: this.data,
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
})
}
});
new Vue({
el: '#app',
data: {
message: "test",
labels: [],
data: []
},
methods: {
fetchData: function() {
this.$http.get('/admin/fetch_data').then(res => {
this.labels = res.body[0];
this.data = res.body[1];
})
}
},
beforeMount() {
this.fetchData()
}
});
コンポーネント
<div id="app">
{{message}}
<chart :labels="labels" :data="data" type="bar"></chart>
</div>
データがロードされているように見えるが、ページには、チャートのバーがそこにいるん。