Echarts图表 饼图中间带文字解决某个值过小导致扇区太小影响交互

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-1.9.1.min.js"></script>
    <script src="js/echarts.min.js"></script>
    <style>
        .chart{
            border: 1px solid #f56868;
            width: 300px;
            height: 200px;
        }
    </style>
</head>
<body>
    <div class="chart"></div>
</body>
<script>
    $(function () {
        var people={
            num:10,
            num2:3,
            num3:7,
        };
        /*第一种 javascript 选择器
         *document.getElementsByClassName("chart")[0]
         * 必须加  [0]  标
         */
        /*第二种 jQuery 选择器
         *$(".chart")[0]
         * 必须加  [0]  标
         */

//        var echarts_1 = echarts.init($(".chart")[0]);
        var echarts_1 = echarts.init(document.getElementsByClassName("chart")[0]);

        echarts_1.setOption(option = {
            title: {
//                text: people.num2+'%',
                /*原始方法未进行判断
                * 
                * 推荐下面进行三目运算判断,
                * 在people.num的值为0的时候,
                * 饼图中间数据不会显示NAN
                **/
                text: people.num==0 ? '0%': Math.ceil(people.num2*100/people.num)+'%',
                // subtext: '12月',
                x: 'center',
                y: 'center',
                textStyle: {
                    fontWeight: 'normal',
                    fontSize: 15
                }
            },
            backgroundColor: "#fff",
            color:["#1bb856","#dddddd"],
            tooltip: {
                trigger: 'item',
                formatter: function(params, ticket, callback) {
                    var res = params.seriesName;
                    res += '<br/>' + params.name + ' : ' + params.value + '人' + '';
                    return res;
                }
            },
            selectedOffset:5,
            /* 可以需要的时候放开*/
//             legend: {
//                 orient: 'vertical',
//                 right: '0%',
//                 bottom: '0%',
//                 data: ['已完成', '未完成'],
//                 itemWidth: 30,
//                 itemHeight: 10
//             },
            series: [{
                name: '总人数'+ ' : '+ people.num + '人',
                type: 'pie',
//                minAngle:40,  /*最小的扇区角度(0 ~ 360),用于防止某个值过小导致扇区太小影响交互*/
                selectedMode: 'single',
                radius: ['65%', '80%'],
                avoidLabelOverlap: false,
                label: {
                    normal: {
                        show: true,
                        position: 'outside',
                        formatter: "{b}{c}人"

                    },
                    emphasis: {
                        show: true
                    }
                },
                data: [{
                    value: people.num2,
                    name: '已完成'
                }, {
                    value: people.num3,
                    name: '未完成'
                }],
                labelLine: {
                    normal: {
                        show: true,
                        length:5,
                        length2:10
                    }
                }

            }]
        });
    })
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值