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,
    }
}
相关推荐
万变不离其宗_85 小时前
echarts使用笔记
前端·笔记·echarts
voyageracer1 天前
Vue3 中 echarts resize() 不生效
vue.js·echarts
应巅2 天前
echarts 数据大屏(无UI设计 极简洁版)
前端·ui·echarts
RR13352 天前
图标统计页面的设计与控件 Apache echarts
前端·apache·echarts
这可不简单6 天前
方便易懂的自适应方案---echarts和dom样式大小自适应
前端·vue.js·echarts
八月林城7 天前
echarts在uniapp中使用安卓真机运行时无法显示的问题
android·uni-app·echarts
路很长OoO9 天前
鸿蒙手写ECharts_手势惯性(条形统计图)
echarts·harmonyos·canvas
Alice_hhu10 天前
ResizeObserver 解决 echarts渲染不出来,内容宽度为 0的问题
前端·javascript·echarts
纯阳阳10 天前
如何用echart绘制圆柱
前端·echarts
qq_106138345714 天前
SpringBoot+Vue+Echarts实现可视化图表的渲染
vue.js·spring boot·echarts