今天就跟大家聊聊有關(guān)chart.js怎么在vue項(xiàng)目中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Vue是一套用于構(gòu)建用戶(hù)界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫(kù)只關(guān)注視圖層,方便與第三方庫(kù)和項(xiàng)目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫(kù)開(kāi)發(fā)復(fù)雜的單頁(yè)應(yīng)用。
指令
該指令的作用是dom渲染后觸發(fā),因?yàn)榉莢ue的插件有的是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對(duì)象生成時(shí)觸發(fā)
this.initChart()
},
data: {
handler: function () { // 數(shù)據(jù)變化時(shí)觸發(fā)
this.updateChart()
},
deep: true
}
},
destoryed:function (){
if(this.cahrt){
this.cahrt.destroy()
}
},
computed: {
currentOptions: function (){
var options = {}
if(this.options){ // 加載自定義配置參數(shù)
for(var i in this.options){
options[i] = this.options[i]
}
}
return options
}
},
methods: {
setCanvas: function(el){ // dom生成時(shí)觸發(fā)
this.canvas = el
},
initChart: function () { // 更新chart結(jié)果
if(this.data && this.currentOptions){ // 保證參數(shù)的存在
this.chart = new Chart(this.canvas.getContext('2d'),{
type: this.type,
data: this.data,
options: this.currentOptions
})
}
},
updateChart: function () { // 更新chart結(jié)果
this.chart.data = JSON.parse(JSON.stringify(this.data))
this.chart.update()
}
}
}
</script>用法
<chartjs :options="options" type="pie" :data="data"></chartjs>
看完上述內(nèi)容,你們對(duì)chart.js怎么在vue項(xiàng)目中使用有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
本文題目:chart.js怎么在vue項(xiàng)目中使用-創(chuàng)新互聯(lián)
當(dāng)前地址:http://www.js-pz168.com/article40/jjceo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶(hù)體驗(yàn)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)公司、建站公司、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容