perf: 处理渐变色和自定义色的交互

This commit is contained in:
奔跑的面条 2023-02-26 23:34:40 +08:00
parent 87386e69a3
commit 8b6c616a15
7 changed files with 84 additions and 34 deletions

View File

@ -15,7 +15,7 @@ import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore
import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index' import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components' import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
import { useChartDataFetch } from '@/hooks' import { useChartDataFetch } from '@/hooks'
import { isPreview } from '@/utils' import { isPreview, colorGradientCustomMerge} from '@/utils'
const props = defineProps({ const props = defineProps({
themeSetting: { themeSetting: {
@ -45,7 +45,9 @@ watch(
(newColor: keyof typeof chartColorsSearch) => { (newColor: keyof typeof chartColorsSearch) => {
try { try {
if (!isPreview()) { if (!isPreview()) {
const themeColor = chartColorsSearch[newColor] || chartColorsSearch[defaultTheme] const themeColor =
colorGradientCustomMerge(chartEditStore.getEditCanvasConfig.chartCustomThemeColorInfo)[newColor] ||
colorGradientCustomMerge(chartEditStore.getEditCanvasConfig.chartCustomThemeColorInfo)[defaultTheme]
props.chartConfig.option.series.forEach((value: any, index: number) => { props.chartConfig.option.series.forEach((value: any, index: number) => {
value.areaStyle.color = new graphic.LinearGradient(0, 0, 0, 1, [ value.areaStyle.color = new graphic.LinearGradient(0, 0, 0, 1, [
{ {

View File

@ -14,7 +14,7 @@ import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore
import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index' import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components' import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
import { useChartDataFetch } from '@/hooks' import { useChartDataFetch } from '@/hooks'
import { isPreview } from '@/utils' import { isPreview, colorGradientCustomMerge} from '@/utils'
const props = defineProps({ const props = defineProps({
themeSetting: { themeSetting: {
@ -44,7 +44,9 @@ watch(
(newColor: keyof typeof chartColorsSearch) => { (newColor: keyof typeof chartColorsSearch) => {
try { try {
if (!isPreview()) { if (!isPreview()) {
const themeColor = chartColorsSearch[newColor] || chartColorsSearch[defaultTheme] const themeColor =
colorGradientCustomMerge(chartEditStore.getEditCanvasConfig.chartCustomThemeColorInfo)[newColor] ||
colorGradientCustomMerge(chartEditStore.getEditCanvasConfig.chartCustomThemeColorInfo)[defaultTheme]
props.chartConfig.option.series.forEach((value: any, index: number) => { props.chartConfig.option.series.forEach((value: any, index: number) => {
value.areaStyle.color = new graphic.LinearGradient(0, 0, 0, 1, [ value.areaStyle.color = new graphic.LinearGradient(0, 0, 0, 1, [
{ {

View File

@ -15,7 +15,7 @@ import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore
import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index' import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components' import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
import { useChartDataFetch } from '@/hooks' import { useChartDataFetch } from '@/hooks'
import { isPreview } from '@/utils' import { isPreview, colorGradientCustomMerge } from '@/utils'
const props = defineProps({ const props = defineProps({
themeSetting: { themeSetting: {
@ -45,7 +45,9 @@ watch(
(newColor: keyof typeof chartColorsSearch) => { (newColor: keyof typeof chartColorsSearch) => {
try { try {
if (!isPreview()) { if (!isPreview()) {
const themeColor = chartColorsSearch[newColor] || chartColorsSearch[defaultTheme] const themeColor =
colorGradientCustomMerge(chartEditStore.getEditCanvasConfig.chartCustomThemeColorInfo)[newColor] ||
colorGradientCustomMerge(chartEditStore.getEditCanvasConfig.chartCustomThemeColorInfo)[defaultTheme]
props.chartConfig.option.series.forEach((value: any) => { props.chartConfig.option.series.forEach((value: any) => {
value.lineStyle.shadowColor = themeColor[2] value.lineStyle.shadowColor = themeColor[2]
value.lineStyle.color.colorStops.forEach((v: { color: string }, i: number) => { value.lineStyle.color.colorStops.forEach((v: { color: string }, i: number) => {

View File

@ -10,7 +10,7 @@ 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, isString, isNumber } from '@/utils' import { isPreview, isString, isNumber, colorGradientCustomMerge } 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'
import { useChartDataFetch } from '@/hooks' import { useChartDataFetch } from '@/hooks'
@ -44,7 +44,9 @@ watch(
(newColor: keyof typeof chartColorsSearch) => { (newColor: keyof typeof chartColorsSearch) => {
try { try {
if (!isPreview()) { if (!isPreview()) {
const themeColor = chartColorsSearch[newColor] || chartColorsSearch[defaultTheme] const themeColor =
colorGradientCustomMerge(chartEditStore.getEditCanvasConfig.chartCustomThemeColorInfo)[newColor] ||
colorGradientCustomMerge(chartEditStore.getEditCanvasConfig.chartCustomThemeColorInfo)[defaultTheme]
// //
props.chartConfig.option.series[0].backgroundStyle.color = themeColor[2] props.chartConfig.option.series[0].backgroundStyle.color = themeColor[2]
// //

View File

@ -2,7 +2,7 @@ import Color from 'color'
import { useDesignStore } from '@/store/modules/designStore/designStore' import { useDesignStore } from '@/store/modules/designStore/designStore'
import { PickCreateComponentType } from '@/packages/index.d' import { PickCreateComponentType } from '@/packages/index.d'
import { EditCanvasConfigType } from '@/store/modules/chartEditStore/chartEditStore.d' import { EditCanvasConfigType } from '@/store/modules/chartEditStore/chartEditStore.d'
import { chartColors, CustomColorsType } from '@/settings/chartThemes/index' import { chartColors, chartColorsSearch, CustomColorsType } from '@/settings/chartThemes/index'
type AttrType = PickCreateComponentType<'attr'> type AttrType = PickCreateComponentType<'attr'>
type StylesType = PickCreateComponentType<'styles'> type StylesType = PickCreateComponentType<'styles'>
@ -93,12 +93,12 @@ export function darken(color: string, concentration: number) {
* @returns * @returns
*/ */
export function hslToHexa(hslString: string): string { export function hslToHexa(hslString: string): string {
const color = Color(hslString); const color = Color(hslString)
return color.hexa() return color.hexa()
} }
export function hslToHex(hslString: string): string { export function hslToHex(hslString: string): string {
const color = Color(hslString); const color = Color(hslString)
return color.hex() return color.hex()
} }
@ -119,9 +119,9 @@ export const setHtmlTheme = (themeName?: string) => {
/** /**
* * * *
* @param chartDefaultColors * @param chartDefaultColors
* @param customColor * @param customColor
* @returns * @returns
*/ */
export const colorCustomMerge = (customColor?: CustomColorsType[]) => { export const colorCustomMerge = (customColor?: CustomColorsType[]) => {
type FormateCustomColorType = { type FormateCustomColorType = {
@ -138,4 +138,26 @@ export const colorCustomMerge = (customColor?: CustomColorsType[]) => {
} }
}) })
return { ...formateCustomColor, ...chartColors } return { ...formateCustomColor, ...chartColors }
} }
/**
* *
* @param customColor
*/
export const colorGradientCustomMerge = (customColor?: CustomColorsType[]) => {
type FormateGradientCustomColorType = {
[T: string]: string[]
}
const formateGradientCustomColor: FormateGradientCustomColorType = {}
customColor?.forEach(item => {
formateGradientCustomColor[item.id] = [
item.color[0],
item.color[1],
fade(item.color[0], 0.3),
fade(item.color[0], 0.5),
fade(item.color[1], 0.5)
]
})
return { ...formateGradientCustomColor, ...chartColorsSearch }
}

View File

@ -199,21 +199,34 @@ const createColor = () => {
// //
const deleteHandle = (index: number) => { const deleteHandle = (index: number) => {
goDialog({ const positiveHandle = () => {
message: `是否删除此颜色?`, colorList.splice(index, 1)
onPositiveCallback: () => { chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.CHART_CUSTOM_THEME_COLOR_INFO, cloneDeep(colorList))
colorList.splice(index, 1) nextTick(() => {
chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.CHART_CUSTOM_THEME_COLOR_INFO, cloneDeep(colorList)) if (index) {
nextTick(() => { selectHandle(colorList[index - 1])
if (index) { } else {
selectHandle(colorList[index - 1]) //
} else { selectColor.selectInfo = undefined
// }
selectColor.selectInfo = undefined })
} }
}) if (updateColor.value !== undefined) {
} goDialog({
}) message: '当前有变动未保存,是否直接放弃修改?',
onPositiveCallback: () => {
updateColor.value = undefined
positiveHandle()
}
})
} else {
goDialog({
message: `是否删除此颜色?`,
onPositiveCallback: () => {
positiveHandle()
}
})
}
} }
// //
@ -226,13 +239,17 @@ const saveHandle = () => {
if (!updateColor.value) return if (!updateColor.value) return
const index = colorList.findIndex(item => item.id === updateColor.value?.id) const index = colorList.findIndex(item => item.id === updateColor.value?.id)
if (index !== -1) { if (index !== -1) {
window.$message.success('颜色应用成功!')
const updateColorPrefix = cloneDeep({ ...updateColor.value, name: updateColor.value.name || '未定义' }) const updateColorPrefix = cloneDeep({ ...updateColor.value, name: updateColor.value.name || '未定义' })
colorList.splice(index, 1, updateColorPrefix) colorList.splice(index, 1, updateColorPrefix)
window.$message.success('颜色应用成功!')
updateColor.value = undefined updateColor.value = undefined
const selectTheme = chartEditStore.getEditCanvasConfig.chartThemeColor
//
chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.CHART_THEME_COLOR, 'dark')
// //
nextTick(() => { nextTick(() => {
chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.CHART_CUSTOM_THEME_COLOR_INFO, cloneDeep(colorList)) chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.CHART_CUSTOM_THEME_COLOR_INFO, cloneDeep(colorList))
chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.CHART_THEME_COLOR, selectTheme)
}) })
} else { } else {
window.$message.error('颜色应用失败!') window.$message.error('颜色应用失败!')

View File

@ -43,12 +43,14 @@ import { ChartEditStorageType } from '../../index.d'
import { PreviewRenderGroup } from '../PreviewRenderGroup/index' import { PreviewRenderGroup } from '../PreviewRenderGroup/index'
import { CreateComponentGroupType } from '@/packages/index.d' import { CreateComponentGroupType } from '@/packages/index.d'
import { chartColors } from '@/settings/chartThemes/index' import { chartColors } from '@/settings/chartThemes/index'
import { animationsClass, getFilterStyle, getTransformStyle, getBlendModeStyle } from '@/utils' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { animationsClass, getFilterStyle, getTransformStyle, getBlendModeStyle, colorCustomMerge } from '@/utils'
import { getSizeStyle, getComponentAttrStyle, getStatusStyle, getPreviewConfigStyle } from '../../utils' import { getSizeStyle, getComponentAttrStyle, getStatusStyle, getPreviewConfigStyle } from '../../utils'
import { useLifeHandler } from '@/hooks' import { useLifeHandler } from '@/hooks'
// //
const { initDataPond, clearMittDataPondMap } = useChartDataPondFetch() const { initDataPond, clearMittDataPondMap } = useChartDataPondFetch()
const chartEditStore = useChartEditStore()
const props = defineProps({ const props = defineProps({
localStorageInfo: { localStorageInfo: {
@ -63,10 +65,11 @@ const themeSetting = computed(() => {
return chartThemeSetting return chartThemeSetting
}) })
// //
const themeColor = computed(() => { const themeColor = computed(() => {
const chartThemeColor = props.localStorageInfo.editCanvasConfig.chartThemeColor const colorCustomMergeData = colorCustomMerge(props.localStorageInfo.editCanvasConfig.chartCustomThemeColorInfo)
return chartColors[chartThemeColor] return colorCustomMergeData[props.localStorageInfo.editCanvasConfig.chartThemeColor]
}) })
// //