index.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <div :class="className" :style="{height:height,width:width}" />
  3. </template>
  4. <script>
  5. // NOTE: hht 22.1.27 改造成通用组件
  6. // https://echarts.apache.org/examples/zh/
  7. import echarts from 'echarts'
  8. require('echarts/theme/macarons') // echarts theme
  9. import resize from './mixins/resize'
  10. export default {
  11. name: 'mx-chart',
  12. mixins: [resize],
  13. props: {
  14. className: {
  15. type: String,
  16. default: 'chart'
  17. },
  18. width: {
  19. type: String,
  20. default: '100%'
  21. },
  22. height: {
  23. type: String,
  24. default: '300px'
  25. },
  26. options: {
  27. type: Object,
  28. default: null
  29. }
  30. },
  31. data() {
  32. return {
  33. chart: null
  34. }
  35. },
  36. watch: {
  37. options: function(opt) {
  38. if (!this.chart || !opt) return
  39. console.log('mx-chart set option', this.options)
  40. this.chart.setOption(opt, true) // true: notMerge
  41. }
  42. },
  43. mounted() {
  44. this.$nextTick(() => {
  45. this.initChart()
  46. })
  47. },
  48. beforeDestroy() {
  49. if (!this.chart) {
  50. return
  51. }
  52. this.chart.dispose()
  53. this.chart = null
  54. },
  55. methods: {
  56. initChart() {
  57. this.chart = echarts.init(this.$el, 'macarons')
  58. if (!this.options) return
  59. console.log('mx-chart set option', this.options)
  60. this.chart.setOption(this.options)
  61. }
  62. }
  63. }
  64. </script>
  65. <style scoped>
  66. </style>