在vue中使用Echarts利用watch做動(dòng)態(tài)數(shù)據(jù)渲染操作
依舊直接上代碼~
首先安裝引入Echarts,我是直接把Echarts掛到VUE全局變量上了
//引入echartsimport Vue from ’vue’;import echarts from ’echarts’;Vue.prototype.$echarts = echarts;
<template> <div class='demo-container'> <div ref='chart_wrap' class='chart_wrap'></div> </div></template><script>export default { name: 'demo', computed: {}, data() { return { seriesData: [] }; }, created() {}, mounted() { this.initCharts(); setTimeout(() => { this.seriesData.push({ name: '銷量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }); }, 5000); }, watch: { seriesData(val, oldVal) { console.log(1111, val, oldVal); this.setOptions(val); } }, methods: { initCharts() { this.chart = this.$echarts.init(this.$refs.chart_wrap); this.setOptions(); }, setOptions(series) { console.log(22222,this.chart,series); this.chart.setOption({ title: { text: 'ECharts 入門示例' }, tooltip: {}, legend: { data: ['銷量'] }, xAxis: { data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子'] }, yAxis: {}, series: series }); } }};</script><style rel='stylesheet/scss' lang='scss' scoped>.chart_wrap { height: 400px;}</style>
補(bǔ)充知識(shí):echarts-循環(huán)生成圖
1、問(wèn)題背景
利用for循環(huán)生產(chǎn)多個(gè)氣泡圖,并且每個(gè)氣泡都可以點(diǎn)擊
2、實(shí)現(xiàn)源碼
<!DOCTYPE html><html><head><meta charset='UTF-8'><title>echarts-循環(huán)生成圖</title><link rel='shortcut icon' href='http://www.aoyou183.cn/js/echarts-2.2.7/doc/asset/ico/favicon.png'><script type='text/javascript' src='http://www.aoyou183.cn/js/echarts-2.2.7/doc/asset/js/jquery.min.js' ></script><script type='text/javascript' src='http://www.aoyou183.cn/js/echarts-2.2.7/doc/example/www2/js/echarts-all.js' ></script><style>body,html,#div-chart{width: 99%;height: 100%;font-family: '微軟雅黑';font-size: 12px;}.chart{width: 1200px;height: 100px;}</style><script>$(document).ready(function(){buildChart();buildChartJS();});function buildChart(){$('#div-chart').empty();var chart = '';for(var i=0;i<8;i++){chart += '<div id=’chart'+i+'’ class=’chart’></div>'; }$('#div-chart').append(chart);}function buildChartJS(){for(var i=0;i<8;i++){var chart = document.getElementById(’chart’+i); var echart = echarts.init(chart); var option = { legend: { data:[’scatter1’], show:false }, splitLine:{ show:false }, grid:{ borderWidth:0 }, xAxis : [ { show:false, type : ’value’, splitNumber: 2, scale: true, axisLine:{ show:false }, splitLine:{ show:false }, axisTick:{ show:false } } ], yAxis : [ { show:false, type : ’value’, splitNumber: 2, scale: true, axisLine:{ show:false }, splitLine:{ show:false } } ], series : [ { name:’scatter1’, type:’scatter’, symbol: ’emptyCircle’, symbolSize: 20, itemStyle : { normal: { color:’#0068B7’, label:{ show: true, position: ’inside’, textStyle : { fontSize : 26, fontFamily : ’微軟雅黑’, color:’#0068B7’ } } } }, data: randomDataArray() } ]}; function eConsole(param) { alert(param.value); console.dir(param); } echart.on('click', eConsole);echart.setOption(option); }}function randomDataArray() { var d = []; var arr = [3,5,7,9,10,1,2,4,8,6]; var len = 10; for(var i=0;i<len;i++) { d.push([i+1,0,arr[i],]); } return d;}</script></head><body><div id='div-chart'></div></body></html>
3、實(shí)現(xiàn)結(jié)果
以上這篇在vue中使用Echarts利用watch做動(dòng)態(tài)數(shù)據(jù)渲染操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算2. JavaScript中的for循環(huán)與雙重for循環(huán)詳解3. vue項(xiàng)目登錄成功拿到令牌跳轉(zhuǎn)失敗401無(wú)登錄信息的解決4. 詳解PHP結(jié)構(gòu)型設(shè)計(jì)模式之橋接模式Bridge Pattern5. JSP出現(xiàn)中文亂碼問(wèn)題解決方法詳解6. 詳解如何使用Net將HTML簡(jiǎn)歷導(dǎo)出為PDF格式7. 如何將asp.net core程序部署到Linux服務(wù)器8. 表單中Readonly和Disabled的區(qū)別詳解9. 通用 HTTP 簽名組件的另類實(shí)現(xiàn)方式10. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)
