index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <div class="echart" :class="{ bg, noPadding }" :style="{ height }" ref="chart"></div>
  3. </template>
  4. <script setup lang="ts">
  5. import * as echarts from 'echarts'
  6. import { onMounted, ref, watch } from 'vue'
  7. // import * as chartOptions from './options.js'
  8. import { useStore } from '/@/store/index'
  9. const store = useStore()
  10. const loadingOption = {
  11. // maskColor: 'rgba(255, 255, 255, 0.1)'
  12. maskColor: 'rgba(255, 255, 255, 0)',
  13. text: '',
  14. color: '#409eff',
  15. // textColor: '#000',
  16. // zlevel: 0,
  17. // fontSize: 12,
  18. // showSpinner: true,
  19. spinnerRadius: 18,
  20. lineWidth: 2,
  21. // fontWeight: 'normal',
  22. // fontStyle: 'normal',
  23. // fontFamily: 'sans-serif'
  24. }
  25. const props = defineProps({
  26. bg: Boolean,
  27. noPadding: Boolean,
  28. auto: {
  29. type: Boolean,
  30. default: false,
  31. },
  32. autoLoading: {
  33. type: Boolean,
  34. default: true ,
  35. },
  36. height: {
  37. type: String,
  38. default: '100%',
  39. },
  40. type: {
  41. type: String,
  42. default: 'Bar',
  43. },
  44. option: {
  45. type: Object,
  46. default: () => {
  47. return {}
  48. },
  49. },
  50. })
  51. let myChart: any = null
  52. let optionCache: any = null
  53. const chart = ref()
  54. // 绘制图形
  55. const draw = (option?: object) => {
  56. myChart && myChart.dispose()
  57. myChart = echarts.init(chart.value)
  58. myChart.hideLoading()
  59. if (option) {
  60. optionCache = option
  61. myChart.setOption(option)
  62. } else {
  63. // myChart.setOption(chartOptions[`get${props.type}Option`]({ ...props.option }))
  64. }
  65. }
  66. const loading = () => {
  67. myChart?.setOption({}, { notMerge: true })
  68. myChart?.showLoading(loadingOption)
  69. }
  70. const getChart = () => {
  71. return myChart
  72. }
  73. const download = (name: string = 'chart picture') => {
  74. const picInfo = myChart.getDataURL({
  75. type: 'png',
  76. pixelRatio: 2,
  77. backgroundColor: '#fff',
  78. })
  79. let elink = document.createElement('a')
  80. elink.download = name
  81. elink.style.display = 'none'
  82. elink.href = picInfo
  83. document.body.appendChild(elink)
  84. elink.click()
  85. URL.revokeObjectURL(elink.href)
  86. document.body.removeChild(elink)
  87. }
  88. // 配置变化 重绘
  89. watch(
  90. () => props.option,
  91. () => {
  92. draw()
  93. }
  94. )
  95. watch(
  96. () => store.state.global.resize,
  97. () => {
  98. draw(optionCache)
  99. }
  100. )
  101. onMounted(() => {
  102. myChart = echarts.init(chart.value)
  103. props.autoLoading && loading()
  104. props.auto && draw()
  105. })
  106. defineExpose({ draw, loading, download, getChart })
  107. </script>
  108. <style scoped lang="scss">
  109. .echart {
  110. height: 100%;
  111. width: 100%;
  112. // margin: 1vh 0;
  113. padding: 2px;
  114. }
  115. .bg {
  116. background: #444;
  117. }
  118. </style>