基于ECharts封装的vue折线图组件

Love The Way You Lie 2023-06-07 12:05 107阅读 0赞
  1. <template>
  2. <div class="qf-e-chart">
  3. <div class="e-chart-canvas" ref="echarts"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import echarts from 'echarts'
  8. export default {
  9. name: 'EChart',
  10. props: {
  11. options: { type: Object, default: {} },
  12. clickCall: { type: Function, default: () => {} }
  13. },
  14. methods: {
  15. setChart() {
  16. // 初始化echarts实例
  17. const chart = echarts.init(this.$refs.echarts)
  18. let option = {
  19. title: {},
  20. tooltip: {},
  21. legend: {},
  22. xAxis: {},
  23. yAxis: {},
  24. series: [],
  25. toolbox: {},
  26. grid: {}
  27. }
  28. Object.assign(option, this.options)
  29. chart.clear()
  30. // 绘制图表
  31. chart.setOption(option, true)
  32. // 浏览器大小改变时重置大小
  33. window.onresize = function () {
  34. chart.resize()
  35. }
  36. // 容器大小改变时重置大小
  37. this.$nextTick(() => {
  38. chart.resize()
  39. })
  40. // 防止重复触发点击事件
  41. if (chart._$handlers.click) {
  42. chart._$handlers.click.length = 0
  43. }
  44. chart.on('click', (params) => {
  45. this.clickCall && this.clickCall(params)
  46. })
  47. },
  48. initChart() {
  49. this.$nextTick(() => {
  50. this.setChart()
  51. })
  52. }
  53. },
  54. mounted() {
  55. this.initChart()
  56. }
  57. }
  58. </script>
  59. <style scoped>
  60. .e-chart-canvas {
  61. width: 100%;
  62. min-height: 380px;
  63. max-height: 400px;
  64. }
  65. </style>

发表评论

表情:
评论列表 (有 0 条评论,107人围观)

还没有评论,来说两句吧...

相关阅读