vue2

  1. <!--
  2. * @Description:
  3. * @Author: Harry
  4. * @Date: 2021-10-28 18:57:36
  5. * @Url: https://u.mr90.top
  6. * @github: https://github.com/rr210
  7. * @LastEditTime: 2021-10-28 19:07:08
  8. * @LastEditors: Harry
  9. -->
  10. <template>
  11. <div class="com-container">
  12. <div class="com-chart" ref="stockRef"></div>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. chartInstance: null,
  20. allData: null,
  21. titleFontSize: 0,
  22. timerId: null
  23. }
  24. },
  25. mounted() {
  26. this.initChart()
  27. this.getData()
  28. window.addEventListener('resize', this.screenAdapter)
  29. this.screenAdapter()
  30. },
  31. destroyed() {
  32. window.removeEventListener('resize', this.screenAdapter)
  33. clearInterval(this.timerId)
  34. },
  35. computed() {},
  36. methods: {
  37. // 初始化图表
  38. initChart() {
  39. this.chartInstance = this.$echarts.init(this.$refs.stockRef)
  40. const initOption = {}
  41. this.chartInstance.setOption(initOption)
  42. this.chartInstance.on('mouseover', () => {
  43. clearInterval(this.timerId)
  44. })
  45. this.chartInstance.on('mouseout', () => {
  46. this.startInterval()
  47. })
  48. },
  49. getData() {
  50. this.updateChart()
  51. },
  52. updateChart() {
  53. const updateOption = {}
  54. this.chartInstance.setOption(updateOption)
  55. },
  56. screenAdapter() {
  57. this.titleFontSize = (this.$refs.stockRef.offsetWidth / 100) * 3.6
  58. const adapterOption = {}
  59. this.chartInstance.setOption(adapterOption)
  60. this.chartInstance.resize()
  61. },
  62. startInterval() {
  63. if (this.timerId) clearInterval(this.timerId)
  64. this.timerId = setInterval(() => {}, 3000)
  65. }
  66. }
  67. }
  68. </script>
  69. <style lang="less" scoped>
  70. </style>

vue3

  1. <template>
  2. <div id="customerChart" :style="{ width: '90vw', height: '300px' }"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. import { onMounted, onUnmounted, reactive } from 'vue'
  7. export default {
  8. setup() {
  9. const state = reactive({
  10. chartInstance: null,
  11. allData: null,
  12. titleFontSize: 0,
  13. timerId: null,
  14. dom: null
  15. })
  16. // 初始化图表
  17. const initChart = function () {
  18. state.chartInstance = echarts.init(state.dom)
  19. const initOption = {}
  20. state.chartInstance.setOption(initOption)
  21. state.chartInstance.on('mouseover', () => {
  22. clearInterval(state.timerId)
  23. })
  24. state.chartInstance.on('mouseout', () => {
  25. startInterval()
  26. })
  27. }
  28. const getData = function () {
  29. updateChart()
  30. }
  31. const updateChart = function () {
  32. const updateOption = {
  33. title: { text: '历史识别分析' },
  34. tooltip: {},
  35. xAxis: {
  36. data: ['12-3', '12-4', '12-5', '12-6', '12-7', '12-8']
  37. },
  38. yAxis: {},
  39. series: [
  40. {
  41. name: '用户量',
  42. type: 'line',
  43. data: [5, 20, 36, 10, 10, 20]
  44. }
  45. ]
  46. }
  47. state.chartInstance.setOption(updateOption)
  48. }
  49. const screenAdapter = function () {
  50. state.titleFontSize = (state.dom.offsetWidth / 100) * 3.6
  51. const adapterOption = {}
  52. state.chartInstance.setOption(adapterOption)
  53. state.chartInstance.resize()
  54. }
  55. const startInterval = function () {
  56. if (state.timerId) clearInterval(state.timerId)
  57. state.timerId = setInterval(() => { }, 3000)
  58. }
  59. onMounted(() => { // 需要获取到element,所以是onMounted的Hook
  60. // 绘制图表
  61. state.dom = document.getElementById('customerChart')
  62. initChart()
  63. getData()
  64. window.addEventListener('resize', screenAdapter)
  65. screenAdapter()
  66. })
  67. onUnmounted(() => {
  68. window.removeEventListener('resize', state.screenAdapter)
  69. clearInterval(state.timerId)
  70. })
  71. return {
  72. }
  73. }
  74. }
  75. </script>
  76. <style lang='less' scoped>
  77. </style>