echarts.vue 3.8 KB

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