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

信铁寒胜实测:用了下面第三种方式,改了饼状图的颜色

 

ECharts 饼状图颜色设置教程

  • 方法一:在 series 内配置饼状图颜色
  • 方法二:在 option 内配置饼状图颜色
  • 方法三:在 data 内配置饼状图颜色
  • 方法四:配置 ECharts 饼状图随机颜色

Charts 饼状图中的每个扇形颜色其实都可以自定义或者随机显示颜色。本文讲解 4 种配置修改 ECharts 饼图颜色的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
series: [
  itemStyle: {
    normal: {
      color: function (colors) {
         var colorList = [
                    '#fc8251',
                    '#5470c6',
                    '#9A60B4',
                    '#ef6567',
                    '#f9c956',
                    '#3BA272'
                  ];
         return colorList[colors.dataIndex];
       }
     },
   }
 ]

  EChart.js 在 series 中设置饼状图颜色的 Demo 源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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  内配置饼状图颜色

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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  内配置饼状图颜色

1
2
3
4
5
6
7
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: 432, name: '口碑介绍',itemStyle: {color:'#f9c956'}}
      ]
**

  

 EChart.js 在 data 中设置饼状图颜色的 Demo 源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 饼状图随机颜色 

1
2
3
4
5
6
7
8
9
10
11
color: function () {
          return (
            'rgb(' +
            [
              Math.round(Math.random() * 270),
              Math.round(Math.random() * 370),
              Math.round(Math.random() * 400)
            ].join(',') +
            ')'
          );
        },

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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种方式总结_javascript技巧_脚本之家 (jb51.net)

 

posted @   信铁寒胜  阅读(7767)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示