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,
    }
}
相关推荐
马卫斌 前端工程师2 小时前
vue3 实现echarts 3D 地图
前端·javascript·echarts
盼哥PyAI实验室2 天前
纯前端打造个人成长网站:零后端、零部署、零服务器的实践分享
运维·服务器·前端·javascript·echarts·个人开发
Lsx_2 天前
详解ECharts中的convertToPixel和convertFromPixel
前端·javascript·echarts
java水泥工3 天前
基于Echarts+HTML5可视化数据大屏展示-工厂信息监控台
echarts·html5·可视化大屏·大屏模版
菩提树下的杨过4 天前
dify+LLM+echarts打造智能可视化数据分析AI助手
echarts·dify
合作小小程序员小小店6 天前
web网页,在线%抖音,舆情%分析系统demo,基于python+web+echart+nlp+知识图谱,数据库mysql
数据库·python·自然语言处理·flask·nlp·echarts·知识图谱
一川_7 天前
《Echarts内存泄漏惊魂记:我的页面在导航切换中“炸”了!》
echarts
合作小小程序员小小店7 天前
大屏开发,在线歌词舆情分析系统demo,基于python,flask,web,echart,nlp,自然语言数据库mysql。
后端·python·flask·nlp·echarts
Lsx_8 天前
ECharts 全局触发click点击事件(柱状图、折线图增大点击范围)
前端·javascript·echarts
xiaohe06019 天前
🚀 拥抱 create-uni,一行命令轻松集成 Uni ECharts!
vue.js·uni-app·echarts