index.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { ref, computed } from 'vue'
  2. import { useFuturesStore } from '@/stores'
  3. import { formatDate } from '@/filters'
  4. import { queryHistoryTikDatas } from '@/services/api/quote'
  5. import { useDataset } from './dataset'
  6. import { useOptions } from './options'
  7. export function useSmoothedLineChart(goodscode: string) {
  8. const { quoteWatch } = useFuturesStore()
  9. const { dataset, clearData } = useDataset()
  10. const { options, initOptions, updateOptions } = useOptions(dataset)
  11. const loading = ref(false)
  12. const isEmpty = ref(false)
  13. const dataIndex = ref(-1); // 当前数据索引值
  14. // 当前选中的数据项
  15. const selectedItem = computed(() => {
  16. const { price } = dataset.line.source;
  17. return {
  18. price: price[dataIndex.value] ?? '--',
  19. }
  20. })
  21. /**
  22. * 初始化数据
  23. */
  24. const initData = () => {
  25. clearData();
  26. dataIndex.value = -1;
  27. isEmpty.value = true;
  28. loading.value = true;
  29. queryHistoryTikDatas({
  30. data: {
  31. goodsCode: goodscode,
  32. startTime: formatDate(new Date().toISOString(), 'YYYY-MM-DD 00:00:00'),
  33. endTime: formatDate(new Date().toISOString(), 'YYYY-MM-DD 23:59:59'),
  34. isAsc: true,
  35. }
  36. }).then((res) => {
  37. if (res.data.length) {
  38. const { date, price } = dataset.line.source
  39. dataIndex.value = res.data.length - 1
  40. isEmpty.value = false
  41. res.data.forEach(({ PE, TS }) => {
  42. date.push(formatDate(TS, 'HH:mm:ss'))
  43. price.push(PE)
  44. })
  45. }
  46. }).finally(() => {
  47. loading.value = false
  48. })
  49. }
  50. // 监听行情推送
  51. quoteWatch(goodscode, ({ last, lasttime }) => {
  52. if (!loading.value && last) {
  53. const { date, price } = dataset.line.source
  54. date.push(formatDate(lasttime, 'HH:mm:ss'))
  55. price.push(last)
  56. updateOptions()
  57. }
  58. })
  59. return {
  60. options,
  61. loading,
  62. isEmpty,
  63. dataIndex,
  64. selectedItem,
  65. initData,
  66. initOptions,
  67. }
  68. }