Commit 5fc22f51561dad3bb8644189b6dae8e14331c11b

Authored by xp.Huang
2 parents ed3194e3 1407effd

Merge branch 'ft' into 'main_dev'

feat(src/packages): 修复select和时间联动组件联动图表dataset视图未更新问题

See merge request yunteng/thingskit-view!79
  1 +/**
  2 + * 重写select下拉框联动
  3 + */
  4 +import {toRaw, toRefs} from 'vue'
  5 +import {CreateComponentType} from '@/packages/index.d'
  6 +import {useChartEditStore} from '@/store/modules/chartEditStore/chartEditStore'
  7 +import {customRequest} from "@/api/external/customRequest";
  8 +import {useFilterFn} from "@/hooks/external/useFilterFn";
  9 +
  10 +// 获取类型
  11 +type ChartEditStoreType = typeof useChartEditStore
  12 +
  13 +// Params 参数修改触发 api 更新图表请求
  14 +export const useChartInteract = (
  15 + chartConfig: CreateComponentType,
  16 + useChartEditStore: ChartEditStoreType,
  17 + param: { [T: string]: any },
  18 + interactEventOn: string
  19 +) => {
  20 + const chartEditStore = useChartEditStore()
  21 + const {interactEvents} = chartConfig.events
  22 + const fnOnEvent = interactEvents.filter(item => {
  23 + return item.interactOn === interactEventOn
  24 + })
  25 +
  26 + if (fnOnEvent.length === 0) return
  27 + fnOnEvent.forEach(async (item) => {
  28 + const index = chartEditStore.fetchTargetIndex(item.interactComponentId)
  29 + if (index === -1) return
  30 + const {Params, Header} = toRefs(chartEditStore.componentList[index].request.requestParams)
  31 + Object.keys(item.interactFn).forEach(key => {
  32 + if (Params.value[key]) {
  33 + Params.value[key] = param[item.interactFn[key]]
  34 + }
  35 + if (Header.value[key]) {
  36 + Header.value[key] = param[item.interactFn[key]]
  37 + }
  38 + })
  39 + const res = await customRequest(toRaw(chartEditStore.componentList[index].request))
  40 + if (res) {
  41 + try {
  42 + const filter = chartEditStore.componentList[index].filter
  43 + const {value} = useFilterFn(filter, res)
  44 + if (!value) return
  45 + chartEditStore.componentList[index].option.dataset = value
  46 + } finally {
  47 + console.log
  48 + }
  49 + }
  50 + })
  51 +}
  52 +
  53 +// 联动事件触发的 type 变更时,清除当前绑定内容
  54 +export const clearInteractEvent = (chartConfig: CreateComponentType) => {
  55 +
  56 +}
1 <template> 1 <template>
2 <v-chart 2 <v-chart
3 - ref="vChartRef"  
4 - :init-options="initOptions"  
5 - :theme="themeColor"  
6 - :option="option"  
7 - :manual-update="isPreview()"  
8 - :update-options="{ 3 + ref="vChartRef"
  4 + :init-options="initOptions"
  5 + :theme="themeColor"
  6 + :option="option"
  7 + :manual-update="isPreview()"
  8 + :update-options="{
9 replaceMerge: replaceMergeArr 9 replaceMerge: replaceMergeArr
10 }" 10 }"
11 - autoresize  
12 - @mouseover="handleHighlight"  
13 - @mouseout="handleDownplay" 11 + autoresize
  12 + @mouseover="handleHighlight"
  13 + @mouseout="handleDownplay"
14 > 14 >
15 </v-chart> 15 </v-chart>
16 </template> 16 </template>
17 17
18 <script setup lang="ts"> 18 <script setup lang="ts">
19 -import { ref, nextTick, computed, watch, PropType, onMounted } from 'vue' 19 +import {ref, nextTick, computed, watch, PropType, onMounted} from 'vue'
20 import VChart from 'vue-echarts' 20 import VChart from 'vue-echarts'
21 -import { useCanvasInitOptions } from '@/hooks/useCanvasInitOptions.hook'  
22 -import { use } from 'echarts/core'  
23 -import { CanvasRenderer } from 'echarts/renderers'  
24 -import { BarChart } from 'echarts/charts'  
25 -import config, { includes, seriesItem } from './config'  
26 -import { mergeTheme } from '@/packages/public/chart'  
27 -import { useChartDataFetch } from '@/hooks'  
28 -import { CreateComponentType } from '@/packages/index.d'  
29 -import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'  
30 -import { isPreview } from '@/utils'  
31 -import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components' 21 +import {useCanvasInitOptions} from '@/hooks/useCanvasInitOptions.hook'
  22 +import {use} from 'echarts/core'
  23 +import {CanvasRenderer} from 'echarts/renderers'
  24 +import {BarChart} from 'echarts/charts'
  25 +import config, {includes, seriesItem} from './config'
  26 +import {mergeTheme} from '@/packages/public/chart'
  27 +import {useChartDataFetch} from '@/hooks'
  28 +import {CreateComponentType} from '@/packages/index.d'
  29 +import {useChartEditStore} from '@/store/modules/chartEditStore/chartEditStore'
  30 +import {isPreview} from '@/utils'
  31 +import {DatasetComponent, GridComponent, TooltipComponent, LegendComponent} from 'echarts/components'
32 import isObject from 'lodash/isObject' 32 import isObject from 'lodash/isObject'
33 import dataJson from './data.json' 33 import dataJson from './data.json'
34 -import { useFullScreen } from '../../utls/fullScreen' 34 +import {useFullScreen} from '../../utls/fullScreen'
35 35
36 const props = defineProps({ 36 const props = defineProps({
37 themeSetting: { 37 themeSetting: {
@@ -69,7 +69,7 @@ const toolBoxOption = { @@ -69,7 +69,7 @@ const toolBoxOption = {
69 onclick: () => { 69 onclick: () => {
70 const getEchartDom = vChartRef.value?.getDom() 70 const getEchartDom = vChartRef.value?.getDom()
71 const domName = document.getElementById(getEchartDom.id) as HTMLElement 71 const domName = document.getElementById(getEchartDom.id) as HTMLElement
72 - const htmlName = document.querySelector('html') as HTMLHtmlElement 72 + const htmlName = document.querySelector('html') as HTMLHtmlElement
73 useFullScreen(domName, htmlName) 73 useFullScreen(domName, htmlName)
74 } 74 }
75 } 75 }
@@ -77,33 +77,33 @@ const toolBoxOption = { @@ -77,33 +77,33 @@ const toolBoxOption = {
77 } 77 }
78 props.chartConfig.option = { 78 props.chartConfig.option = {
79 ...props.chartConfig.option, 79 ...props.chartConfig.option,
80 - ...{ toolbox: toolBoxOption } 80 + ...{toolbox: toolBoxOption}
81 } 81 }
82 82
83 // dataset 无法变更条数的补丁 83 // dataset 无法变更条数的补丁
84 watch( 84 watch(
85 - () => props.chartConfig.option.dataset,  
86 - (newData: { dimensions: any }, oldData) => {  
87 - try {  
88 - if (!isObject(newData) || !('dimensions' in newData)) return  
89 - if (Array.isArray(newData?.dimensions)) {  
90 - const seriesArr = []  
91 - for (let i = 0; i < newData.dimensions.length - 1; i++) {  
92 - seriesArr.push(seriesItem) 85 + () => props.chartConfig.option.dataset,
  86 + (newData: { dimensions: any }, oldData) => {
  87 + try {
  88 + if (!isObject(newData) || !('dimensions' in newData)) return
  89 + if (Array.isArray(newData?.dimensions)) {
  90 + const seriesArr = []
  91 + for (let i = 0; i < newData.dimensions.length - 1; i++) {
  92 + seriesArr.push(seriesItem)
  93 + }
  94 + replaceMergeArr.value = ['series']
  95 + props.chartConfig.option.series = seriesArr
  96 + nextTick(() => {
  97 + replaceMergeArr.value = []
  98 + })
93 } 99 }
94 - replaceMergeArr.value = ['series']  
95 - props.chartConfig.option.series = seriesArr  
96 - nextTick(() => {  
97 - replaceMergeArr.value = []  
98 - }) 100 + } catch (error) {
  101 + console.log(error)
99 } 102 }
100 - } catch (error) {  
101 - console.log(error) 103 + },
  104 + {
  105 + deep: false
102 } 106 }
103 - },  
104 - {  
105 - deep: false  
106 - }  
107 ) 107 )
108 108
109 let seriesDataNum = -1 109 let seriesDataNum = -1
@@ -167,20 +167,31 @@ const handleDownplay = () => { @@ -167,20 +167,31 @@ const handleDownplay = () => {
167 } 167 }
168 168
169 watch( 169 watch(
170 - () => props.chartConfig.option.isCarousel,  
171 - newData => {  
172 - if (newData) {  
173 - addPieInterval(undefined, true)  
174 - props.chartConfig.option.legend.show = false  
175 - } else {  
176 - props.chartConfig.option.legend.show = true  
177 - clearPieInterval() 170 + () => props.chartConfig.option.isCarousel,
  171 + newData => {
  172 + if (newData) {
  173 + addPieInterval(undefined, true)
  174 + props.chartConfig.option.legend.show = false
  175 + } else {
  176 + props.chartConfig.option.legend.show = true
  177 + clearPieInterval()
  178 + }
178 } 179 }
179 - }  
180 ) 180 )
181 181
182 -const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore, (newData: typeof dataJson) => { 182 +//fix 修复v-chart图表绑定联动组件视图不更新问题
  183 +const updateVChart = (newData: any) => {
  184 + if (!newData) return
  185 + vChartRef.value?.clear()
  186 + vChartRef.value?.setOption({
  187 + ...option.value,
  188 + dataset: newData
  189 + })
  190 +}
  191 +
  192 +const {vChartRef} = useChartDataFetch(props.chartConfig, useChartEditStore, (newData) => {
183 // addPieInterval(newData) 193 // addPieInterval(newData)
  194 + updateVChart(newData)
184 }) 195 })
185 196
186 onMounted(() => { 197 onMounted(() => {
@@ -68,7 +68,7 @@ const toolBoxOption = { @@ -68,7 +68,7 @@ const toolBoxOption = {
68 onclick: () => { 68 onclick: () => {
69 const getEchartDom = vChartRef.value?.getDom() 69 const getEchartDom = vChartRef.value?.getDom()
70 const domName = document.getElementById(getEchartDom.id) as HTMLElement 70 const domName = document.getElementById(getEchartDom.id) as HTMLElement
71 - const htmlName = document.querySelector('html') as HTMLHtmlElement 71 + const htmlName = document.querySelector('html') as HTMLHtmlElement
72 useFullScreen(domName, htmlName) 72 useFullScreen(domName, htmlName)
73 } 73 }
74 } 74 }
@@ -177,10 +177,22 @@ watch( @@ -177,10 +177,22 @@ watch(
177 } 177 }
178 ) 178 )
179 179
180 -const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore, (newData: typeof dataJson) => { 180 +//fix 修复v-chart图表绑定联动组件视图不更新问题
  181 +const updateVChart = (newData: any) => {
  182 + if (!newData) return
  183 + vChartRef.value?.clear()
  184 + vChartRef.value?.setOption({
  185 + ...option.value,
  186 + dataset: newData
  187 + })
  188 +}
  189 +
  190 +const {vChartRef} = useChartDataFetch(props.chartConfig, useChartEditStore, (newData) => {
181 // addPieInterval(newData) 191 // addPieInterval(newData)
  192 + updateVChart(newData)
182 }) 193 })
183 194
  195 +
184 onMounted(() => { 196 onMounted(() => {
185 seriesDataMaxLength = dataJson.source.length 197 seriesDataMaxLength = dataJson.source.length
186 if (props.chartConfig.option.isCarousel) { 198 if (props.chartConfig.option.isCarousel) {
@@ -62,7 +62,7 @@ const toolBoxOption = { @@ -62,7 +62,7 @@ const toolBoxOption = {
62 onclick: () => { 62 onclick: () => {
63 const getEchartDom = vChartRef.value?.getDom() 63 const getEchartDom = vChartRef.value?.getDom()
64 const domName = document.getElementById(getEchartDom.id) as HTMLElement 64 const domName = document.getElementById(getEchartDom.id) as HTMLElement
65 - const htmlName = document.querySelector('html') as HTMLHtmlElement 65 + const htmlName = document.querySelector('html') as HTMLHtmlElement
66 useFullScreen(domName, htmlName) 66 useFullScreen(domName, htmlName)
67 } 67 }
68 } 68 }
@@ -184,8 +184,19 @@ watch( @@ -184,8 +184,19 @@ watch(
184 } 184 }
185 ) 185 )
186 186
187 -const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore, (newData: typeof dataJson) => { 187 +//fix 修复v-chart图表绑定联动组件视图不更新问题
  188 +const updateVChart = (newData: any) => {
  189 + if (!newData) return
  190 + vChartRef.value?.clear()
  191 + vChartRef.value?.setOption({
  192 + ...option.value,
  193 + dataset: newData
  194 + })
  195 +}
  196 +
  197 +const {vChartRef} = useChartDataFetch(props.chartConfig, useChartEditStore, (newData) => {
188 // addPieInterval(newData) 198 // addPieInterval(newData)
  199 + updateVChart(newData)
189 }) 200 })
190 201
191 onMounted(() => { 202 onMounted(() => {
@@ -14,7 +14,7 @@ import { PropType, toRefs, ref, shallowReactive, watch } from 'vue' @@ -14,7 +14,7 @@ import { PropType, toRefs, ref, shallowReactive, watch } from 'vue'
14 import dayjs from 'dayjs' 14 import dayjs from 'dayjs'
15 import { CreateComponentType } from '@/packages/index.d' 15 import { CreateComponentType } from '@/packages/index.d'
16 import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' 16 import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
17 -import { useChartInteract } from '@/hooks' 17 +import { useChartInteract } from '@/hooks/external/useChartSelectInteract.hook'
18 import { InteractEventOn } from '@/enums/eventEnum' 18 import { InteractEventOn } from '@/enums/eventEnum'
19 import { ComponentInteractParamsEnum } from './interact' 19 import { ComponentInteractParamsEnum } from './interact'
20 20
@@ -50,9 +50,9 @@ const onChange = (v: number | number[]) => { @@ -50,9 +50,9 @@ const onChange = (v: number | number[]) => {
50 props.chartConfig, 50 props.chartConfig,
51 useChartEditStore, 51 useChartEditStore,
52 { 52 {
53 - [ComponentInteractParamsEnum.DATE_START]: v[0],  
54 - [ComponentInteractParamsEnum.DATE_END]: v[1],  
55 - [ComponentInteractParamsEnum.DATE_RANGE]: `${v[0]}-${v[1]}` 53 + [ComponentInteractParamsEnum.DATE_START]: v[0] || dayjs().valueOf(),
  54 + [ComponentInteractParamsEnum.DATE_END]: v[1] || dayjs().valueOf(),
  55 + [ComponentInteractParamsEnum.DATE_RANGE]: `${v[0] || dayjs().valueOf()}-${v[1] || dayjs().valueOf()}`
56 }, 56 },
57 InteractEventOn.CHANGE 57 InteractEventOn.CHANGE
58 ) 58 )
@@ -61,7 +61,7 @@ const onChange = (v: number | number[]) => { @@ -61,7 +61,7 @@ const onChange = (v: number | number[]) => {
61 useChartInteract( 61 useChartInteract(
62 props.chartConfig, 62 props.chartConfig,
63 useChartEditStore, 63 useChartEditStore,
64 - { [ComponentInteractParamsEnum.DATE]: v }, 64 + { [ComponentInteractParamsEnum.DATE]: v || dayjs().valueOf() },
65 InteractEventOn.CHANGE 65 InteractEventOn.CHANGE
66 ) 66 )
67 } 67 }
@@ -12,6 +12,6 @@ export const OverrideInputsTabConfig: ConfigType = { @@ -12,6 +12,6 @@ export const OverrideInputsTabConfig: ConfigType = {
12 category: ChatCategoryEnum.MORE, 12 category: ChatCategoryEnum.MORE,
13 categoryName: ChatCategoryEnumName.MORE, 13 categoryName: ChatCategoryEnumName.MORE,
14 package: PackagesCategoryEnum.INFORMATIONS, 14 package: PackagesCategoryEnum.INFORMATIONS,
15 - chartFrame: ChartFrameEnum.STATIC, 15 + chartFrame: ChartFrameEnum.COMMON,
16 image: 'inputs_tab.png' 16 image: 'inputs_tab.png'
17 } 17 }
@@ -10,7 +10,7 @@ export const option = { @@ -10,7 +10,7 @@ export const option = {
10 // 时间组件展示类型,必须和 interactActions 中定义的数据一致 10 // 时间组件展示类型,必须和 interactActions 中定义的数据一致
11 [COMPONENT_INTERACT_EVENT_KET]: ComponentInteractEventEnum.DATA, 11 [COMPONENT_INTERACT_EVENT_KET]: ComponentInteractEventEnum.DATA,
12 // 默认值 12 // 默认值
13 - selectValue: '1', 13 + selectValue: [],
14 // 暴露配置内容给用户 14 // 暴露配置内容给用户
15 dataset: [ 15 dataset: [
16 { 16 {
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 <collapse-item name="下拉配置" :expanded="true"> 2 <collapse-item name="下拉配置" :expanded="true">
3 <setting-item-box name="选择项" :alone="true"> 3 <setting-item-box name="选择项" :alone="true">
4 <SettingItem name="选择项"> 4 <SettingItem name="选择项">
5 - <n-select size="small" v-model:value="optionData.selectValue" :options="optionData.dataset" /> 5 + <n-select multiple size="small" v-model:value="optionData.selectValue" :options="optionData.dataset" />
6 </SettingItem> 6 </SettingItem>
7 </setting-item-box> 7 </setting-item-box>
8 <setting-item-box name="文字颜色" :alone="true"> 8 <setting-item-box name="文字颜色" :alone="true">
1 <template> 1 <template>
2 <n-select 2 <n-select
  3 + multiple
3 v-model:value="option.value.selectValue" 4 v-model:value="option.value.selectValue"
4 :options="option.value.dataset" 5 :options="option.value.dataset"
5 :style="`width:${w}px;`" 6 :style="`width:${w}px;`"
@@ -12,7 +13,7 @@ @@ -12,7 +13,7 @@
12 import { PropType, toRefs, shallowReactive, watch } from 'vue' 13 import { PropType, toRefs, shallowReactive, watch } from 'vue'
13 import { CreateComponentType } from '@/packages/index.d' 14 import { CreateComponentType } from '@/packages/index.d'
14 import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' 15 import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
15 -import { useChartInteract } from '@/hooks' 16 +import { useChartInteract } from '@/hooks/external/useChartSelectInteract.hook'
16 import { InteractEventOn } from '@/enums/eventEnum' 17 import { InteractEventOn } from '@/enums/eventEnum'
17 import { ComponentInteractParamsEnum } from './interact' 18 import { ComponentInteractParamsEnum } from './interact'
18 19
@@ -42,7 +43,7 @@ const option = shallowReactive({ @@ -42,7 +43,7 @@ const option = shallowReactive({
42 }) 43 })
43 44
44 // 监听事件改变 45 // 监听事件改变
45 -const onChange = (v: string) => { 46 +const onChange = (v: string[]) => {
46 // 存储到联动数据 47 // 存储到联动数据
47 useChartInteract( 48 useChartInteract(
48 props.chartConfig, 49 props.chartConfig,