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,
    }
}
相关推荐
尘中客3 天前
放弃 Echarts?前端直接渲染后端高精度 SVG 矢量图流的踩坑记录
前端·javascript·echarts·前端开发·svg矢量图·echarts避坑
Dxy12393102165 天前
ECharts折线图入门学习:从基础到实战的完整指南
学习·信息可视化·echarts
yb305小白5 天前
echarts 排名Y轴数据过多出现滚动条,排名柱形条绑定事件
前端·echarts
Dxy12393102165 天前
ECharts入门学习:从零开始打造炫酷数据可视化
学习·信息可视化·echarts
533_7 天前
[echarts] 使用scss变量
前端·echarts·scss
漂移的电子7 天前
【echarts 细节】
前端·javascript·echarts
qq_342218128 天前
echarts 横向渐变格子柱状图 三层
前端·javascript·echarts
Red丶哞9 天前
echarts快速上手
echarts
漂移的电子10 天前
【常用】element-ui/moment.js/echarts.js等
javascript·ui·echarts
Julyued12 天前
vue3开发echarts热力图
前端·echarts