我正在尝试使用 vuechartjs 生成一个 vue 图表,该图表将在一段时间(年)内重复编号 10、20、30、40、50。
我很难让图例和工具提示按预期工作。目前,标签只打印出标签数组,工具提示显示各自的标签。
预期的标签图例:
Label 1, Label 2, Label 3, Label 4, Label 5
用各自的颜色:#F44336, #FC9900, #FCC106, #8BC34A, #4CAF50
。
- 如何获得带有此标签和颜色的图例?
- 如何获得显示
label
数据点而不是每个数据点的工具提示labels
? - 如何将图像添加到工具提示/标签?
Vue.component('reactive', {
extends: VueChartJs.Bar,
mixins: [VueChartJs.mixins.reactiveProp],
data: function () {
return {
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
},
gridLines: {
display: true
}
}],
xAxes: [{
ticks: {
beginAtZero: true
},
gridLines: {
display: false
}
}]
},
legend: {
display: true
},
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItems, data) {
return '$' + tooltipItems.yLabel;
}
}
},
responsive: true,
maintainAspectRatio: false,
height: 200
}
}
},
mounted () {
// this.chartData is created in the mixin
this.renderChart(this.chartData, this.options)
}
})
var vm = new Vue({
el: '.app',
data () {
return {
datacollection: null
}
},
created () {
this.fillData()
},
methods: {
fillData () {
this.datacollection = {
labels: ['January 2010', 'February 2010', 'March', 'April', 'May 2011', 'June 2018', 'July', 'August 2011', 'August', 'October', 'November', 'December 2018'],
datasets: [
{
label: ['Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5'],
backgroundColor: this.getBackground(),
data: [this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt()]
}
]
}
},
getRandomInt () {
var myArray = [10,20,30,40,50]
return myArray[Math.floor(Math.random()*myArray.length)];
},
getBackground() {
return ["#8BC34A", "#4CAF50", "#4CAF50", "#4CAF50", "#4CAF50", "#4CAF50", "#F44336", "#FC9900", "#FCC106", "#8BC34A", "#8BC34A", "#4CAF50"]
},
}
})
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<script src="https://unpkg.com/vue-chartjs@3.4.0/dist/vue-chartjs.js"></script>
<div class="app">
<h1>Bar Chart</h1>
<reactive :chart-data="datacollection"></reactive>
<button class="button is-primary" @click="fillData()">Randomize</button>
</div>