echarts laben 饼图指示线条+文字颜色继承

注意:
label并不支持HTML片段
版本5.30

 label: {
                        show: true,
                        alignTo: 'edge',
                        position: 'outside',
                        // formatter: '{name|{b}}\n{time|{c} 小时}',
                        // formatter: '{b|{b}}\n{cstyle|{c}}',
                        formatter: params => `${params.value >= 1 ? '{b|' + params.value + '}' : params.value}元\n${params.name}`,  // label不支持html片段  
                        edgeDistance: 1,
                        lineHeight: 20,
                        rich: { // 因为label不支持html片段,所以css样式需要使用rich给添加进去  // formatter: '{b|{b}}\n{cstyle|{c}}', 详见https://echarts.apache.org/zh/option.html#series-pie.label
                            b: {
                                fontSize: 30,
                                color: "red",
                            },
                            cstyle: {
                                color: 'blue'
                            }
                        },
                        color: 'inherit'  // inherit 让字体和线条保持同一种颜色
                    },

效果图:
大于等于1的值以红色字体展示字体大小设置为30,其余小于1的继承线条的颜色
在这里插入图片描述
完整代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="./echarts.js"></script>
    <title>Document</title>
</head>

<body>
    <div id="echarts" style="width: 600px;height:500px"></div>
    <script>
        var chartDom = document.getElementById('echarts');
        var myChart = echarts.init(chartDom);
        var option;

        var datas = [
            [
                { name: '圣彼得堡来客', value: 5.6 },
                { name: '陀思妥耶夫斯基全集', value: 1 },
                { name: '史记精注全译(全6册)', value: 0.8 },
                { name: '加德纳艺术通史', value: 0.5 },
                { name: '表象与本质', value: 0.5 },
                { name: '其它', value: 3.8 }
            ],
        ];
        option = {
            title: {
                text: '阅读书籍分布',
                left: 'center',
                textStyle: {
                    color: '#999',
                    fontWeight: 'normal',
                    fontSize: 14
                }
            },
            series: datas.map(function (data, idx) {
                var top = idx * 33.3;
                return {
                    type: 'pie',
                    radius: [20, 60],
                    top: top + '%',
                    height: '33.33%',
                    left: 'center',
                    width: 400,
                    itemStyle: {
                        borderColor: '#fff',
                        borderWidth: 1
                    },
                    label: {
                        show: true,
                        alignTo: 'edge',
                        position: 'outside',
                        // formatter: '{name|{b}}\n{time|{c} 小时}',
                        // formatter: '{b|{b}}\n{cstyle|{c}}',
                        formatter: params => `${params.value >= 1 ? '{b|' + params.value + '}' : params.value}元\n${params.name}`,  // label不支持html片段  
                        edgeDistance: 1,
                        lineHeight: 20,
                        rich: { // 因为label不支持html片段,所以css样式需要使用rich给添加进去  // formatter: '{b|{b}}\n{cstyle|{c}}', 详见https://echarts.apache.org/zh/option.html#series-pie.label
                            b: {
                                fontSize: 30,
                                color: "red",
                            },
                            cstyle: {
                                color: 'blue'
                            }
                        },
                        color: 'inherit'  // inherit 让字体和线条保持同一种颜色
                    },
                    labelLine: {
                        length: 15,
                        length2: 0,
                        maxSurfaceAngle: 80
                    },
                    labelLayout: function (params) {
                        const isLeft = params.labelRect.x < myChart.getWidth() / 2;
                        const points = params.labelLinePoints;
                        // Update the end point.
                        points[2][0] = isLeft
                            ? params.labelRect.x
                            : params.labelRect.x + params.labelRect.width;
                        return {
                            labelLinePoints: points
                        };
                    },
                    data: data
                };
            })
        };
        option && myChart.setOption(option);

    </script>
</body>

</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值