vue集成chart.js的實現方法
更新時間:2019年08月20日 10:20:34 作者:改改心情
這篇文章主要介紹了vue集成chartjs的實現方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
指令
該指令的作用是dom渲染后觸發(fā),因為非vue的插件有的是dom必須存在的情況下才可以執(zhí)行
Vue.directive('loaded-callback', {
inserted: function (el, binding, vnode) {
binding.value(el, binding, vnode)
}
})
安裝chartjs
npm install chart.js --save
chartjs 組件
<template>
<canvas refs="chartcanvas" v-loaded-callback="setCanvas"></canvas>
</template>
<script type="text/javascript">
require('chart.js')
export default{
name: 'components-base-chartjs',
props: {
'data': {},
'options': {},
'type': {}
},
data:function(){
return {
canvas: null,
chart: null
}
},
watch:{
canvas: function () { // chart對象生成時觸發(fā)
this.initChart()
},
data: {
handler: function () { // 數據變化時觸發(fā)
this.updateChart()
},
deep: true
}
},
destoryed:function (){
if(this.cahrt){
this.cahrt.destroy()
}
},
computed: {
currentOptions: function (){
var options = {}
if(this.options){ // 加載自定義配置參數
for(var i in this.options){
options[i] = this.options[i]
}
}
return options
}
},
methods: {
setCanvas: function(el){ // dom生成時觸發(fā)
this.canvas = el
},
initChart: function () { // 更新chart結果
if(this.data && this.currentOptions){ // 保證參數的存在
this.chart = new Chart(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()
}
}
}
</script>
用法
<chartjs :options="options" type="pie" :data="data"></chartjs>
options 及數據結構
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 使用Chart.js圖表庫制作漂亮的響應式表單
- Chart.js 輕量級HTML5圖表繪制工具庫(知識整理)
- 詳解Chart.js輕量級圖表庫的使用經驗
- 在 Angular 中使用Chart.js 和 ng2-charts的示例代碼
- 使用Vue.js 和Chart.js制作絢麗多彩的圖表
- ichart.js繪制虛線、平均分虛線效果的實現代碼
- Chart.js在Laravel項目中的應用示例
- 利用ECharts.js畫K線圖的方法示例
- JavaScript Chart 插件整理
- 詳解vue文件中使用echarts.js的兩種方式
- vue.js+Echarts開發(fā)圖表放大縮小功能實例
- Chart.js功能與使用方法小結
相關文章
vscode+vue cli3.0創(chuàng)建項目配置Prettier+eslint方式
這篇文章主要介紹了vscode+vue cli3.0創(chuàng)建項目配置Prettier+eslint方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
vue-router之nuxt動態(tài)路由設置的兩種方法小結
今天小編就為大家分享一篇vue-router之nuxt動態(tài)路由設置的兩種方法小結,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
詳解Vue基于vue-quill-editor富文本編輯器使用心得
這篇文章主要介紹了Vue基于vue-quill-editor富文本編輯器使用心得,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

