ECharts 饼图颜色设置教程 - 4 种方式设置饼图颜色

67 篇文章 1 订阅
16 篇文章 1 订阅

ECharts 饼状图颜色设置教程

  1. 方法一:在 series 内配置饼状图颜色
  2. 方法二:在 option 内配置饼状图颜色
  3. 方法三:在 data 内配置饼状图颜色
  4. 方法四:配置 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];
                }
              },
            }

          },
        ],
      });

效果

在这里插入图片描述

方法二:在 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'}}
      ]
**

在这里插入图片描述
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(',') +
            ')'
          );
        },

在这里插入图片描述

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: '口碑介绍'}
      ]

    }
  ]
};

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
ECharts饼图颜色可以通过多方式进行设置。其中,可以在`series`内配置饼图颜色,也可以在`option`内配置饼图颜色,还可以在`data`内配置饼图颜色。此外,还可以通过配置实现饼图的随机颜色。\[1\] 如果希望在不同页面调用同一个接口的饼图时显示相同的颜色,可以采取以下解决方案:\[2\] - 方式一:在`option`内通过`color`属性配置饼图颜色。 - 方式二:在`series`内配置饼图颜色。 - 方式三:在`data`内配置饼图颜色。 通过以上方法,可以实现对ECharts饼图中每个扇形的颜色进行自定义或随机显示。比如,如果X轴代表各销售渠道名,可以指定每个扇面的颜色以实现全局统一的识别色彩。\[3\] #### 引用[.reference_title] - *1* *3* [ECharts 饼图颜色设置教程 - 4 方式设置饼图颜色](https://blog.csdn.net/weixin_48201324/article/details/124077117)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ECharts 饼状图颜色设置](https://blog.csdn.net/qq_38778882/article/details/130596533)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值