Echarts 颜色重复问题

默认 Echarts 仅有 9 种颜色,如果超过则会出现重复,导致用户体验不是很好。

现在提供一种解决思路。超过则使用随机颜色填充:

ts 复制代码
function generateColors(count: number) {
  const defaultColors = [
    '#5D82DB',
    '#73D13D',
    '#F7B500',
    '#FA541C',
    '#FD8D3C',
    '#999999',
    '#73c0de', // 浅蓝
    '#3ba272', // 深绿
    '#9a60b4', // 紫色
    '#ea7ccc', // 粉色
  ]

  if (count <= defaultColors.length) {
    return defaultColors
  }

  // 否则随机生成 count - defaultColors.length 个颜色
  const colors = [...defaultColors]
  
  for (let i = defaultColors.length; i < count; i++) {
    colors.push(`#${Math.floor(Math.random() * 16777215).toString(16)}`)
  }

  return colors
}

使用

ts 复制代码
{
  const getOption = (total: number) => {
    const colors = generateColors(data.length)

    const op: echarts.EChartsOption = {
      animation,
      // @ts-expect-error
      color: colors,
    }
}
相关推荐
Kier10 天前
🔋 Vue + ECharts 实现分段折线图教学实战:电池趋势图案例
前端·javascript·echarts
@十八子德月生11 天前
第十章——8天Python从入门到精通【itheima】-99~101-Python基础综合案例-数据可视化(案例介绍=JSON格式+pyecharts简介)
大数据·python·信息可视化·pycharm·echarts·数据可视化
天上掉下来个程小白12 天前
Apache ECharts-02.入门案例
前端·spring boot·apache·echarts·苍穹外卖
GIS好难学14 天前
Echarts数据可视化开发教程+120套开源数据可视化大屏H5模板
前端·信息可视化·echarts
小浪努力学前端15 天前
React + ECharts:给tooltip里的按钮绑定事件,我踩过的那些坑
前端·react.js·echarts
elon_z18 天前
【项目实训#08】HarmonyOS知识图谱前端可视化实现
前端·echarts·知识图谱·harmonyos
紫薯馍馍19 天前
Dify创建 echarts图表 (二)dify+python后端flask实现
前端·flask·echarts·dify
戒不掉的伤怀20 天前
react,使用echarts过程
前端·react.js·echarts
万变不离其宗_821 天前
echarts使用笔记
前端·笔记·echarts
voyageracer22 天前
Vue3 中 echarts resize() 不生效
vue.js·echarts