多个图表写法
| <div id="main" style="width: 100%; height: 260px"></div> |
| |
| <div id="main2" style="width: 100%; height: 300px; margin-top: 20px"></div> |
复制
| import * as echarts from "echarts"; |
| import { brokenLineOptions } from "./echarts/broken-line"; |
| import { barGraphOption } from "./echarts/bar-graph"; |
| |
| |
| methods:{ |
| |
| initEahcrts() { |
| |
| |
| this.myChart = echarts.init(document.querySelector("#main")); |
| |
| this.myChart.setOption(brokenLineOptions, true); |
| }, |
| chartResize() { |
| this.myChart.resize({ |
| width: document.querySelector("#main").getBoundingClientRect().width, |
| }); |
| }, |
| |
| initBrokenLine() { |
| this.myChart2 = echarts.init(document.getElementById("main2")); |
| this.myChart2.setOption(barGraphOption); |
| }, |
| chartResize2() { |
| this.myChart2.resize({ |
| width: document.querySelector("#main2").getBoundingClientRect().width, |
| }); |
| }, |
| }, |
| mounted() { |
| this.initEahcrts(); |
| this.initBrokenLine(); |
| window.addEventListener("resize", this.chartResize); |
| window.addEventListener("resize", this.chartResize2); |
| }, |
复制
重点:事件监听后要记得移除事件,因为是绑定在window上的
| |
| beforeDestroy() { |
| window.removeEventListener("resize", this.chartResize); |
| window.removeEventListener("resize", this.chartResize2); |
| }, |
复制