饼图-使用graphic饼图中间显示文字,label带边框和百分比

本文介绍如何使用ECharts实现饼图中多行文字效果及动态数据展示,包括文字换行处理、颜色设置、百分比显示等细节,通过具体代码示例,帮助读者理解并掌握复杂饼图的配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

var midText = '多行显示的文字效果展示';
var _midText = showFontNum(midText);
var _value = [1, 2, 3, 4];
var _name = ["深圳", "广州", "北京", "上海"];
var _color = ['#a5be', '#5eccf8', 'pink', 'yellow'];
var pieData = []; //存储主导饼图的数据
for (var i = 0; i < _name.length; i++) {
    var item = {
        value: _value[i],
        name: _name[i],
        itemStyle: {
            color: _color[i]
        }
    };
    pieData.push(item);
}
var _rich = {
    // 对【城市展示】的设置
    a: {
        color: '#999',
        lineHeight: 22,
        align: 'center'
    },
    // 对【城市展示】背景的设置
    abg: {
        backgroundColor: '#333',
        width: '100%',
        align: 'right',
        height: 22,
        borderRadius: [4, 4, 0, 0]
    },
    // 下划线的设置
    hr: {
        borderColor: 'blue',
        width: '100%',
        borderWidth: 0.5,
        height: 0
    },
    // legend名称设置
    b: {
        fontSize: 16,
        lineHeight: 33
    },
    // 百分比设置
    per: {
        color: '#eee',
        backgroundColor: '#334455',
        padding: [2, 4],
        borderRadius: 2
    }
};
option = {
    backgroundColor: '#1A4D83',
    graphic: [{ //2、中心的文字设置
        type: 'text',
        z: 100,
        left: 'center',
        top: 'middle',
        style: {
            x: 0,
            y: 0,
            fill: '#fff',
            text: _midText,
            font: 'bolder 16px  "Microsoft YaHei", sans-serif',
            textAlign: 'center', //3、居中显示
        }

    }],
    tooltip: {
        show: true,
        trigger: 'item',
        axisPointer: { // 坐标轴指示器,坐标轴触发有效
            type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
        },
        formatter: "{a}</br>{b}: {c}</br>占比:{d}%"
    },
    legend: {
        orient: 'vertical',
        right: '0%',
        bottom: '15%',
        textStyle: {
            fontWeight: 'normal',
            fontSize: '16'
        },
        data: ['男男', '女女', '男女', '不详']
    },
    series: [{ //显示中间文字的辅助图1
            type: 'pie',
            radius: ['22%', '28%'], //28% 和主导图的左值一致
            silent: true, //禁用鼠标悬浮效果
            hoverAnimation: false,
            itemStyle: {
                normal: {
                    color: '#164270',
                },
            },
            data: [2],
            zlevel: 1,
        },
        { //显示外部边框的辅助图2
            type: 'pie',
            radius: ['50%', '56%'], //50% 和主导图的右值一致
            silent: true, //禁用鼠标悬浮效果
            hoverAnimation: false,
            itemStyle: {
                normal: {
                    color: '#164270',
                },
            },
            data: [2],
            zlevel: 2,
        },
        { //主导内容
            name: '城市展示',
            type: 'pie',
            radius: ['28%', '50%'],
            startAngle: 190, //设置起始的角度
            avoidLabelOverlap: false,
            hoverAnimation: false,
            zlevel: 3,
            label: {
                normal: {
                    formatter: '{a|{a}}{abg|}\n{hr|}\n  {b|{b}:}{c}  {per|{d}%}  ',
                    backgroundColor: '#eee',
                    borderColor: '#aaa',
                    borderWidth: 1,
                    borderRadius: 4,
                    shadowBlur: 3,
                    shadowOffsetX: 2,
                    shadowOffsetY: 2,
                    shadowColor: '#999',
                    padding: [0, 7],
                    rich: _rich,
                }
            },
            labelLine: {
                normal: {
                    show: true,
                    length: 20,
                    length2: 40,
                    lineStyle: {
                        type: 'dashed',
                        width: 2
                    }
                }
            },
            data: pieData,
        }
    ]
};
//文字显示函数
function showFontNum(midText) {
    var newParamsName = ""; // 最终拼接成的字符串
    var paramsNameNumber = midText.length; // 实际文字个数
    var provideNumber = 4; // 每行能显示的字的个数
    var rowNumber = Math.ceil(paramsNameNumber / provideNumber); // 换行的话,需要显示几行,向上取整
    /**
     * 判断标签的个数是否大于规定的个数, 如果大于,则进行换行处理 如果不大于,即等于或小于,就返回原标签
     */
    if (paramsNameNumber > provideNumber) {
        /** 循环每一行,p表示行 */
        for (var p = 0; p < rowNumber; p++) {
            var tempStr = ""; // 表示每一次截取的字符串
            var start = p * provideNumber; // 开始截取的位置
            var end = start + provideNumber; // 结束截取的位置
            // 此处特殊处理最后一行的索引值
            if (p === rowNumber - 1) {
                // 最后一次不换行
                tempStr = midText.substring(start, paramsNameNumber);
            } else {
                // 每一次拼接字符串并换行
                tempStr = midText.substring(start, end) + "\n\n";
            }
            newParamsName += tempStr; // 最终拼成的字符串
        }
    } else {
        newParamsName = midText;
    }
    return newParamsName;
}

样式:{ // 配置项(用于保存时校验,尽量不要删除此行) // 标题 title: { text: '', // 主标题 subtext: '', // 副标题 left: 'center', // 标题位置 textStyle: { // 主标题样式 fontSize: 18, // 字体大小 color: '#333', // 字体颜色 fontWeight: 'bold', // 字体加粗 100 - 900 或 bold }, // 副标题样式 subtextStyle: { fontSize: 12,color: '#CCC',fontWeight: 'normal', }, }, // 例 legend: { show: true, //是否显示 type: 'scroll', // plain普通、scroll可翻页 orient: 'horizontal' ,// 例布局朝向 horizontal横向、vertical纵向 left: 'center', // 例位置 left、center、right textStyle: { color: '#556677', // 文字的颜色。 fontSize: 12, // 文字的字体大小。 }, }, // 表数据项 series: { radius: [10, 70], //内外圈、单值则没有空环 center: ['50%', '50%'], //位置(前者距左距离,后者距上距离) itemStyle: { borderRadius: 10, // 每个半环的倒角 borderColor: '#fff', // 半环边框颜色 borderWidth: 4, // 半环边框宽度 相当于每个环之间的间隔 }, }, backgroundColor:'#0C439B', uEchartsConfig:{color:'white', backgroundColor:'#0C439B'} }数据源:select '源悦' as xValue,'源悦收入' AS yName, round(sum(sofp_kpwsje)/10000,2) as yValue from sofp where year(sofp_kprq)=&当前年度& and sofp_srflid not in ('06','07','08') and sofp_gsid='10'现在请你参考这个帮我写这个:“{ // 表整体背景色 backgroundColor: '', // 默认透明 // 表标题配置项 uEchartsConfig: { color:'white', //文本颜色 backgroundColor:'', //背景色 默认透明 fontSize:'15px', //文本字体 }, // 配置项(用于保存时校验,尽量不要删除此行) // 标题 title: { text: '', // 主标题 subtext: '', // 副标题 left: 'center', // 标题位置 textStyle: { // 主标题样式 fontSize: 18, // 字体大小 color: '#333', // 字体颜色 fontWeight: 'bold', // 字体加粗 100 - 900 或 bold }, // 副标题样式 subtextStyle: { fontSize: 12,color: '#CCC',fontWeight: 'normal', }, }, // 例 legend: { show: true, //是否显示 type: 'scroll', // plain普通、scroll可翻页 wdith: '80%', // 例所占宽度(例多配合scroll使用) orient: 'horizontal' ,// 例布局朝向 horizontal横向、vertical纵向 left: 'center', // 例位置 left、center、right top: 'top', // 例位置 top、middle、bottom // pageIconColor: '#fff', // 翻页箭头颜色 // pageTextStyle: {color: '#fff' }, // 翻页页码样式 textStyle: { color: '#556677', // 文字的颜色。 fontSize: 12, // 文字的字体大小。 }, }, // 表数据项 series: { radius: [50, 70], //内外圈、单值则没有空环 center: ['50%', '50%'], //位置(前者距左距离,后者距上距离) itemStyle: { borderRadius: 10, // 每个半环的倒角 borderColor: '#fff', // 半环边框颜色 borderWidth: 2, // 半环边框宽度 相当于每个环之间的间隔 }, // 显示label: { normal: { formatter: "{b}: {c}", // 自定义显示格式(b:名称, c:值, d:百分比) }, }, } }”里面数据源该怎么写
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佛佛ง

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值