| | |
| | | 使用同一个图形实例,接受父组件传入的折线图option |
| | | --> |
| | | <template> |
| | | <div ref="chart" class="line-chart"></div> |
| | | <div ref="chart" class="line-chart11"></div> |
| | | </template> |
| | | |
| | | <script> |
| | | import * as _echarts from 'echarts'; |
| | | import * as _echarts from 'echarts' |
| | | |
| | | export default { |
| | | props: { |
| | |
| | | return { |
| | | x: [], |
| | | y: [] |
| | | }; |
| | | } |
| | | } |
| | | }, |
| | | title: { |
| | |
| | | data() { |
| | | return { |
| | | chart: null |
| | | }; |
| | | } |
| | | }, |
| | | mounted() { |
| | | this.intiChart(); |
| | | window.addEventListener('resize', this.resizeChart); |
| | | this.intiChart() |
| | | window.addEventListener('resize', this.resizeChart) |
| | | }, |
| | | watch: { |
| | | chartData() { |
| | | // option变化时,图形再次初始化 |
| | | this.setOption(); |
| | | }, |
| | | |
| | | this.setOption() |
| | | } |
| | | }, |
| | | // beforeUnmount() { |
| | | // if (this.chart) { |
| | | // this.chart.dispose(); |
| | | // } |
| | | // }, |
| | | |
| | | methods: { |
| | | intiChart() { |
| | | // 创建echarts实例 |
| | | this.chart = _echarts.init(this.$refs.chart); |
| | | this.chart = _echarts.init(this.$refs.chart) |
| | | }, |
| | | |
| | | setOption() { |
| | |
| | | name: this.xName, |
| | | data: this.chartData.x, |
| | | type: 'category', |
| | | axisLabel: { |
| | | formatter: function (value) { |
| | | return value.slice(5); |
| | | } |
| | | axisLabel: { |
| | | formatter: function (value) { |
| | | return value.slice(5) |
| | | } |
| | | } |
| | | }, |
| | | yAxis: { |
| | | type: 'value', |
| | |
| | | data: this.chartData.y |
| | | } |
| | | ] |
| | | }; |
| | | this.chart.setOption(option); |
| | | } |
| | | this.chart.setOption(option) |
| | | }, |
| | | |
| | | // 跟页面响应式变化 |
| | | resizeChart() { |
| | | // this.chart.resize(); |
| | | this.$nextTick(() => { |
| | | if (this.chart) { |
| | | this.chart.resize(); |
| | | } |
| | | }); |
| | | setTimeout(() => { |
| | | this.chart.resize() |
| | | }, 1000) |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | </script> |
| | | |
| | | <style> |
| | | .line-chart { |
| | | width: 100%; |
| | | .line-chart11 { |
| | | width: 600px; |
| | | height: 300px; |
| | | margin-top: 25px; |
| | | margin-left: 30px; |
| | | } |
| | | </style> |