安装chartjs

npminstallchart.js--save

chartjs组件



require('chart.js')
exportdefault{
name:'components-base-chartjs',
props:{
'data':{},
'options':{},
'type':{}
},
data:function(){
return{
canvas:null,
chart:null
}
},
watch:{
canvas:function(){//chart对象生成时触发
this.initChart()
},
data:{
handler:function(){//数据变化时触发
this.updateChart()
},
deep:true
}
},
destoryed:function(){
if(this.cahrt){
this.cahrt.destroy()
}
},
computed:{
currentOptions:function(){
varoptions={}
if(this.options){//加载自定义配置参数
for(variinthis.options){
options[i]=this.options[i]
}
}
returnoptions
}
},
methods:{
setCanvas:function(el){//dom生成时触发
this.canvas=el
},
initChart:function(){//更新chart结果
if(this.data&&this.currentOptions){//保证参数的存在
this.chart=newChart(this.canvas.getContext('2d'),{
type:this.type,
data:this.data,
options:this.currentOptions
})
}
},
updateChart:function(){//更新chart结果
this.chart.data=JSON.parse(JSON.stringify(this.data))
this.chart.update()
}
}
}

用法


options及数据结构

请跳转

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。

热门推荐

1 长辈请客祝福语简短精辟
2 对转校同学祝福语简短
3 祝贺员工结婚祝福语简短
4 祝贺母校祝福语简短英语
5 妈妈病人出院祝福语简短
6 国庆节日简短祝福语
7 别人的新年祝福语简短
8 准妈妈花束祝福语简短
9 留学生祝福语简短
10 新年顾客简短祝福语大全
11 给表妹生日祝福语简短
12 狗年同事拜年祝福语简短
13 交接同事退休祝福语简短
14 新年孕妈祝福语简短
15 简短的祝福语中秋搞笑
16 朋友生闺女简短祝福语
17 牛年祝福语简短搞笑的
18 爸妈金婚文案祝福语简短