echarts中读取json文件;并在环形图中间显示总数

json文件

{
  "list": [
    { "value": 3, "name": "张江北区" },
    { "value": 3, "name": "张江南区" },
    { "value": 2, "name": "张江中区" },
    { "value": 3, "name": "康桥工业园南区" },
    { "value": 5, "name": "国际医学园区" },
    { "value": 4, "name": "规划保留用地" },
    { "value": 5, "name": "康桥工业园北区" }
  ]
}

html文件

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/echarts.min.js"></script>
    <script src="./jquery/jquery.js"></script>
    <style>
        ::-webkit-scrollbar {
            display: none;
            /* Chrome Safari */
        }

        * {
            margin: 0;
            padding: 0;
        }

        body {
            /* width: 100vw;
            height: 90vh; */
            /* background-color: rgb(94, 140, 158); */
        }

        * {
            content: "";
        }

        .main {
            width: 100vw;
            height: 90vh;
            /* background-color: rgb(14, 150, 59); */
            display: block;
        }
    </style>
</head>

<body>
    <canvas class="main"></canvas>
    <script>


        let width = document.body.offsetWidth;
        let height = document.body.offsetHeight;

        var el = document.querySelector('.main');
        el.width = width;
        el.height = height;

        var data = []
        var total = 0;

        $.ajax({
            type: 'get',
            url: 'json/data.json',
            dataType: 'json',
            success: function (response) {
                console.log(response.list);
                data = response.list;
            },
            error: function () {
                console.log('数据获取失败');
            }
        }).then(() => {
            console.log(data);
            data.forEach((res)=>{
                total+=res.value;
            })
            let myCharts = echarts.init(el);

            function setEchartsPie(data, total) {

                let option = {
                    // data = [1,2,2,3,5,4,4],
                    title: {
                        zlevel: 0,
                        subtext: total + '例',
                        subtextStyle: {
                            color: '#13227a',
                            fontWeight: 'bold',
                            fontSize: 50
                        },
                        x: 'center',//文字位置
                        y: 'center'//文字位置
                    },

                    legend: {
                        show: false,
                        data: ['张江北区', '张江南区', '张江中区', '康桥工业园南区', '国际医学园区', '规划保留用地', '康桥工业园北区']
                    },
                    tooltip: {
                        show: false
                    },

                    legend: {
                        show: false,
                        selectedMode: false,
                    },
                    series: [{
                        name: '访问来源',
                        type: 'pie',
                        radius: ['60%', '75%'],
                        avoidLabelOverlap: false,
                        hoverAnimation: false,
                        label: {
                            show: true,
                            // position: 'center',
                            formatter: '{b}:{c}例',
                            rich: {
                                c: {
                                    color: '#13227a',
                                    fontSize: 12,
                                    lineHeight: 10
                                },
                                // // b: {
                                //     fontSize: 12,
                                //     lineHeight: 20,
                                //     color: '#333'
                                // },
                            },
                            textStyle: {
                                fontSize: 25,

                                color: "#666"
                            }
                        },
                        emphasis: {
                            label: {
                                show: false,
                                fontSize: '30',
                                fontWeight: 'bold'
                            }
                        },
                        labelLine: {
                            show: true,
                            length: 50,
                            length2: 150,
                            lineStyle: {
                                color: '#808080'
                            },
                        },

                        data: data,
                    }]
                };
                myCharts.setOption(option);
            }
            setEchartsPie(data, total)
        });
    </script>
</body>

</html>

结果截图:

 

 

结尾:

运行不出来的小伙伴更新一下自己的echarts版本。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值