Commit bd94bc7c4b3b0ec5ceda1be418fa4c775279d686
1 parent
ba1c0e09
feat(src/packages): 修复select和时间联动组件联动图表dataset视图未更新问题
Showing
6 changed files
with
149 additions
and
59 deletions
| 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 |
| @@ -13,7 +13,7 @@ | @@ -13,7 +13,7 @@ | ||
| 13 | import { PropType, toRefs, shallowReactive, watch } from 'vue' | 13 | import { PropType, toRefs, shallowReactive, watch } from 'vue' |
| 14 | import { CreateComponentType } from '@/packages/index.d' | 14 | import { CreateComponentType } from '@/packages/index.d' |
| 15 | import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' | 15 | import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' |
| 16 | -import { useChartInteract } from '@/hooks' | 16 | +import { useChartInteract } from '@/hooks/external/useChartSelectInteract.hook' |
| 17 | import { InteractEventOn } from '@/enums/eventEnum' | 17 | import { InteractEventOn } from '@/enums/eventEnum' |
| 18 | import { ComponentInteractParamsEnum } from './interact' | 18 | import { ComponentInteractParamsEnum } from './interact' |
| 19 | 19 |