index.vue
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref, unref } from 'vue'
import type { ECharts, EChartsOption } from 'echarts'
import { init } from 'echarts'
import { getSetValue, setOption } from './index.config'
import type { CreateComponentType, RenderComponentExposeType } from '@/core/Library/types'
import 'echarts-liquidfill' // 水球图插件
import { useOnMessage } from '@/core/Library/hook/useOnMessage'
import type { NodeDataDataSourceJsonType } from '@/api/node/model'
import { useLatestMessageValue } from '@/core/Library/hook/useLatestMessageValue'
import { useContentDataStore } from '@/store/modules/contentData'
import { useProductsStore } from '@/store/modules/products'
const props = defineProps<{
  config: CreateComponentType
}>()
const contentDataStore = useContentDataStore()
const getCurrentNodeData = computed(() => contentDataStore.contentData.find(item => item.id === props.config.cellInfo?.id))
const getCellBounds = computed(() => props.config.cellBounds || { width: 300, height: 300, x: 0, y: 0 })
const chartElRef = ref<Nullable<HTMLDivElement>>()
const chartInstance = ref<Nullable<ECharts>>()
const productsStore = useProductsStore()
function initChartInstance() {
  const { dataSourceJson } = unref(getCurrentNodeData) || {}
  const { circularFlowMeterOption } = dataSourceJson || {}
  chartInstance.value = init(unref(chartElRef))
  chartInstance.value.setOption(setOption(40, circularFlowMeterOption))
}
const { onMessage } = useOnMessage({
  onReceiveDataSourceMessage(commandSource, message) {
    const { data } = commandSource
    const { attr, deviceName, deviceProfileId } = data as NodeDataDataSourceJsonType
    const { functionName } = productsStore.getObjectModelByIdWithIdentifier(deviceProfileId, (attr) as string) || {}
    const { latestValue } = useLatestMessageValue(message.data, (attr as string))
    unref(chartInstance)?.setOption({
      title: {
        text: `${deviceName || ''} - ${functionName || ''}`,
      },
      series: [{
        data: getSetValue(Number(latestValue)),
      }],
    } as EChartsOption)
  },
})
onMounted(() => {
  initChartInstance()
})
onUnmounted(() => {
  chartInstance.value?.dispose()
  chartInstance.value = null
})
defineExpose<RenderComponentExposeType>({ onMessage })
</script>
<template>
  <div class="w-full h-full justify-center flex items-center">
    <div
      ref="chartElRef"
      :style="{ width: `${getCellBounds.width}px`, height: `${getCellBounds.height}px` }"
      class="w-full h-full"
    />
  </div>
</template>