安装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 2026除夕节祝福语简短
9 祝福语朋友留言简短大全
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短