echartsEchar.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <div :id="uuid" :style="style"></div>
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. export default {
  7. props: {
  8. height: {
  9. type: String,
  10. default: "100%",
  11. },
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. options: {
  17. type: Object,
  18. default: null,
  19. },
  20. widthHtval: {
  21. type: [String, Boolean],
  22. default: false
  23. },
  24. clickOnTheEvent: { // 是否开启点击事件
  25. type: [Boolean],
  26. default: false
  27. }
  28. },
  29. components: {},
  30. data() {
  31. return {
  32. uuid: null,
  33. myChart: null,
  34. };
  35. },
  36. computed: {
  37. style() {
  38. return {
  39. height: this.height,
  40. width: this.width,
  41. };
  42. },
  43. },
  44. watch: {
  45. options() {
  46. if (this.myChart) {
  47. if (this.widthHtval) {
  48. this.myChart.resize({
  49. width: this.widthHtval
  50. })
  51. }
  52. this.myChart.setOption(this.options, { notMerge: true });
  53. }
  54. },
  55. },
  56. created() {
  57. this.uuid = this.idGen();
  58. console.log(this.uuid, '<===== uuId')
  59. },
  60. mounted() {
  61. this.myChart = echarts.init(document.getElementById(this.uuid));
  62. this.myChart.setOption(this.options);
  63. if (this.clickOnTheEvent) {
  64. this.myChart.getZr().on('click', params => {
  65. this.$emit('chartClickEvents', { params, myChart: this.myChart })
  66. })
  67. }
  68. },
  69. methods: {
  70. // idGen() {
  71. // return new Date().getTime();
  72. // },
  73. idGen() {
  74. const arr = new Uint8Array(16);
  75. window.crypto.getRandomValues(arr);
  76. arr[6] = (arr[6] & 0x0f) | 0x40; // 设置版本为 0100
  77. arr[8] = (arr[8] & 0x3f) | 0x80; // 设置变体为 10xx
  78. return [...arr].map((b) => ('00' + b.toString(16)).slice(-2)).join('').match(/.{1,8}/g).join('-');
  79. },
  80. },
  81. beforeDestroy() {
  82. if (this.myChart) {
  83. this.myChart.dispose();
  84. }
  85. }
  86. };
  87. </script>
  88. <style scoped lang="scss"></style>