index.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <div ref="chartRef" class="app-hqchart" :style="{ width, height }"></div>
  3. </template>
  4. <script lang="ts" setup>
  5. import { shallowRef, onMounted, onUnmounted } from 'vue'
  6. import { Chart } from 'hqchart'
  7. import { timerInterceptor } from '@/utils/timer'
  8. import ResizeObserver from 'resize-observer-polyfill'
  9. const props = defineProps({
  10. // 图表宽度
  11. width: {
  12. type: String,
  13. default: '100%',
  14. },
  15. // 图表高度
  16. height: {
  17. type: String,
  18. default: '100%',
  19. },
  20. // 图表配置项
  21. option: {
  22. type: Object
  23. },
  24. dataIndex: {
  25. type: Number,
  26. default: 0,
  27. }
  28. })
  29. const emit = defineEmits(['ready', 'update:dataIndex'])
  30. const chartRef = shallowRef()
  31. const resource = Chart.JSChart.GetResource()
  32. resource.FrameLogo.Text = ''
  33. // https://blog.csdn.net/jones2000/article/details/104122774
  34. Chart.JSConsole.Chart.Log = () => ({})
  35. Chart.JSConsole.Complier.Log = () => ({})
  36. // 默认配置项
  37. const defaultOption = {
  38. Symbol: 'default', // 股票代码(必填)
  39. IsAutoUpdate: false,
  40. IsApiPeriod: true, // 每次切换周期请求接口数据
  41. }
  42. onMounted(() => {
  43. const el = chartRef.value
  44. const chartInstance = Chart.JSChart.Init(el)
  45. chartInstance.SetOption(props.option ?? defaultOption)
  46. // 自适应图表大小
  47. const resize = timerInterceptor.setDebounce(() => {
  48. chartInstance.OnSize()
  49. }, 50)
  50. // 监听元素变化
  51. const resizeObserver = new ResizeObserver(resize)
  52. resizeObserver.observe(el)
  53. emit('ready', chartInstance)
  54. onUnmounted(() => {
  55. resizeObserver.unobserve(el)
  56. chartInstance.ClearChart()
  57. })
  58. })
  59. </script>