echarts 环形图

运行结果:

代码

import {truncate, merge} from 'lodash';
import {getBasePieOptions, getTooltipFormatter} from "*/money/utils";

const colorArray = ['#1F8BFF', '#EDBE75', '#26E3F0', '#AF8FFF', '#61DDAA', '#FD996A', '#8367E0', '#1AAF87']

export function getTypeOptions(data) {
    const options = getBasePieOptions()
    // 提示框配置
    options.tooltip = merge(options.tooltip, {
        formatter: (params) => {
            const {color, name, value} = params
            return getTooltipFormatter(color, [name, value])
        }
    })

    // 图表配置
    options.legend = merge(options.legend, {
        top: 100,
        right: 16,
        height: 298,
        textStyle: {
            rich: {
                // 标题
                a: {
                    width: 100,
                    padding: [0, 6, 0, 0],
                },
                // 数量
                b: {
                    width: 50,
                    align: 'right',
                    fontSize: 16,
                    color: '#333',
                    padding: [0, 2, 0, 0],
                    verticalAlign: 'center'
                },
                // 份
                c: {
                    align: 'right',
                    fontSize: 12,
                    color: '#666',
                    verticalAlign: 'center'
                },
                // 百分比
                d: {
                    width: 45,
                    align: 'right',
                    padding: [0, 0, 0, 36],
                    fontSize: 16,
                    color: '#333',
                },
                // 百分比符号
                e: {
                    align: 'right',
                    fontSize: 12,
                }
            }
        },
        formatter: function (name) {
            let item = data.find(item => item.name === name)
            let truncatedName = truncate(name, 12) // 截断文本,保留字符
            return `{a|${truncatedName}}{b|${item.value}}{c|份}{d|${item.percent}}{e|%}`
        },
    })

    // 系列配置
    const seriesConfig = options.baseSeries
    delete options.baseSeries
    options.series = [merge(seriesConfig, {
        right: '50%',
        radius: ['50%', '65%'],
        emphasis: {
            label: {
                rich: {
                    // 标题
                    a: {
                        fontSize: 20,
                        fontWeight: 500,
                        lineHeight: 28,
                        padding: [0, 0, 25, 0]
                    },
                    // 数量
                    b: {
                        fontSize: 32,
                        fontWeight: 500,
                    },
                    // 单位
                    c: {
                        fontSize: 14,
                        color: '#999',
                        padding: [4, 0, 0, 0],
                    },
                },
                formatter: function (params) {
                    return `{a|${params.name}} \n {b|${params.value}} {c|份}`;
                },
            },
        },
        data: data.map((item, index) => {
            return {
                name: item.name,
                value: item.value,
                percent: item.percent,
                itemStyle: {
                    color: colorArray[index % colorArray.length] // 使用颜色数组中的索引值作为颜色
                }
            };
        })
    })]
    return options
}

export function getStatusOptions(data) {
    const options = getBasePieOptions()
    // 提示框配置
    options.tooltip = merge(options.tooltip, {
        formatter: (params) => {
            const {color, name, value} = params
            return getTooltipFormatter(color, [name, value])
        }
    })

    // 图表配置
    options.legend = merge(options.legend, {
        top: 65,
        right: 16,
        height: 298,
        textStyle: {
            rich: {
                // 标题
                a: {
                    width: 100,
                    padding: [0, 6, 0, 0],
                },
                // 数量
                b: {
                    width: 50,
                    align: 'right',
                    fontSize: 16,
                    color: '#333',
                    padding: [0, 2, 0, 0],
                    verticalAlign: 'center'
                },
                // 份
                c: {
                    align: 'right',
                    fontSize: 12,
                    color: '#666',
                    verticalAlign: 'center'
                },
                // 百分比
                d: {
                    width: 45,
                    align: 'right',
                    padding: [0, 0, 0, 36],
                    fontSize: 16,
                    color: '#333',
                },
                // 百分比符号
                e: {
                    align: 'right',
                    fontSize: 12,
                }
            }
        },
        formatter: function (name) {
            let item = data.find(item => item.name === name)
            let truncatedName = truncate(name, {length: 12}) // 截断文本,保留字符
            return `{a|${truncatedName}}{b|${item.value}}{c|份}{d|${item.percent}}{e|%}`
        },
    })

    // 系列配置
    const seriesConfig = options.baseSeries
    delete options.baseSeries
    options.series = [merge(seriesConfig, {
        right: '50%',
        radius: ['50%', '65%'],
        emphasis: {
            label: {
                rich: {
                    // 标题
                    a: {
                        fontSize: 20,
                        fontWeight: 500,
                        lineHeight: 28,
                        padding: [0, 0, 25, 0]
                    },
                    // 数量
                    b: {
                        fontSize: 32,
                        fontWeight: 500,
                    },
                    // 单位
                    c: {
                        fontSize: 14,
                        color: '#999',
                        padding: [4, 0, 0, 0],
                    },
                },
                formatter: function (params) {
                    return `{a|${params.name}} \n {b|${params.value}} {c|份}`;
                },
            },
        },
        data: data.map((item, index) => {
            return {
                name: item.name,
                value: item.value,
                percent: item.percent,
                itemStyle: {
                    color: colorArray[index % colorArray.length] // 使用颜色数组中的索引值作为颜色
                }
            };
        })
    })]
    return options
}
export function getTooltipFormatter(color, tips) {
    return `
                <div style="display: flex;align-items: center;justify-content: flex-start;background-color: rgba(255, 255, 255, 0.9); padding: 8px; border-radius: 4px;">
                    <span style="display: inline-block; margin-right: 5px; width: 10px; height: 10px; border-radius: 50%;background-color: ${color}';"></span>
                    <span style="font-family: Source Han Sans CN;color: #666666;text-align: left;margin-right:10px;letter-spacing: 0px;line-height: 20px;font-size: 12px;font-weight: normal;">
                        ${tips[0]}
                    </span>
                    <span style="margin-left: auto; font-family: Source Han Sans CN;color: #333333;letter-spacing: 0px;line-height: 20px;font-size: 12px;font-weight: normal;">
                        ${tips[1]}
                    </span>
                </div>
            `
}


// 获取饼图配置
export function getBasePieOptions() {
    return {
        tooltip: {
            trigger: 'item',
            extraCssText: 'border-radius: 6px;background: linear-gradient(314deg, rgba(253, 254, 255, 0.6) -6%, rgba(244, 247, 252, 0.6) 85%);backdrop-filter: blur(10px);box-shadow: 0px 10px 20px 0px rgba(167, 200, 255, 0.5),inset 0px -2px 12px 0px rgba(229, 237, 250, 0.5),inset 0px 2px 6px 0px rgba(229, 237, 250, 0.9);',
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            icon: 'circle',
            itemGap: 14,
            itemWidth: 12,
            itemHeight: 12,
            // selectedMode: false,
            textStyle: {
                overflow: 'hidden',
                textOverflow: 'ellipsis',
                whiteSpace: 'nowrap',
                rich: {
                    a: {
                        fontSize: 12,
                        color: '#666',
                        lineHeight: 15,
                        fontWeight: 500,
                        verticalAlign: 'center'
                    },
                    b: {
                        fontSize: 12,
                        color: '#666',
                        fontWeight: 500,
                        lineHeight: 15,
                        verticalAlign: 'center'
                    },
                    c: {
                        fontSize: 12,
                        color: '#666',
                        fontWeight: 500,
                        lineHeight: 15,
                        verticalAlign: 'center'
                    },
                    d: {
                        fontSize: 12,
                        color: '#666',
                        fontWeight: 500,
                        lineHeight: 15,
                        verticalAlign: 'center'
                    },
                    e: {
                        fontSize: 12,
                        color: '#666',
                        fontWeight: 500,
                        lineHeight: 15,
                        verticalAlign: 'center'
                    }
                }
            },
        },
        baseSeries: {
            name: 'pie',
            type: 'pie',
            radius: ['35%', '50%'],
            legendHoverLink: false,
            label: {
                show: false,
                position: 'center',
            },
            emphasis: {
                label: {
                    show: true,
                    fontFamily: 'Source Han Sans CN',
                    rich: {
                        a: {
                            fontSize: 14,
                            color: '#333',
                            lineHeight: 26,
                            fontWeight: 400,
                            verticalAlign: 'center'
                        },
                        b: {
                            fontSize: 14,
                            color: '#333',
                            lineHeight: 26,
                            fontWeight: 400,
                            verticalAlign: 'center'
                        },
                        c: {
                            fontSize: 14,
                            color: '#333',
                            lineHeight: 26,
                            fontWeight: 400,
                            verticalAlign: 'center'
                        },
                        d: {
                            fontSize: 14,
                            color: '#333',
                            lineHeight: 26,
                            fontWeight: 400,
                            verticalAlign: 'center'
                        },
                        e: {
                            fontSize: 14,
                            color: '#333',
                            lineHeight: 26,
                            fontWeight: 400,
                            verticalAlign: 'center'
                        },
                    }
                }
            },
            labelLine: {
                show: true
            },
            avoidLabelOverlap: false,
        }
    }
}

// 增强饼图功能
export function emphasizePieFunction(chartInstance, data, {
    enableLoop,
    interval,
    immediate
}) {
    // 初始化索引
    chartInstance.currentIndex = 0;
    chartInstance.seriesIndex = 0;

    chartInstance.on('mouseover', (e) => {
        if (enableLoop) {
            // 鼠标悬浮于饼图时清除轮播,并展示悬浮块的信息
            clearInterval(chartInstance.timer)
            chartInstance.timer = null
        }
        chartInstance.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex: chartInstance.currentIndex
        })
        if (e.dataIndex === chartInstance.currentIndex) {
            chartInstance.dispatchAction({
                type: 'highlight', // 启动高亮
                seriesIndex: 0,
                dataIndex: chartInstance.currentIndex
            })
        }
        chartInstance.currentIndex = e.dataIndex
    })
    chartInstance.on('mouseout', (e) => {
        clearInterval(chartInstance.timer)
        if (enableLoop) autoTime()
    })

    function autoTime() {
        chartInstance.dispatchAction({
            type: 'highlight', // 启动高亮
            seriesIndex: chartInstance.seriesIndex,
            dataIndex: chartInstance.currentIndex
        })
        chartInstance.timer = setInterval(() => {
            chartInstance.dispatchAction({
                type: 'downplay', // 关闭高亮
                seriesIndex: 0,
                dataIndex: chartInstance.currentIndex
            })
            chartInstance.currentIndex++
            if (chartInstance.currentIndex === data.length) {
                chartInstance.currentIndex = 0
            }
            chartInstance.dispatchAction({
                type: 'highlight', // 启动高亮
                seriesIndex: 0,
                dataIndex: chartInstance.currentIndex
            })
        }, interval)
    }

    if (immediate) autoTime();
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts环形是一种常用的数据可视化表类型,用于展示数据的占比关系。它通常由一个圆环和多个扇形组成,扇形的大小表示数据的比例。以下是创建Echarts环形的步骤: 1. 首先,导入Echarts库并创建一个容器来放置表。可以使用HTML中的div元素作为表的容器。 2. 接下来,定义一个JavaScript变量来存储表的配置选项。配置选项包括表的类型、标题、例、数据等。 3. 在配置选项中,设置表的类型为环形。可以使用"pie"或者"pie"的别名"饼"来表示环形。 4. 设置表的标题和副标题,以便更好地描述表的含义。 5. 添加例,例用于标识不同扇形的含义。可以根据数据的类别或者名称来设置例。 6. 定义表的数据。数据可以是一个数组,每个元素代表一个扇形。每个扇形包括名称和对应的数值。 7. 对于环形,可以设置内外半径来调整环的大小。可以使用"radius"或者"innerRadius"和"outerRadius"来设置半径值。 8. 设置其他样式选项,如背景颜色、字体样式、边框样式等。 9. 最后,使用Echarts的API将容器和配置选项绑定在一起,并渲染出表。 总结一下,创建Echarts环形的步骤包括导入库、创建容器、定义配置选项、设置表类型、标题、例和数据、调整半径和样式,最后将容器和配置选项绑定并渲染表。 了解更多关于Echarts环形的详细信息,可以参考Echarts的官方文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值