ECharts饼图颜色设置的4种方式总结

作者:开源拾贝时间:2026-10-31 06:00:29浏览:649

本文总结了ECharts饼状图颜色设置的4种方法。包括在series、option和data中分别配置颜色,以及实现随机颜色的技巧。通过具体代码示例,展示如何自定义或动态生成饼图各扇区的显示颜色,帮助开发者灵活控制图表视觉效果。

ECharts 实现饼图色彩配置的教程

  • 方案一:于 series 中设定饼图色彩
  • 方案二:于 option 中设定饼图色彩
  • 方案三:于 data 中设定饼图色彩
  • 方案四:设定 ECharts 饼图随机色彩

Charts 饼图中各扇区色彩均可自定义或随机生成。本文阐述 4 种修改 ECharts 饼图色彩的配置方法。

方案一:于 series 中设定饼图色彩

series: [
  itemStyle: {
    normal: {
      color: function (colors) {
         var colorList = [
                    '#fc8251',
                    '#5470c6',
                    '#9A60B4',
                    '#ef6567',
                    '#f9c956',
                    '#3BA272'
                  ];
         return colorList[colors.dataIndex];
       }
     },
   }
 ]

EChart.js 在 series 中配置饼图色彩的 Demo 源码:

 this.chart.setOption({
        legend: {
          orient: "vertical",
          left: "left",
          textStyle: {
            //图例中文字的样式
            color: "#ffffff",
            fontSize: 12,
          },
        },
        series: [
          {
            type: "pie",
            radius: "50%",
            data: this.optionData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            labelLine: {
              show: false, //隐藏指示线
            },
            label: {
              show: false, //隐藏标示文字
            },
            itemStyle: {
              normal: {
                color: function (colors) {
                  var colorList = [
                    '#fc8251',
                    '#5470c6',
                    '#9A60B4',
                    '#ef6567',
                    '#f9c956',
                    '#3BA272'
                  ];
                  return colorList[colors.dataIndex];
                }
              },
            }
          },
        ],
      });

效果展示

ECharts 实现饼图色彩配置的教程

方案二:于 option 中设定饼图色彩

 this.chart.setOption({
        legend: {
          orient: "vertical",
          left: "left",
          textStyle: {
            //图例中文字的样式
            color: "#ffffff",
            fontSize: 12,
          },
        },
        color:['#fc8251','#5470c6','#9A60B4','#ef6567', '#f9c956','#3BA272'];
        series: [
          {
            type: "pie",
            radius: "50%",
            data: this.optionData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            labelLine: {
              show: false, //隐藏指示线
            },
            label: {
              show: false, //隐藏标示文字
            },
          },
        ],
      });

方案三:于 data 中设定饼图色彩

**data: [
        { value: 917, name: '搜索引擎',itemStyle: {color:'#fc8251'}},
        { value: 873, name: '微信朋友圈',itemStyle: {color:'#5470c6'}},
        { value: 678, name: 'Feeds 广告',itemStyle: {color:'#91cd77'}},
        { value: 583, name: '直接访问',itemStyle: {color:'#ef6567'}},
        { value: 432, name: '口碑介绍',itemStyle: {color:'#f9c956'}}
      ]
**

ECharts 实现饼图色彩配置的教程

EChart.js 在 data 中配置饼图色彩的 Demo 源码:

option = {
  legend: {
    top: 'bottom'
  },
  series: [
    {
      name: '',
      type: 'pie',
      radius: [50, 250],
      center: ['50%', '50%'],
      roseType: 'area',
      itemStyle: {
        borderRadius: 8
      },
      data: [
        { value: 917, name: '搜索引擎',itemStyle: {color:'#fc8251'}},
        { value: 873, name: '微信朋友圈',itemStyle: {color:'#5470c6'}},
        { value: 678, name: 'Feeds 广告',itemStyle: {color:'#91cd77'}},
        { value: 583, name: '直接访问',itemStyle: {color:'#ef6567'}},
        { value: 332, name: '电话销售',itemStyle: {color:'#f9c956'} },
        { value: 432, name: '口碑介绍',itemStyle: {color:'#75bedc'}}
      ]
    }
  ]
};

方案四:设定 ECharts 饼图随机色彩

color: function () {
          return (
            'rgb(' +
            [
              Math.round(Math.random() * 270),
              Math.round(Math.random() * 370),
              Math.round(Math.random() * 400)
            ].join(',') +
            ')'
          );
        },

ECharts 实现饼图色彩配置的教程

option = {
  legend: {
    top: 'bottom'
  },
  series: [
    {
      name: '',
      type: 'pie',
      radius: [50, 250],
      center: ['50%', '50%'],
      roseType: 'area',
      itemStyle: {
        color: function () {
          return (
            'rgb(' +
            [
              Math.round(Math.random() * 270),
              Math.round(Math.random() * 370),
              Math.round(Math.random() * 400)
            ].join(',') +
            ')'
          );
        },
        borderRadius: 8
      },
      data: [
        { value: 917, name: '搜索引擎'},
        { value: 873, name: '微信朋友圈'},
        { value: 678, name: 'Feeds 广告'},
        { value: 583, name: '直接访问'},
        { value: 332, name: '电话销售'},
        { value: 432, name: '口碑介绍'}
      ]
    }
  ]
};

总结

至此,关于 ECharts 饼图色彩配置的 4 种方式总结完毕。如需了解更多 ECharts 色彩设置内容,请查阅本站往期文章或继续浏览后续内容,感谢支持!

作者最新文章