fix: 解决组件数据无法更新的问题

This commit is contained in:
奔跑的面条 2022-06-27 20:26:24 +08:00
parent 3ad3d8d9a0
commit 2d05e8054c
12 changed files with 133 additions and 92 deletions

View File

@ -4,11 +4,12 @@ import { RequestHttpEnum } from '@/enums/httpEnum'
// 单个X数据 // 单个X数据
export const chartDataUrl = '/mock/chartData' export const chartDataUrl = '/mock/chartData'
export const rankListUrl = '/mock/rankList'
export const numberFloatUrl = '/mock/number/float' export const numberFloatUrl = '/mock/number/float'
export const numberIntUrl = '/mock/number/int' export const numberIntUrl = '/mock/number/int'
export const textUrl = '/mock/text' export const textUrl = '/mock/text'
export const imageUrl = '/mock/image' export const imageUrl = '/mock/image'
export const rankListUrl = '/mock/rankList'
export const scrollBoardUrl = '/mock/scrollBoard'
const mockObject: MockMethod[] = [ const mockObject: MockMethod[] = [
{ {
@ -16,33 +17,38 @@ const mockObject: MockMethod[] = [
// url: /\/mock\/mockData(|\?\S*)$/, // url: /\/mock\/mockData(|\?\S*)$/,
url: chartDataUrl, url: chartDataUrl,
method: RequestHttpEnum.GET, method: RequestHttpEnum.GET,
response: () => test.fetchMockData, response: () => test.fetchMockData
},
{
url: rankListUrl,
method: RequestHttpEnum.GET,
response: () => test.fetchRankList,
}, },
{ {
url: numberFloatUrl, url: numberFloatUrl,
method: RequestHttpEnum.GET, method: RequestHttpEnum.GET,
response: () => test.fetchNumberFloat, response: () => test.fetchNumberFloat
}, },
{ {
url: numberIntUrl, url: numberIntUrl,
method: RequestHttpEnum.GET, method: RequestHttpEnum.GET,
response: () => test.fetchNumberInt, response: () => test.fetchNumberInt
}, },
{ {
url: textUrl, url: textUrl,
method: RequestHttpEnum.GET, method: RequestHttpEnum.GET,
response: () => test.fetchText, response: () => test.fetchText
}, },
{ {
url: imageUrl, url: imageUrl,
method: RequestHttpEnum.GET, method: RequestHttpEnum.GET,
response: () => test.fetchImage(Math.round(Math.random()*10)), response: () => test.fetchImage(Math.round(Math.random() * 10))
}, },
{
url: rankListUrl,
method: RequestHttpEnum.GET,
response: () => test.fetchRankList
},
{
url: scrollBoardUrl,
method: RequestHttpEnum.GET,
response: () => test.fetchScrollBoard
}
] ]
export default mockObject export default mockObject

View File

@ -10,35 +10,35 @@ export default {
{ {
product: '@name', product: '@name',
'dataOne|100-900': 3, 'dataOne|100-900': 3,
'dataTwo|100-900': 3, 'dataTwo|100-900': 3
}, },
{ {
product: '@name', product: '@name',
'dataOne|100-900': 3, 'dataOne|100-900': 3,
'dataTwo|100-900': 3, 'dataTwo|100-900': 3
}, },
{ {
product: '@name', product: '@name',
'dataOne|100-900': 3, 'dataOne|100-900': 3,
'dataTwo|100-900': 3, 'dataTwo|100-900': 3
}, },
{ {
product: '@name', product: '@name',
'dataOne|100-900': 3, 'dataOne|100-900': 3,
'dataTwo|100-900': 3, 'dataTwo|100-900': 3
}, },
{ {
product: '@name', product: '@name',
'dataOne|100-900': 3, 'dataOne|100-900': 3,
'dataTwo|100-900': 3, 'dataTwo|100-900': 3
}, },
{ {
product: '@name', product: '@name',
'dataOne|100-900': 3, 'dataOne|100-900': 3,
'dataTwo|100-900': 3, 'dataTwo|100-900': 3
}, }
], ]
}, }
}, },
// 排名列表 // 排名列表
fetchRankList: { fetchRankList: {
@ -58,32 +58,50 @@ export default {
{ name: '@name', 'value|100-900': 5 }, { name: '@name', 'value|100-900': 5 },
{ name: '@name', 'value|100-900': 5 }, { name: '@name', 'value|100-900': 5 },
{ name: '@name', 'value|100-900': 5 }, { name: '@name', 'value|100-900': 5 },
{ name: '@name', 'value|100-900': 5 }, { name: '@name', 'value|100-900': 5 }
], ]
},
// 轮播表格
fetchScrollBoard: {
code: 0,
status: 200,
msg: '请求成功',
data: [
['行1列1', '行1列2', '1'],
['行2列1', '行2列2', '2'],
['行3列1', '行3列2', '3'],
['行4列1', '行4列2', '4'],
['行5列1', '行5列2', '5'],
['行6列1', '行6列2', '6'],
['行7列1', '行7列2', '行7列3'],
['行8列1', '行8列2', '行8列3'],
['行9列1', '行9列2', '行9列3'],
['行10列1', '行10列2', '行10列3']
]
}, },
// 获取数字 // 获取数字
fetchNumberFloat: { fetchNumberFloat: {
code: 0, code: 0,
status: 200, status: 200,
msg: '请求成功', msg: '请求成功',
data: '@float(0, 0.99, 1, 2)', data: '@float(0, 0.99, 1, 4)'
}, },
fetchNumberInt: { fetchNumberInt: {
code: 0, code: 0,
status: 200, status: 200,
msg: '请求成功', msg: '请求成功',
data: '@integer(0, 100)', data: '@integer(0, 100)'
}, },
fetchText: { fetchText: {
code: 0, code: 0,
status: 200, status: 200,
msg: '请求成功', msg: '请求成功',
data: '@paragraph(1, 10)', data: '@paragraph(1, 10)'
}, },
fetchImage: (num: number) => ({ fetchImage: (num: number) => ({
code: 0, code: 0,
status: 200, status: 200,
msg: '请求成功', msg: '请求成功',
data: `https://robohash.org/${num}`, data: `https://robohash.org/${num}`
}), })
} }

View File

@ -3,7 +3,7 @@
:type="type" :type="type"
:height="h" :height="h"
:processing="processing" :processing="processing"
:percentage="dataset" :percentage="option.dataset"
:indicator-placement="indicatorPlacement" :indicator-placement="indicatorPlacement"
:color="color" :color="color"
:rail-color="railColor" :rail-color="railColor"
@ -15,16 +15,16 @@
fontSize: `${indicatorTextSize}px` fontSize: `${indicatorTextSize}px`
}" }"
> >
{{dataset}} {{unit}} {{option.dataset}} {{unit}}
</n-text> </n-text>
</n-progress> </n-progress>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { PropType, toRefs, watch } from 'vue' import { PropType, toRefs, watch, shallowReactive } from 'vue'
import { useChartDataFetch } from '@/hooks' import { useChartDataFetch } from '@/hooks'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import config from './config' import config, { option as configOption } from './config'
const props = defineProps({ const props = defineProps({
chartConfig: { chartConfig: {
@ -49,5 +49,19 @@ const {
dataset dataset
} = toRefs(props.chartConfig.option) } = toRefs(props.chartConfig.option)
useChartDataFetch(props.chartConfig, useChartEditStore) const option = shallowReactive({
dataset: configOption.dataset
})
//
watch(
() => props.chartConfig.option.dataset,
(newData: any) => {
option.dataset = newData
}
)
//
useChartDataFetch(props.chartConfig, useChartEditStore, (newData: number) => {
option.dataset = newData
})
</script> </script>

View File

@ -1,11 +1,5 @@
<template> <template>
<v-chart <v-chart :theme="themeColor" :option="option.value" autoresize></v-chart>
ref="vChartRef"
:theme="themeColor"
:option="option.value"
:manual-update="isPreview()"
autoresize
></v-chart>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -15,7 +9,7 @@ import { use } from 'echarts/core'
import 'echarts-liquidfill/src/liquidFill.js' import 'echarts-liquidfill/src/liquidFill.js'
import { CanvasRenderer } from 'echarts/renderers' import { CanvasRenderer } from 'echarts/renderers'
import { GridComponent } from 'echarts/components' import { GridComponent } from 'echarts/components'
import config from './config' import config from './config'
import { isPreview } from '@/utils' import { isPreview } from '@/utils'
import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index' import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
@ -24,16 +18,16 @@ import { useChartDataFetch } from '@/hooks'
const props = defineProps({ const props = defineProps({
themeSetting: { themeSetting: {
type: Object, type: Object,
required: true, required: true
}, },
themeColor: { themeColor: {
type: Object, type: Object,
required: true, required: true
}, },
chartConfig: { chartConfig: {
type: Object as PropType<config>, type: Object as PropType<config>,
required: true, required: true
}, }
}) })
use([CanvasRenderer, GridComponent]) use([CanvasRenderer, GridComponent])
@ -41,7 +35,7 @@ use([CanvasRenderer, GridComponent])
const chartEditStore = useChartEditStore() const chartEditStore = useChartEditStore()
const option = reactive({ const option = reactive({
value: {}, value: {}
}) })
// //
@ -53,36 +47,45 @@ watch(
// //
props.chartConfig.option.series[0].backgroundStyle.color = themeColor[2] props.chartConfig.option.series[0].backgroundStyle.color = themeColor[2]
// //
props.chartConfig.option.series[0].color[0].colorStops = [ props.chartConfig.option.series[0].color[0].colorStops = [
{ {
offset: 0, offset: 0,
color: themeColor[0], color: themeColor[0]
}, },
{ {
offset: 1, offset: 1,
color: themeColor[1], color: themeColor[1]
}, }
] ]
} }
option.value = props.chartConfig.option option.value = props.chartConfig.option
}, },
{ {
immediate: true, immediate: true
} }
) )
const updateDataset = (newData: string | number) => { //
props.chartConfig.option.series[0].data = [parseFloat(`${newData}`).toFixed(2)] const dataHandle = (newData: number) => {
option.value = props.chartConfig.option return parseFloat(newData.toFixed(2))
} }
//
watch( watch(
() => props.chartConfig.option.dataset, () => props.chartConfig.option.dataset,
newData => updateDataset(newData), newData => {
console.log(dataHandle(newData))
props.chartConfig.option.series[0].data = [dataHandle(newData)]
option.value = props.chartConfig.option
},
{ {
immediate: true, immediate: true
} }
) )
const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore) //
useChartDataFetch(props.chartConfig, useChartEditStore, (newData: number) => {
// @ts-ignore
option.value.series[0].data = [dataHandle(newData)]
})
</script> </script>

View File

@ -48,9 +48,7 @@ const dataHandle = (newData: any) => {
// //
watch( watch(
() => props.chartConfig.option.dataset, () => props.chartConfig.option.dataset,
newData => { newData => dataHandle(newData),
dataHandle(newData)
},
{ {
immediate: true immediate: true
} }

View File

@ -4,12 +4,13 @@ import { NumberConfig } from './index'
import cloneDeep from 'lodash/cloneDeep' import cloneDeep from 'lodash/cloneDeep'
export const option = { export const option = {
from: 50000, // 数据说明
to: 100000, dataset: 100000,
from: 0,
dur: 3, dur: 3,
precision: 0, precision: 0,
showSeparator: true, showSeparator: true,
numberSize: 24, numberSize: 34,
numberColor: '#4a9ef8', numberColor: '#4a9ef8',
prefixText: '¥', prefixText: '¥',
prefixColor: '#4a9ef8', prefixColor: '#4a9ef8',

View File

@ -1,13 +1,6 @@
<template> <template>
<CollapseItem name="内容" :expanded="true"> <CollapseItem name="内容" :expanded="true">
<SettingItemBox name="数值"> <SettingItemBox name="数值">
<SettingItem name="起始值">
<n-input-number
v-model:value="optionData.from"
size="small"
:min="0"
></n-input-number>
</SettingItem>
<SettingItem name="终点值"> <SettingItem name="终点值">
<n-input-number <n-input-number
v-model:value="optionData.to" v-model:value="optionData.to"
@ -22,12 +15,6 @@
:min="1" :min="1"
></n-input-number> ></n-input-number>
</SettingItem> </SettingItem>
<SettingItem>
<n-space>
<n-switch v-model:value="optionData.showSeparator" size="small" />
<n-text>展示分割符</n-text>
</n-space>
</SettingItem>
<SettingItem name="精度"> <SettingItem name="精度">
<n-input-number <n-input-number
v-model:value="optionData.precision" v-model:value="optionData.precision"
@ -35,6 +22,12 @@
:min="0" :min="0"
></n-input-number> ></n-input-number>
</SettingItem> </SettingItem>
<SettingItem>
<n-space>
<n-switch v-model:value="optionData.showSeparator" size="small" />
<n-text>展示分割符</n-text>
</n-space>
</SettingItem>
</SettingItemBox> </SettingItemBox>
<SettingItemBox name="数值"> <SettingItemBox name="数值">

View File

@ -6,7 +6,7 @@
</span> </span>
</template> </template>
<span :style="`color:${numberColor};font-size:${numberSize}px`"> <span :style="`color:${numberColor};font-size:${numberSize}px`">
<n-number-animation :from="option.from" :to="option.to" :duration="dur * 1000" :show-separator="showSeparator" <n-number-animation :from="option.from" :to="option.dataset" :duration="dur * 1000" :show-separator="showSeparator"
:precision="precision"></n-number-animation> :precision="precision"></n-number-animation>
</span> </span>
<template #suffix> <template #suffix>
@ -31,7 +31,7 @@ const props = defineProps({
}) })
const option = reactive({ const option = reactive({
from: 0, from: 0,
to: 0, dataset: 0,
}) })
const { w, h } = toRefs(props.chartConfig.attr) const { w, h } = toRefs(props.chartConfig.attr)
let { let {
@ -48,8 +48,8 @@ let {
const updateNumber = (newData: number) => { const updateNumber = (newData: number) => {
// //
option.from = option.to option.from = option.dataset
option.to = newData option.dataset = newData
} }
watch( watch(
@ -60,14 +60,15 @@ watch(
) )
watch( watch(
() => props.chartConfig.option.to, () => props.chartConfig.option.dataset,
() => { () => {
option.to = props.chartConfig.option.to option.dataset = props.chartConfig.option.dataset
}, { immediate: true } }, { immediate: true }
) )
useChartDataFetch(props.chartConfig, useChartEditStore, updateNumber) useChartDataFetch(props.chartConfig, useChartEditStore, updateNumber)
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@include go('decorates-number') { @include go('decorates-number') {
display: flex; display: flex;

View File

@ -24,6 +24,7 @@ import { PropType, toRefs, shallowReactive, watch } from 'vue'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { useChartDataFetch } from '@/hooks' import { useChartDataFetch } from '@/hooks'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { option as configOption } from './config'
const props = defineProps({ const props = defineProps({
chartConfig: { chartConfig: {
@ -48,7 +49,7 @@ const {
} = toRefs(props.chartConfig.option) } = toRefs(props.chartConfig.option)
const option = shallowReactive({ const option = shallowReactive({
dataset: '' dataset: configOption.dataset
}) })
// //
@ -62,7 +63,7 @@ watch(
) )
// //
useChartDataFetch(props.chartConfig, useChartEditStore, (newData: any) => { useChartDataFetch(props.chartConfig, useChartEditStore, (newData: string) => {
option.dataset = newData option.dataset = newData
}) })
</script> </script>

View File

@ -10,6 +10,7 @@ import { PropType, toRefs, shallowReactive, watch } from 'vue'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { useChartDataFetch } from '@/hooks' import { useChartDataFetch } from '@/hooks'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { option as configOption } from './config'
const props = defineProps({ const props = defineProps({
chartConfig: { chartConfig: {
@ -19,7 +20,7 @@ const props = defineProps({
}) })
const option = shallowReactive({ const option = shallowReactive({
dataset: '' dataset: configOption.dataset
}) })
const { w, h } = toRefs(props.chartConfig.attr) const { w, h } = toRefs(props.chartConfig.attr)
@ -30,8 +31,6 @@ watch(
() => props.chartConfig.option.dataset, () => props.chartConfig.option.dataset,
(newData: any) => { (newData: any) => {
option.dataset = newData option.dataset = newData
}, {
immediate: true
} }
) )

View File

@ -308,7 +308,10 @@ watch(
) )
// ( dataset) // ( dataset)
useChartDataFetch(props.chartConfig, useChartEditStore) useChartDataFetch(props.chartConfig, useChartEditStore, (resData: any[]) => {
props.chartConfig.option.dataset = resData
onRestart()
})
onUnmounted(() => { onUnmounted(() => {
stopAnimation() stopAnimation()

View File

@ -62,7 +62,7 @@ import { ref, toRefs } from 'vue'
import { icon } from '@/plugins' import { icon } from '@/plugins'
import { SettingItemBox } from '@/components/Pages/ChartItemSetting' import { SettingItemBox } from '@/components/Pages/ChartItemSetting'
import { RequestHttpEnum, ResultEnum } from '@/enums/httpEnum' import { RequestHttpEnum, ResultEnum } from '@/enums/httpEnum'
import { chartDataUrl, rankListUrl, numberFloatUrl, numberIntUrl, textUrl, imageUrl } from '@/api/mock' import { chartDataUrl, rankListUrl, scrollBoardUrl, numberFloatUrl, numberIntUrl, textUrl, imageUrl } from '@/api/mock'
import { http } from '@/api/http' import { http } from '@/api/http'
import { SelectHttpType } from '../../index.d' import { SelectHttpType } from '../../index.d'
import { ChartDataMatchingAndShow } from '../ChartDataMatchingAndShow' import { ChartDataMatchingAndShow } from '../ChartDataMatchingAndShow'
@ -81,9 +81,6 @@ const apiList = [
{ {
value: `【图表】${ chartDataUrl }` value: `【图表】${ chartDataUrl }`
}, },
{
value: `【表格】${ rankListUrl }`
},
{ {
value: `【文本】${ textUrl }` value: `【文本】${ textUrl }`
}, },
@ -95,7 +92,14 @@ const apiList = [
}, },
{ {
value: `【图片地址】${ imageUrl }` value: `【图片地址】${ imageUrl }`
} },
{
value: `【排名列表】${ rankListUrl }`
},
{
value: `【滚动表格】${ scrollBoardUrl }`
},
] ]
// //