1. 程式人生 > >echarts 在 vue 隨瀏覽器視窗改變響應

echarts 在 vue 隨瀏覽器視窗改變響應

// 引入 echarts 主模組。 import * as echarts from 'echarts/lib/echarts' // 引入折線圖。 import 'echarts/lib/chart/pie' import 'echarts/lib/chart/bar' // 引入提示框元件、標題元件、工具箱元件。 import 'echarts/lib/component/title' import 'echarts/lib/component/tooltip' import 'echarts/lib/component/legend' import home from '@/api/home' var clearId export default
{ data() { return { countNum: 0, options_person: { tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' }, legend: { orient: 'vertical', x: 'right', bottom: 'center', itemWidth: 20, itemHeight: 20
, data: ['教職工', '學生', '訪客', '社會人員', '其他'] }, series: [ { name: '人員佔比', type: 'pie', radius: ['60%', '80%'], center: ["40%", "50%"], avoidLabelOverlap: false, label: { normal: { show: false
, position: 'center' }, emphasis: { show: true, textStyle: { fontSize: '22', fontWeight: 'bold' } } }, labelLine: { normal: { show: false } }, data: [ { value: 0, name: '教職工', itemStyle: { color: '#728FFC' } }, { value: 0, name: '學生', itemStyle: { color: '#FFB22D' } }, { value: 0, name: '訪客', itemStyle: { color: '#3EB934' } }, { value: 0, name: '社會人員', itemStyle: { color: '#FF6EEC' } }, { value: 0, name: '其他', itemStyle: { color: '#FF6E9B' } } ] } ] }, options_statistical: { title: { text: '進出人數', textStyle: { color: '#000', fontWeight: '400', fontSize: 14 } }, tooltip: { //提示框元件 trigger: 'axis', formatter: '{b}<br/>{a0}: {c0}<br/>', axisPointer: { type: 'shadow' }, textStyle: { color: '#fff', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12 } }, grid: { left: 0, right: 0, bottom: 0, top: 50, containLabel: true }, xAxis: [ { type: 'category', data: [], axisLabel: { //座標軸刻度標籤的相關設定。 // interval: 0,//設定為 1,表示『隔一個標籤顯示一個標籤』 // margin:15, textStyle: { color: '#000', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12 }, rotate: 65 }, axisTick: { //座標軸刻度相關設定。 show: false }, axisLine: { //座標軸軸線相關設定 lineStyle: { color: '#fff', opacity: 0.2 } }, splitLine: { //座標軸在 grid 區域中的分隔線。 show: false } } ], yAxis: [ { type: 'value', splitNumber: 5, axisLabel: { textStyle: { color: '#000', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12 } }, axisLine: { show: false }, axisTick: { show: false }, splitLine: { show: true, lineStyle: { color: ['#DCDCDC'], opacity: 1 } } } ], series: [ { name: '總數量', type: 'bar', data: [], barWidth: 10, barGap: 0, //柱間距離 label: { //圖形上的文字標籤 normal: { show: true, position: 'top', textStyle: { color: '#000', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12 } } }, itemStyle: { normal: { show: true, color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: '#FF6EEC' }, { offset: 1, color: '#728FFC' } ]), barBorderRadius: 50, borderWidth: 0 } } } ] }, homeBar: '', homePie: '' } }, mounted() { this.drawLines(); // 重點,能夠響應瀏覽器視窗改變的程式碼:原理主要是 // 監聽瀏覽器視窗變化,進而重新繪製圖表 window.addEventListener('resize', () => { this.homeBar.resize(); // 多個圖表,第一個圖表 this.homePie.resize(); // 多個圖表,第二個圖表 }); }, methods: { drawLines() { // 基於echarts,初始化 echarts 例項並繪製圖表 this.homePie = echarts.init(this.$refs._person_chart); this.homePie.setOption(this.options_person); this.homeBar = echarts.init(this.$refs._statistical_chart) this.homeBar.setOption(this.options_statistical) }, init() { home.homeReportBySchool().then((res) => { res.map((item, index) => { this.options_statistical.xAxis[0].data[index] = item.name this.options_statistical.series[0].data[index] = item.count }) this.drawLines() }) }, scaleByPerson() { this.countNum = 0; home.homeScaleByPerson().then((res) => { let new_res = res.filter((item) => { return item.name != '總數' }) let count_array = res.filter((item) => { return item.name == '總數' })[0] this.options_person.legend.data = new_res.map((i) => { return i.name }) new_res.map((item, index) => { this.options_person.series[0].data[index].name = item.name; this.options_person.series[0].data[index].value = item.count; }) this.countNum = (count_array && count_array.count) ? count_array.count : 0; this.drawLines() }) } }, created() { this.init() this.scaleByPerson() clearId = setInterval(() => { this.init() }, 5000) }, destroyed() { clearTimeout(clearId); } }