echarts.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!--
  2. * @LastEditors: gcz
  3. -->
  4. <!-- https://blog.csdn.net/zyz13883658166/article/details/124430938 -->
  5. <template>
  6. <div ref="wrap">
  7. <!-- <div>{{mid}}</div> -->
  8. <div class="ntitle">{{option.title.text}}</div>
  9. <div class="addr">{{$store.state.addr.selectAddr}}</div>
  10. <div :id="mid" class="echarts" :style="style"></div>
  11. </div>
  12. </template>
  13. <script>
  14. import * as echarts from "echarts";
  15. const idGen = () => {
  16. return new Date().getTime();
  17. };
  18. export default {
  19. name: "HelloEcharts",
  20. props: {
  21. height: {
  22. type: String,
  23. default: null,
  24. },
  25. width: {
  26. type: String,
  27. default: "100%",
  28. },
  29. option: {
  30. type: Object,
  31. default: null,
  32. },
  33. mid: {
  34. type: String,
  35. default: null,
  36. },
  37. },
  38. data() {
  39. return {
  40. uuid: null,
  41. myChart: null,
  42. domHeight:null,
  43. };
  44. },
  45. created() {
  46. this.uuid = idGen();
  47. // console.log('this.option',this.option);
  48. },
  49. watch: {
  50. // '$store.state.addr.mapLevel': {
  51. // handler:function (newVal, oldVal) {
  52. // console.log('newVal',newVal);
  53. // console.log('oldVal',oldVal);
  54. // },
  55. // immediate: true
  56. // },
  57. width() {
  58. //如果实例可用
  59. if (this.myChart) {
  60. this.myChart.resize({
  61. Animation: {
  62. duration: 300,
  63. },
  64. });
  65. }
  66. },
  67. option() {
  68. // console.log('option',console.log('this.option',JSON.parse(JSON.stringify(this.option))));
  69. if (this.myChart) {
  70. // notMerge这个方法,是表示配置不重复,但是目前这个分代码没有生效
  71. this.myChart.setOption(this.option),
  72. {
  73. notMerge: true,
  74. };
  75. }
  76. },
  77. },
  78. computed: {
  79. style() {
  80. return {
  81. height: this.height?this.height:this.domHeight-57 +'px',
  82. width: this.width,
  83. };
  84. },
  85. },
  86. methods: {
  87. intChar(charUuid){
  88. // console.log('intChar charUuid',charUuid);
  89. // console.log('this.uuid 前',this.uuid);
  90. // // this.uuid = idGen();
  91. // console.log('this.uuid 后',this.uuid);
  92. echarts.init(document.getElementById(charUuid)).clear();
  93. this.myChart = echarts.init(document.getElementById(charUuid));
  94. this.myChart.setOption(this.option);
  95. }
  96. },
  97. mounted() {
  98. console.log('height',this.height);
  99. let obj = this.$refs.wrap;
  100. let h = window.getComputedStyle(obj).height;
  101. console.log('h',h);
  102. this.domHeight = h.slice(0, -2);
  103. console.log('domHeight',this.domHeight);
  104. console.log('style',this.style);
  105. console.log('echarts mounted');
  106. // echarts.init(document.getElementById('echarts')).dispose();//销毁前一个实例
  107. this.$nextTick(()=>{
  108. this.myChart = echarts.init(document.getElementById(this.mid));
  109. this.myChart.setOption(this.option);
  110. })
  111. },
  112. };
  113. </script>
  114. <style lang='scss' scoped>
  115. .addr{
  116. text-align: right;
  117. padding-right: 24px;
  118. font-size: 14px;
  119. }
  120. </style>