【ReactEcharts】环形图常规案例实现

详细配置项可跳转至我的文章 ReactEcharts常见基础配置项解析

代码实现

import React from 'react';
import ReactEcharts from 'echarts-for-react';
import * as echarts from 'echarts';
import 'echarts-liquidfill';

export default function CircleEchart(prop) {
    let value = prop.score || 0;
    let colorIndex = prop.index || 0;
    let int = value > 0 ? value.toFixed(1).split('.')[0] : 0;
    let float = value > 0 ? value.toFixed(1).split('.')[1] : 0;
    const colorArr = [
        ['rgba(26, 255, 252, 1)', 'rgba(0, 255, 180, 1)'],
        ['rgba(31, 175, 255, 1)', 'rgba(0, 240, 255, 1)'],
        ['rgba(185, 169, 255, 1)', 'rgba(208, 125, 255, 1)'],
        ['rgba(255, 199, 80, 1)', 'rgba(255, 154, 90, 1)'],
    ];
    const borderColor = [
        'rgba(0, 255, 180, 0.8)',
        'rgba(0, 240, 255,0.8)',
        'rgba(208, 125, 255, 0.8)',
        'rgba(255, 154, 90, 0.8)',
    ];
    let option = {
        background:
            'linear-gradient(270deg, rgba(0,0,0,0) 0%, rgba(3,7,22,0.73) 47%, #061135 100%);',
        title: {
            text: `{a|${int}}{b|.${float}}`,
            x: 'center',
            y: 'center',
            textStyle: {
                rich: {
                    a: {
                        fontSize: 18,
                        color: '#fff',
                        fontWeight: '600',
                    },
                    b: {
                        fontSize: 18,
                        color: '#fff',
                        fontWeight: '600',
                        padding: [0, 0, 0, 0], // 调整位置
                    },
                },
            },
        },
        series: [
            {
                type: 'gauge',
                radius: '90%',
                clockwise: false,
                startAngle: '90',
                endAngle: '-269.9999',
                splitNumber: 50,
                detail: {
                    offsetCenter: [0, 0],
                    formatter: ' ',
                },
                pointer: {
                    show: false,
                },
                axisLine: {
                    show: true,
                    lineStyle: {
                        color: [
                            [
                                0,
                                new echarts.graphic.RadialGradient(1, 1, 1, [
                                    {
                                        offset: 0,
                                        color: colorArr[colorIndex][0],
                                    },
                                    {
                                        offset: 1,
                                        color: colorArr[colorIndex][1],
                                    },
                                ]),
                            ],
                            [value / (prop.basePercent || 100), 'transparent'],
                            [
                                1,
                                new echarts.graphic.RadialGradient(0, 0, 1, [
                                    {
                                        offset: 0,
                                        color: colorArr[colorIndex][0],
                                    },
                                    {
                                        offset: 1,
                                        color: colorArr[colorIndex][1],
                                    },
                                ]),
                            ],
                        ],
                        width: 10,
                    },
                },
                axisTick: {
                    show: false,
                },
                splitLine: {
                    show: false,
                },
                axisLabel: {
                    show: false,
                },
            },
            // 外层环
            {
                name: '外层环',
                type: 'pie',
                radius: ['94%', '100%'],
                hoverAnimation: false,
                clockWise: false,
                itemStyle: {
                    normal: {
                        color: borderColor[colorIndex],
                    },
                },
                label: {
                    show: false,
                },
                data: [100],
                z: 2,
            },
            // 内层阴影环
            {
                type: 'pie',
                name: '内层环',
                radius: ['68%', '90%'],
                hoverAnimation: false,
                clockWise: false,
                itemStyle: {
                    normal: {
                        color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [
                            {
                                offset: 0,
                                color: 'rgba(255, 255, 255,.2)',
                            },
                            {
                                offset: 1,
                                color: 'rgba(255, 255, 255,.4)',
                            },
                        ]),
                    },
                },
                label: {
                    show: false,
                },
                data: [100],
                z: 1,
            },
        ],
    };

    return (
        <ReactEcharts
            option={option}
            notMerge
            lazyUpdate
            style={{ height: '100px', width: '100px' }}
        />
    );
}

效果图

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值