fix:明暗主题随机颜色方法修复
This commit is contained in:
@@ -13,29 +13,41 @@ export function generateColorHEX(): string {
|
|||||||
* @returns rgb(24 144 255) / rgba(0,0,0,.85)
|
* @returns rgb(24 144 255) / rgba(0,0,0,.85)
|
||||||
*/
|
*/
|
||||||
export function generateColorRGBA(hasAlpha: boolean = false) {
|
export function generateColorRGBA(hasAlpha: boolean = false) {
|
||||||
let red:number;
|
const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
|
||||||
let green: number;
|
|
||||||
let blue:number;
|
|
||||||
red = Math.floor(Math.random() * 256)+100;//+100增加顏色亮度
|
|
||||||
green = Math.floor(Math.random() * 256)+100;
|
|
||||||
blue = Math.floor(Math.random() * 256)+100;
|
|
||||||
|
|
||||||
const brightChannel = Math.floor(Math.random() * 3);
|
let red: number;
|
||||||
switch (brightChannel) {
|
let green: number;
|
||||||
case 0:
|
let blue: number;
|
||||||
red = Math.min(255, red + 50);
|
|
||||||
break;
|
if (isDark) {
|
||||||
case 1:
|
// 暗色模式下生成较亮的颜色
|
||||||
green = Math.min(255, green + 50);
|
red = Math.floor(Math.random() * 156) + 100; // 100-255
|
||||||
break;
|
green = Math.floor(Math.random() * 156) + 100; // 100-255
|
||||||
case 2:
|
blue = Math.floor(Math.random() * 156) + 100; // 100-255
|
||||||
blue = Math.min(255, blue + 50);
|
|
||||||
break;
|
// 确保至少有一个通道的值较高,使颜色更明亮
|
||||||
|
const brightChannel = Math.floor(Math.random() * 3);
|
||||||
|
switch (brightChannel) {
|
||||||
|
case 0:
|
||||||
|
red = Math.min(255, red + 50);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
green = Math.min(255, green + 50);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
blue = Math.min(255, blue + 50);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 亮色模式下生成正常的颜色
|
||||||
|
red = Math.floor(Math.random() * 256); // 0-255
|
||||||
|
green = Math.floor(Math.random() * 256); // 0-255
|
||||||
|
blue = Math.floor(Math.random() * 256); // 0-255
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasAlpha) {
|
if (hasAlpha) {
|
||||||
const alpha = Math.floor(Math.random() * 100)+50;
|
const alpha = Math.floor(Math.random() * 100);
|
||||||
return `rgb(${red}, ${green}, ${blue}, 0.${alpha})`;
|
return `rgba(${red}, ${green}, ${blue}, 0.${alpha})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `rgb(${red}, ${green}, ${blue})`;
|
return `rgb(${red}, ${green}, ${blue})`;
|
||||||
|
|||||||
@@ -247,6 +247,7 @@ const fetchChartData = async () => {
|
|||||||
|
|
||||||
chartData.value = processChartData(allData);
|
chartData.value = processChartData(allData);
|
||||||
updateChart();
|
updateChart();
|
||||||
|
updateKpiStats();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch chart data:', error);
|
console.error('Failed to fetch chart data:', error);
|
||||||
message.error(t('common.getInfoFail'));
|
message.error(t('common.getInfoFail'));
|
||||||
@@ -276,11 +277,31 @@ const getSplitLineColor = () => {
|
|||||||
// 添加主题变化的观察器
|
// 添加主题变化的观察器
|
||||||
const themeObserver = new MutationObserver(() => {
|
const themeObserver = new MutationObserver(() => {
|
||||||
if (chart) {
|
if (chart) {
|
||||||
|
//清空颜色缓存
|
||||||
|
kpiColors.clear();
|
||||||
// 获取当前的主题色
|
// 获取当前的主题色
|
||||||
const splitLineColor = getSplitLineColor();
|
const splitLineColor = getSplitLineColor();
|
||||||
|
|
||||||
// 重新设置完整的图表配置并触发重新渲染
|
// 重新设置完整的图表配置并触发重新渲染
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
|
// 先生成所有新的颜色并存储
|
||||||
|
selectedKPIs.value.forEach(kpiId => {
|
||||||
|
const color = generateColorRGBA();
|
||||||
|
kpiColors.set(kpiId, color);
|
||||||
|
});
|
||||||
|
// 使用存储的颜色更新图表系列
|
||||||
|
const series = selectedKPIs.value.map(kpiId => {
|
||||||
|
const kpi = kpiColumns.value.find(col => col.kpiId === kpiId);
|
||||||
|
if (!kpi) return null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: kpi.title,
|
||||||
|
type: 'line',
|
||||||
|
data: chartData.value.map(item => item[kpiId] || 0),
|
||||||
|
itemStyle: { color: kpiColors.get(kpiId) },
|
||||||
|
...getSeriesConfig(),
|
||||||
|
};
|
||||||
|
}).filter(Boolean);
|
||||||
const option = {
|
const option = {
|
||||||
title: {
|
title: {
|
||||||
text: t('views.perfManage.kpiOverView.kpiChartTitle'),
|
text: t('views.perfManage.kpiOverView.kpiChartTitle'),
|
||||||
@@ -373,24 +394,17 @@ const themeObserver = new MutationObserver(() => {
|
|||||||
extraCssText: 'box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);'
|
extraCssText: 'box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);'
|
||||||
},
|
},
|
||||||
// 重新设置系列数据
|
// 重新设置系列数据
|
||||||
series: selectedKPIs.value.map(kpiId => {
|
series
|
||||||
const kpi = kpiColumns.value.find(col => col.kpiId === kpiId);
|
|
||||||
if (!kpi) return null;
|
|
||||||
const color = kpiColors.get(kpiId) || generateColorRGBA();
|
|
||||||
return {
|
|
||||||
name: kpi.title,
|
|
||||||
type: 'line',
|
|
||||||
data: chartData.value.map(item => item[kpiId] || 0),
|
|
||||||
itemStyle: { color },
|
|
||||||
...getSeriesConfig(),
|
|
||||||
};
|
|
||||||
}).filter(Boolean)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 使用新的配置更新图表
|
// 使用新的配置更新图表
|
||||||
chart!.setOption(option, true);
|
chart!.setOption(option, true);
|
||||||
// 强制重新渲染
|
// 强制重新渲染
|
||||||
chart!.resize();
|
chart!.resize();
|
||||||
|
// 更新统计数据
|
||||||
|
nextTick(() => {
|
||||||
|
updateKpiStats();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -407,14 +421,6 @@ const updateChart = () => {
|
|||||||
const color = kpiColors.get(kpiId)||generateColorRGBA();
|
const color = kpiColors.get(kpiId)||generateColorRGBA();
|
||||||
kpiColors.set(kpiId, color);
|
kpiColors.set(kpiId, color);
|
||||||
|
|
||||||
// 获取数据数组
|
|
||||||
const data = chartData.value.map((item, index) => {
|
|
||||||
const value = item[kpiId] || 0;
|
|
||||||
// 如果是最后一个数据点,添加特殊样式
|
|
||||||
|
|
||||||
return value;
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: kpi.title,
|
name: kpi.title,
|
||||||
type: 'line',
|
type: 'line',
|
||||||
@@ -743,10 +749,16 @@ interface KPIStats {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 添加计算属性,用于计算每个指标的最大值和最小值
|
// 添加计算属性,用于计算每个指标的最大值和最小值
|
||||||
const kpiStats = computed((): KPIStats[] => {
|
// 将 kpiStats 从计算属性改为响应式引用
|
||||||
if (!chartData.value.length || !kpiColumns.value.length) return [];
|
const kpiStats = ref<KPIStats[]>([]);
|
||||||
|
|
||||||
return selectedKPIs.value.map(kpiId => {
|
// 添加一个计算函数来更新统计数据
|
||||||
|
const updateKpiStats = () => {
|
||||||
|
if (!chartData.value.length || !kpiColumns.value.length) {
|
||||||
|
kpiStats.value = [];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
kpiStats.value = selectedKPIs.value.map(kpiId => {
|
||||||
// 找到对应的KPI标题
|
// 找到对应的KPI标题
|
||||||
const kpi = kpiColumns.value.find(col => col.kpiId === kpiId);
|
const kpi = kpiColumns.value.find(col => col.kpiId === kpiId);
|
||||||
if (!kpi) return null;
|
if (!kpi) return null;
|
||||||
@@ -767,7 +779,8 @@ const kpiStats = computed((): KPIStats[] => {
|
|||||||
avg:avg
|
avg:avg
|
||||||
};
|
};
|
||||||
}).filter((item): item is KPIStats => item !== null);
|
}).filter((item): item is KPIStats => item !== null);
|
||||||
});
|
};
|
||||||
|
|
||||||
|
|
||||||
// 添加表格列定义
|
// 添加表格列定义
|
||||||
const statsColumns: TableColumnType<KPIStats>[] = [
|
const statsColumns: TableColumnType<KPIStats>[] = [
|
||||||
@@ -776,9 +789,12 @@ const statsColumns: TableColumnType<KPIStats>[] = [
|
|||||||
key: 'icon',
|
key: 'icon',
|
||||||
width: 50,
|
width: 50,
|
||||||
customRender: ({ record }: { record: KPIStats }) => {
|
customRender: ({ record }: { record: KPIStats }) => {
|
||||||
|
// 获取当前主题下的颜色
|
||||||
|
// 直接使用 kpiColors 中存储的颜色
|
||||||
|
const color = kpiColors.get(record.kpiId);
|
||||||
return h(LineOutlined, {
|
return h(LineOutlined, {
|
||||||
style: {
|
style: {
|
||||||
color: kpiColors.get(record.kpiId) || '#000', // 使用与折线图相同的颜色
|
color: color || '#000', // 使用与折线图相同的颜色
|
||||||
fontSize: '30px', // 增大图标尺寸到30px
|
fontSize: '30px', // 增大图标尺寸到30px
|
||||||
fontWeight: 'bold', // 加粗
|
fontWeight: 'bold', // 加粗
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user