commonEchart.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <div
  3. :style="{ width: width, height: height }"
  4. ref="baseEchartRef"
  5. id="baseEchart"
  6. v-on-echart-resize
  7. ></div>
  8. </template>
  9. <script>
  10. import * as echarts from "echarts";
  11. import elementResizeDetectorMaker from "element-resize-detector";
  12. import "../aJs/chart.resize";
  13. export default {
  14. props: {
  15. width: {
  16. type: String,
  17. default: "100%"
  18. },
  19. height: {
  20. type: String,
  21. default: "300px"
  22. },
  23. option: {
  24. type: Object,
  25. default: () => {}
  26. }
  27. },
  28. watch: {
  29. option() {
  30. this._initEchart();
  31. }
  32. },
  33. mounted() {
  34. // this.$nextTick(() => {
  35. // setTimeout(() => {
  36. // this._initEchart()
  37. // },0)
  38. // })
  39. this._initEchart()
  40. let erd = elementResizeDetectorMaker();
  41. let that = this;
  42. erd.listenTo(document.getElementById("baseEchart"), (element) => {
  43. // let width = element.offsetWidth;
  44. // let height = element.offsetHeight;
  45. that.$nextTick(() => {
  46. //使echarts尺寸重置
  47. echarts.init(document.getElementById("baseEchart")).resize();
  48. });
  49. });
  50. },
  51. computed: {
  52. },
  53. methods: {
  54. _initEchart() {
  55. let that=this
  56. const echartInstance = echarts.init(this.$refs.baseEchartRef);
  57. echartInstance.setOption(this.option);
  58. echartInstance.on('legendselectchanged', function (params) {
  59. if(that.option.name=='barOption'){
  60. that.$emit('legendselectchanged',params.selected)
  61. }
  62. });
  63. // 监听window尺寸的变化
  64. // echartInstance.resize();
  65. // window.addEventListener("resize", () => {
  66. // echartInstance.resize();
  67. // });
  68. }
  69. }
  70. };
  71. </script>
  72. <style lang="less" scoped></style>