commonChartsLine.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!--
  2. * @Author: zk
  3. * @Date: 2021-09-16 16:24:53
  4. * @LastEditTime: 2021-11-02 15:45:53
  5. * @LastEditors: Please set LastEditors
  6. * @Description: 折线图
  7. * @FilePath: \vue-admin-template-master\src\layout\components\Echarts\commonChartsLine.vue
  8. -->
  9. <template>
  10. <div class="ChartsLine" :id="id"></div>
  11. </template>
  12. <script>
  13. // 折线图初始数据
  14. import ChartsLine from '../../mixin/commonChartsLine'
  15. export default {
  16. name: 'ChartsLine',
  17. props: {
  18. // 折线图生成所需id
  19. id: {
  20. type: String,
  21. default: ''
  22. },
  23. // 折线图额外的数据
  24. option: {
  25. type: Object,
  26. default: () => { }
  27. }
  28. },
  29. mixins: [ChartsLine],
  30. data () {
  31. return {
  32. myChart: null, //折线图实例
  33. desc: 300 // 防抖时间
  34. }
  35. },
  36. mounted () {
  37. // 生成图形
  38. this.initEcharts()
  39. // 监听页面缩放 防止dom重复渲染
  40. window.addEventListener('resize', _.debounce(this.handle, this.desc))
  41. },
  42. beforeDestroy () {
  43. // 销毁实例和移除监听
  44. if (this.myChart) {
  45. this.myChart.dispose()
  46. window.removeEventListener('resize', this.handle)
  47. this.myChart = null
  48. }
  49. },
  50. watch: {
  51. // 监听数据变化 重绘图形
  52. option: {
  53. handler (obj) {
  54. // 初始数据和额外的配置数据耦合
  55. const objData = _.cloneDeep(this.options)
  56. _.merge(objData, obj)
  57. // 生成柱状图
  58. this.myChart.setOption(objData)
  59. },
  60. deep: true
  61. }
  62. },
  63. methods: {
  64. /**
  65. * @description: 图形缩放
  66. * @param {*}
  67. * @return {*}
  68. */
  69. handle () {
  70. if (this.myChart) {
  71. this.myChart.resize()
  72. }
  73. },
  74. /**
  75. * @description: 初始化echarts
  76. * @param {*}
  77. * @return {*}
  78. */
  79. initEcharts () {
  80. // 初始数据和额外的配置数据耦合
  81. const objData = _.cloneDeep(this.options)
  82. _.merge(objData, this.option)
  83. // dom
  84. const chartDom = document.getElementById(this.id)
  85. // 初始化
  86. this.myChart = this.$echarts.init(chartDom)
  87. // 生成柱状图
  88. this.myChart.setOption(objData)
  89. // 事件
  90. this.myChart.on('click', params => {
  91. this.$emit('getChartData', params)
  92. })
  93. }
  94. }
  95. }
  96. </script>
  97. <style lang="scss" scoped>
  98. .ChartsLine {
  99. height: 100%;
  100. width: 100%;
  101. position: absolute;
  102. left: 0;
  103. top: 0;
  104. }
  105. </style>