echartsgas.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <div
  3. style="height: 100%; width: 100%; position: absolute; left: 0; top: 0"
  4. class="ChartsBar"
  5. :id="id"
  6. ></div>
  7. </template>
  8. <script>
  9. import { markRaw } from "vue";
  10. import * as echarts from "echarts";
  11. import * as _ from "lodash";
  12. import { collection } from "./echarts.js";
  13. // 柱状图初始数据
  14. export default {
  15. props: {
  16. // 柱状图生成所需id
  17. id: {
  18. type: String,
  19. default: "collection_ecahrt",
  20. },
  21. // 柱状图额外的数据
  22. option: {
  23. type: Object,
  24. default: () => {},
  25. },
  26. },
  27. data() {
  28. return {
  29. myChart: null, //柱状图实例
  30. };
  31. },
  32. mounted() {
  33. // 柱状图dom
  34. const chartDom = document.getElementById(this.id);
  35. // 柱状图初始化
  36. this.myChart = markRaw(echarts.init(chartDom));
  37. collection.xAxis.data = this.option.time;
  38. // this.option.list.forEach((item, index) => {
  39. // collection.series.push({
  40. // name: "",
  41. // type: "bar",
  42. // // barWidth: 18,
  43. // data: item,
  44. // });
  45. // });
  46. // collection.series[0].data = this.option.data1;
  47. // 生成柱状图
  48. this.myChart.setOption(collection);
  49. // 监听页面缩放 防止dom重复渲染
  50. window.addEventListener("resize", _.debounce(this.handle, this.desc));
  51. },
  52. watch: {
  53. // 监听数据变化,重绘折线图
  54. option: {
  55. deep: true,
  56. handler(newVal) {
  57. collection.series = [];
  58. const chartDom = document.getElementById(this.id);
  59. // 先移除之前的实例,保证重绘的流畅性
  60. chartDom.removeAttribute("_echarts_instance_");
  61. this.myChart = markRaw(echarts.init(chartDom));
  62. collection.xAxis.data = newVal.time;
  63. let arr = JSON.parse(JSON.stringify(newVal.list));
  64. for (let index = 0; index < arr.length; index++) {
  65. collection.series.push({
  66. name: "",
  67. type: "bar",
  68. // barWidth: 18,
  69. data: arr[index],
  70. });
  71. }
  72. this.myChart.setOption(collection);
  73. },
  74. },
  75. },
  76. destroyed() {
  77. // 销毁实例和移除监听
  78. this.myChart.dispose();
  79. window.removeEventListener("resize", this.handle);
  80. },
  81. methods: {
  82. /**
  83. * @description: 图形缩放
  84. * @param {*}
  85. * @return {*}
  86. */
  87. handle() {
  88. this.myChart.resize();
  89. },
  90. },
  91. };
  92. </script>