react中echarts柱状图渐变色

import React, { useEffect, useRef } from 'react'
import * as echarts from 'echarts'

const CarAgeEcharts = (props) => {
    const chartRef = useRef()
    console.log(props)


    useEffect(() => {
        console.log(props)

        var salvProName = [];
        var salvProValue = [];
        if (props?.info?.length) {
            salvProName = props?.info[0];
            salvProValue = props?.info[1];
        }
        var salvProMax = [];//背景按最大值
        let bigNum = 0
        // 取返回数据中的最大值作为背景色
        bigNum = Math.max.apply(null, props?.info[1])
        for (let i = 0; i < salvProValue.length; i++) {
            salvProMax.push(bigNum)
        }
        const options = {
            grid: {
                // 左右边距,调节整个echarts距离外面box的位置
                left: 50,
                bottom: 30,
                top: 10,
            },
            xAxis: {
                type: 'category',
                data: props?.info[0],
                axisTick: {
                    show: false,

                },
                axisLabel: {
                    interval: 0
                }
            },
            yAxis: {
                type: 'value',
                splitLine: {
                    //分割线配置
                    show: true,
                    lineStyle: {
                        color: "rgba(48,170,219,0.15)",
                    },
                },
                axisLabel: {
                    //y轴文字的配置
                    textStyle: {
                        color: "#ffffff",
                        margin: 15,
                    },
                }
            },
            series: [
                {
                    data: props?.info[1],
                    type: 'bar',
                    smooth: true,
                    barWidth: 20,
                    itemStyle: {
                        normal: {
                            barBorderRadius: 10,
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                offset: 0,
                                color: '#18FFE1'
                            }, {
                                offset: 0.5,
                                color: '#18EBFF'
                            }, {
                                offset: 1,
                                color: '#00A2FF'
                            }]), // 柱状图的渐变色
                        },
                    },
                },
                {
                    name: '背景',
                    type: 'bar',
                    barWidth: 20,
                    barGap: '-100%',
                    data: salvProMax,
                    itemStyle: {
                        normal: {
                            color: 'rgba(0, 123, 177, 0.2)',
                            barBorderRadius: 30,
                        }
                    },
                },
            ]
        };
        // 创建一个echarts实例,返回echarts实例。不能在单个容器中创建多个echarts实例
        const chart = echarts.init(chartRef.current)

        // 设置图表实例的配置项和数据
        chart.setOption(options)

        // 组件卸载
        return () => {
            // myChart.dispose() 销毁实例。实例销毁后无法再被使用
            chart.dispose()
        }
    }, [props])

    return (
        // 把图表封装单独放入一个组件中
        <div style={{ width: "100%", height: "100%" }} ref={chartRef}></div>
    )
}
export default CarAgeEcharts
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值