Echarts中柱状图完成横向布局

效果

代码

javascript 复制代码
import React, { useEffect, useRef } from 'react'
import './index.less'
import * as echarts from 'echarts';
const RightTop = (props: any) => {
    const echarts_ref = useRef<HTMLDivElement | null>(null);
    useEffect(() => {
        if (echarts_ref.current) {
            const myChart = echarts.init(echarts_ref.current);
            let colorList = ['#FF4B01', '#FF7B00', '#FFE823', '#fff', '#fff', '#fff']
            let result = props.teamData
            const option = {
                color: colorList,
                tooltip: {
                    show: false,
                    trigger: 'item',
                    borderColor: 'rgba(3, 11, 44, 0.5)',
                    textStyle: {
                        color: '#9bb2de'
                    }
                },
                legend: {
                    show: false
                },
                grid: {
                    left: '0%', // 向左扩展
                    right: '12%', // 向右扩展
                    top: '5%', // 向上扩展
                    bottom: '5%' // 向下扩展
                },
                xAxis: [
                    {
                        splitLine: {
                            show: false
                        },
                        type: 'value',
                        show: false
                    }
                ],
                yAxis: [
                    {
                        splitLine: {
                            show: false
                        },
                        axisLine: {
                            show: false
                        },
                        type: 'category',
                        axisTick: {
                            show: false
                        },
                    }
                ],
                series: [
                    {
                        name: '',
                        type: 'bar',
                        barWidth: 6, // 柱子宽度
                        MaxSize: 0,
                        showBackground: false,
                        borderRadius: [30, 30, 30, 30],
                        // 名称---
                        label: {
                            show: true,
                            offset: [10, -20], // 将标签上移20个像素
                            color: '#ccc',
                            fontWeight: 400,
                            position: 'left',
                            align: 'left',
                            fontSize: 16,
                            fontFamily: 'YouSheBiaoTiHei',
                            formatter: function (params) {
                                return params.data.name
                            }
                        },
                        data: result.map((item, index) => {
                            return {
                                name: item.name,
                                value: item.value,
                                itemStyle: {
                                    borderRadius: [30, 30, 30, 30],
                                    color: {
                                        type: 'linear',
                                        x: 0,
                                        y: 0,
                                        x2: 1,
                                        y2: 1,
                                        colorStops: [
                                            {
                                                offset: 0,
                                                color: '#0F1F45'
                                            },
                                            {
                                                offset: 1,
                                                color: colorList[index]
                                            }
                                        ]
                                    }
                                }
                            }
                        })
                    },
                    {
                        name: '',
                        type: 'bar',
                        barGap: '-100%',
                        barWidth: 4,
                        showBackground: false,
                        borderRadius: [30, 30, 30, 30],
                        label: {
                            position: 'right',
                            distance: 4,
                            show: true,
                            offset: [0, 0],
                            formatter: function (params) {
                                return '{a|' + params.data.value + '}'
                            },
                            rich: {
                                align: 'left',
                                a: {
                                    color: '#00D1FF',
                                    fontSize: 20,
                                    fontFamily: 'UnidreamLED',
                                    fontWeight: 500
                                },
                            }
                        },
                        data: result.map((item, index) => {
                            return {
                                name: item.name,
                                value: item.value,
                                itemStyle: {
                                    borderRadius: [30, 30, 30, 30],
                                    color: {
                                        type: 'linear',
                                        x: 0,
                                        y: 0,
                                        x2: 1,
                                        y2: 1,
                                        colorStops: [
                                            {
                                                offset: 0,
                                                color: '#0F1F45'
                                            },
                                            {
                                                offset: 1,
                                                color: colorList[index]
                                            }
                                        ]
                                    }
                                }
                            }
                        })
                    }
                ]
            }
            myChart.setOption(option);
        }
    }, [props])


    return (
        <div className='right-top'>
            <div className='right-top-big'>
                <div className='right-top-team'>团队</div>
            </div>
            {
                props.teamData.length > 0 && props.teamData ?
                    <div className='right-top-chart' ref={echarts_ref}></div>
                    :
                    <div style={{ height: '100%', display: 'flex', justifyContent: 'center', alignItems: 'center', fontSize: 18 }}>暂无数据</div>
            }
        </div>
    )
}

export default RightTop

less

css 复制代码
.right-top {
  width: 500px;
  height: 290px;
  margin-bottom: 5px;
  .right-top-big {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 500px;
    .right-top-team {
      width: 320px;
      height: 24px;
      margin-top: 14px;
      color: #ffffff;
      font-weight: 400;
      font-size: 22px;
      font-family: YouSheBiaoTiHei, YouSheBiaoTiHei;
      font-style: normal;
      line-height: 20px;
      letter-spacing: 1px;
      text-align: center;
      background: linear-gradient(
        90deg,
        rgba(3, 157, 229, 0) 0%,
        rgba(57, 222, 255, 0.26) 50%,
        rgba(3, 157, 229, 0) 100%
      );
      border-radius: 0px 0px 0px 0px;
    }
  }
  .right-top-chart{
    width: 500px;
    height: 270px;
  }
}

技术栈

React + TypeScript + Less + Echarts

相关推荐
_OP_CHEN4 分钟前
【前端开发之CSS】(三)CSS 常用元素属性宝典(上):从字体到文本,手把手教你打造高颜值网页!
前端·css·html·网页开发·文本属性·字体属性·页面美化
你脸上有BUG41 分钟前
【工程化】记给ant-design-vue打补丁的示例
前端·javascript·vue.js·补丁·ant-design-vue
晚霞的不甘1 小时前
Flutter for OpenHarmony 进阶实战:打造 60FPS 流畅的物理切水果游戏
javascript·flutter·游戏·云原生·正则表达式
雨季6661 小时前
构建 OpenHarmony 文本高亮关键词标记器:用纯字符串操作实现智能标注
开发语言·javascript·flutter·ui·ecmascript·dart
你这个代码我看不懂1 小时前
Vue子父组件.sync
javascript·vue.js·ecmascript
灰灰勇闯IT2 小时前
Flutter for OpenHarmony:布局组件实战指南
前端·javascript·flutter
⑩-2 小时前
Vue框架学习
前端·vue.js·学习
a程序小傲2 小时前
京东Java面试被问:基于Gossip协议的最终一致性实现和收敛时间
java·开发语言·前端·数据库·python·面试·状态模式
小二·2 小时前
Python Web 开发进阶实战:AI 原生应用商店 —— 在 Flask + Vue 中构建模型即服务(MaaS)与智能体分发平台
前端·人工智能·python
Devlive 开源社区2 小时前
技术日报|推理RAG文档索引PageIndex登顶日增1374星,React视频工具Remotion二连冠进前二
前端·react.js·前端框架