Commit a5063ef23dfb2d45b655a8596c856ce908b625a6
1 parent
fc088fa3
perf(src/packages): 重写图表里的饼图-环形,修改绑定数值小数点问题
Showing
5 changed files
with
253 additions
and
0 deletions
| 1 | +import { echartOptionProfixHandle, PublicConfigClass } from '@/packages/public' | |
| 2 | +import { OverridePieCircleConfig } from './index' | |
| 3 | +import { CreateComponentType } from '@/packages/index.d' | |
| 4 | +import cloneDeep from 'lodash/cloneDeep' | |
| 5 | + | |
| 6 | +export const includes = [] | |
| 7 | + | |
| 8 | +const option = { | |
| 9 | + tooltip: { | |
| 10 | + show: true, | |
| 11 | + trigger: 'item' | |
| 12 | + }, | |
| 13 | + legend: { | |
| 14 | + show: true, | |
| 15 | + }, | |
| 16 | + dataset: 0.25, | |
| 17 | + title: { | |
| 18 | + text: 25 + "%", | |
| 19 | + x: "center", | |
| 20 | + y: "center", | |
| 21 | + textStyle: { | |
| 22 | + color: "#56B9F8", | |
| 23 | + fontSize: 30 | |
| 24 | + } | |
| 25 | + }, | |
| 26 | + series: [ | |
| 27 | + { | |
| 28 | + type: "pie", | |
| 29 | + radius: ["75%", "80%"], | |
| 30 | + center: ["50%", "50%"], | |
| 31 | + hoverAnimation: true, | |
| 32 | + color: ["#00bcd44a", "transparent"], | |
| 33 | + label: { | |
| 34 | + show: false | |
| 35 | + }, | |
| 36 | + data: [ | |
| 37 | + { | |
| 38 | + value: [25], | |
| 39 | + itemStyle: { | |
| 40 | + color: "#03a9f4", | |
| 41 | + shadowBlur: 10, | |
| 42 | + shadowColor:"#97e2f5" | |
| 43 | + } | |
| 44 | + }, | |
| 45 | + { | |
| 46 | + value: [75], | |
| 47 | + itemStyle: { | |
| 48 | + color: "#00bcd44a", | |
| 49 | + shadowBlur: 0, | |
| 50 | + shadowColor:"#00bcd44a" | |
| 51 | + } | |
| 52 | + } | |
| 53 | + ] | |
| 54 | + }, | |
| 55 | + ] | |
| 56 | +} | |
| 57 | + | |
| 58 | +export default class Config extends PublicConfigClass implements CreateComponentType { | |
| 59 | + public key: string = OverridePieCircleConfig.key | |
| 60 | + | |
| 61 | + public chartConfig = cloneDeep(OverridePieCircleConfig) | |
| 62 | + | |
| 63 | + // 图表配置项 | |
| 64 | + public option = echartOptionProfixHandle(option, includes) | |
| 65 | +} | ... | ... | 
| 1 | +<template> | |
| 2 | + <!-- 遍历 seriesList --> | |
| 3 | + <CollapseItem v-for="(item, index) in config.series" :key="index" :name="`圆环`" :expanded="true"> | |
| 4 | + <SettingItemBox name="数据"> | |
| 5 | + <SettingItem name="数值"> | |
| 6 | + <n-input-number v-model:value="config.dataset" :min="0" :max="1" :step="0.01" size="small" placeholder="数值"> | |
| 7 | + </n-input-number> | |
| 8 | + </SettingItem> | |
| 9 | + </SettingItemBox> | |
| 10 | + <!-- Echarts 全局设置 --> | |
| 11 | + <SettingItemBox name="进度条"> | |
| 12 | + <SettingItem name="颜色"> | |
| 13 | + <n-color-picker size="small" :modes="['hex']" v-model:value="item.data[0].itemStyle.color"></n-color-picker> | |
| 14 | + </SettingItem> | |
| 15 | + <SettingItem name="阴影模糊等级"> | |
| 16 | + <n-input-number | |
| 17 | + v-model:value="item.data[0].itemStyle.shadowBlur" | |
| 18 | + :min="0" | |
| 19 | + :max="50" | |
| 20 | + :step="1" | |
| 21 | + size="small" | |
| 22 | + placeholder="阴影模糊等级" | |
| 23 | + > | |
| 24 | + </n-input-number> | |
| 25 | + </SettingItem> | |
| 26 | + <SettingItem name="阴影颜色"> | |
| 27 | + <n-color-picker | |
| 28 | + size="small" | |
| 29 | + :modes="['hex']" | |
| 30 | + v-model:value="item.data[0].itemStyle.shadowColor" | |
| 31 | + ></n-color-picker> | |
| 32 | + </SettingItem> | |
| 33 | + </SettingItemBox> | |
| 34 | + <!-- 中心标题 --> | |
| 35 | + <SettingItemBox v-if="config.title" name="标题"> | |
| 36 | + <SettingItem name="颜色"> | |
| 37 | + <n-color-picker size="small" :modes="['hex']" v-model:value="config.title.textStyle.color"></n-color-picker> | |
| 38 | + </SettingItem> | |
| 39 | + <SettingItem name="字体大小"> | |
| 40 | + <n-input-number | |
| 41 | + v-model:value="config.title.textStyle.fontSize" | |
| 42 | + :min="0" | |
| 43 | + :step="1" | |
| 44 | + size="small" | |
| 45 | + placeholder="字体大小" | |
| 46 | + > | |
| 47 | + </n-input-number> | |
| 48 | + </SettingItem> | |
| 49 | + </SettingItemBox> | |
| 50 | + <!-- 其他样式 --> | |
| 51 | + <SettingItemBox name="轨道样式"> | |
| 52 | + <SettingItem name="颜色"> | |
| 53 | + <n-color-picker size="small" :modes="['hex']" v-model:value="item.data[1].itemStyle.color"></n-color-picker> | |
| 54 | + </SettingItem> | |
| 55 | + <SettingItem name="阴影模糊等级"> | |
| 56 | + <n-input-number | |
| 57 | + v-model:value="item.data[1].itemStyle.shadowBlur" | |
| 58 | + :min="0" | |
| 59 | + :step="1" | |
| 60 | + size="small" | |
| 61 | + placeholder="阴影模糊等级" | |
| 62 | + > | |
| 63 | + </n-input-number> | |
| 64 | + </SettingItem> | |
| 65 | + <SettingItem name="阴影颜色"> | |
| 66 | + <n-color-picker | |
| 67 | + size="small" | |
| 68 | + :modes="['hex']" | |
| 69 | + v-model:value="item.data[1].itemStyle.shadowColor" | |
| 70 | + ></n-color-picker> | |
| 71 | + </SettingItem> | |
| 72 | + </SettingItemBox> | |
| 73 | + </CollapseItem> | |
| 74 | +</template> | |
| 75 | + | |
| 76 | +<script setup lang="ts"> | |
| 77 | +import { PropType, computed } from 'vue' | |
| 78 | +// 以下是封装的设置模块布局组件,具体效果可在官网查看 | |
| 79 | +import { CollapseItem, SettingItemBox, SettingItem } from '@/components/Pages/ChartItemSetting' | |
| 80 | +import { GlobalThemeJsonType } from '@/settings/chartThemes' | |
| 81 | + | |
| 82 | +const props = defineProps({ | |
| 83 | + optionData: { | |
| 84 | + type: Object as PropType<GlobalThemeJsonType>, | |
| 85 | + required: true | |
| 86 | + } | |
| 87 | +}) | |
| 88 | + | |
| 89 | +const config = computed(() => { | |
| 90 | + return props.optionData | |
| 91 | +}) | |
| 92 | +</script> | ... | ... | 
| 1 | +import { ConfigType, PackagesCategoryEnum, ChartFrameEnum } from '@/packages/index.d' | |
| 2 | +import { ChatCategoryEnum, ChatCategoryEnumName } from '@/packages/components/Charts/index.d' | |
| 3 | +import { useWidgetKey } from '@/packages/external/useWidgetKey' | |
| 4 | + | |
| 5 | +const { key, conKey, chartKey } = useWidgetKey('OverridePieCircle', true) | |
| 6 | + | |
| 7 | +export const OverridePieCircleConfig: ConfigType = { | |
| 8 | + key, | |
| 9 | + chartKey, | |
| 10 | + conKey, | |
| 11 | + title: '自定义饼图-环形', | |
| 12 | + category: ChatCategoryEnum.PIE, | |
| 13 | + categoryName: ChatCategoryEnumName.PIE, | |
| 14 | + package: PackagesCategoryEnum.CHARTS, | |
| 15 | + chartFrame: ChartFrameEnum.ECHARTS, | |
| 16 | + image: 'pie-circle.png' | |
| 17 | +} | ... | ... | 
| 1 | +<template> | |
| 2 | + <v-chart :theme="themeColor" :init-options="initOptions" :option="option.value" autoresize> </v-chart> | |
| 3 | +</template> | |
| 4 | + | |
| 5 | +<script setup lang="ts"> | |
| 6 | +import { PropType, reactive, watch } from 'vue' | |
| 7 | +import VChart from 'vue-echarts' | |
| 8 | +import { useCanvasInitOptions } from '@/hooks/useCanvasInitOptions.hook' | |
| 9 | +import { use } from 'echarts/core' | |
| 10 | +import { CanvasRenderer } from 'echarts/renderers' | |
| 11 | +import { PieChart } from 'echarts/charts' | |
| 12 | +import { mergeTheme } from '@/packages/public/chart' | |
| 13 | +import config, { includes } from './config' | |
| 14 | +import { useChartDataFetch } from '@/hooks' | |
| 15 | +import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' | |
| 16 | +import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent, TitleComponent } from 'echarts/components' | |
| 17 | + | |
| 18 | +const props = defineProps({ | |
| 19 | + themeSetting: { | |
| 20 | + type: Object, | |
| 21 | + required: true | |
| 22 | + }, | |
| 23 | + themeColor: { | |
| 24 | + type: Object, | |
| 25 | + required: true | |
| 26 | + }, | |
| 27 | + chartConfig: { | |
| 28 | + type: Object as PropType<config>, | |
| 29 | + required: true | |
| 30 | + } | |
| 31 | +}) | |
| 32 | + | |
| 33 | +const initOptions = useCanvasInitOptions(props.chartConfig.option, props.themeSetting) | |
| 34 | + | |
| 35 | +use([DatasetComponent, CanvasRenderer, PieChart, GridComponent, TooltipComponent, LegendComponent, TitleComponent]) | |
| 36 | + | |
| 37 | +const option = reactive({ | |
| 38 | + value: {} | |
| 39 | +}) | |
| 40 | + | |
| 41 | +const dataHandle = (newData: any) => { | |
| 42 | + const d = parseFloat(`${newData}`) * 100 | |
| 43 | + let config = props.chartConfig.option | |
| 44 | + config.title.text = d.toFixed(0) + '%' | |
| 45 | + config.series[0].data[0].value[0] = d | |
| 46 | + config.series[0].data[1].value[0] = 100 - d | |
| 47 | + option.value = mergeTheme(props.chartConfig.option, props.themeSetting, includes) | |
| 48 | + option.value = props.chartConfig.option | |
| 49 | +} | |
| 50 | + | |
| 51 | +// 配置时 | |
| 52 | +watch( | |
| 53 | + () => props.chartConfig.option.dataset, | |
| 54 | + newData => { | |
| 55 | + try { | |
| 56 | + dataHandle(newData) | |
| 57 | + } catch (error) { | |
| 58 | + console.log(error) | |
| 59 | + } | |
| 60 | + }, | |
| 61 | + { | |
| 62 | + immediate: true, | |
| 63 | + deep: false | |
| 64 | + } | |
| 65 | +) | |
| 66 | + | |
| 67 | +// 预览时 | |
| 68 | +useChartDataFetch(props.chartConfig, useChartEditStore, (resData: number) => { | |
| 69 | + let d = parseFloat(`${resData}`) * 100 | |
| 70 | + // @ts-ignore | |
| 71 | + option.value.title.text = d.toFixed(0) + '%' | |
| 72 | + // @ts-ignore | |
| 73 | + option.value.series[0].data[0].value[0] = d | |
| 74 | + // @ts-ignore | |
| 75 | + option.value.series[0].data[1].value[0] = 100 - d | |
| 76 | +}) | |
| 77 | +</script> | ... | ... | 
| ... | ... | @@ -17,6 +17,7 @@ import { OverrideBarCommonConfig } from '@/packages/components/external/Charts/B | 
| 17 | 17 | import { OverrideLineCommonConfig } from '@/packages/components/external/Charts/Lines/OverrideLineCommon' | 
| 18 | 18 | import { OverrideLineGradientsConfig } from '@/packages/components/external/Charts/Lines/OverrideLineGradients' | 
| 19 | 19 | import { OverrideProcessConfig } from '@/packages/components/external/Charts/Mores/OverrideProcess' | 
| 20 | +import { OverridePieCircleConfig } from '@/packages/components/external/Charts/Pies/OverridePieCircle' | |
| 20 | 21 | |
| 21 | 22 | export function useInjectLib(packagesList: EPackagesType) { | 
| 22 | 23 | packagesList[EPackagesCategoryEnum.COMPOSES] = ComposesList | 
| ... | ... | @@ -37,6 +38,7 @@ export function useInjectLib(packagesList: EPackagesType) { | 
| 37 | 38 | addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.CHARTS, OverrideLineCommonConfig) | 
| 38 | 39 | addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.CHARTS, OverrideLineGradientsConfig) | 
| 39 | 40 | addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.CHARTS, OverrideProcessConfig) | 
| 41 | + addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.CHARTS, OverridePieCircleConfig) | |
| 40 | 42 | } | 
| 41 | 43 | |
| 42 | 44 | /** | ... | ... |