<template> <div ref="chart" id="Chart" style="width:100%;height:400px;"></div> </template> <script> export default { data() { return {} }, props: ['title', 'classList', 'data','dataZoom'], methods: { chart() { // 基於准備好的dom,初始化echarts實例 let myChart = this.$echarts.init(document.getElementById('Chart')) // 繪制圖表 myChart.setOption({ color: ['#3398DB','red'], title: { x: 'left', text: this.title }, legend: { left: 'right', top: 'top' }, tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } }, grid: { left: '3%', right: '4%', bottom: '12%', containLabel: true }, xAxis: [ { type: 'category', data: this.classList, axisTick: { alignWithLabel: true }, axisLabel: { //設置x軸的字 show: true, interval: 0, //使x軸橫坐標全部顯示 rotate: 50, //傾斜度 -90 至 90 默認為0 formatter: function(value, index) { if (value.length > 8) { return value.substr(0, 5) + '...' } else { return value } } } } ], yAxis: [ { type: 'value', name: '人數', boundaryGap: [0, '100%'] } ], series: [ { name: this.data, type: 'line', barWidth: '60%', data: this.data }, ], dataZoom:this.dataZoom?this.dataZoom:[] }) window.onresize = function() { myChart.resize() } } }, created() {}, mounted() { this.chart() } } </script> <style scoped> </style>
1.以上代碼是引入echarts,橫坐標與dataZoom是從父組件傳遞過來的,dataZoom使用父組件傳遞是為了確保數據量少的圖表不需要拉伸展示數據
const dataZoom = [ { id: 'dataZoomX', type: 'slider', xAxisIndex: [0], filterMode: 'filter', start: 0, end: 50, } ]
以上是dadaZoom的值,我們需要動態設置end的值,以確保不管數據有多少,打開后不會擠成一團,
this.dataZoom[0].end=10/this.source.length*100
以上代碼,顯示當數據是10條數據的時候,展示正合適。
可根據調取數據,確定幾條數據合適
2.dataZoom與坐標軸重合,
grid: { left: '3%', right: '4%', bottom: '12%', containLabel: true },
在以上字段中。設置bottom的值,即可放置在妥善的位置。