React中封装echarts图表组件以及自适应窗口变化

本文介绍了如何在React应用中使用ECharts库创建动态图表,包括引入、接口定义、组件实现以及响应式调整。作者展示了如何设置图表选项和监听窗口大小变化以实现自适应布局。

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

前言

hello world欢迎来到前端的新世界


😜当前文章系列专栏:react.js
🐱‍👓博主在前端领域还有很多知识和技术需要掌握,正在不断努力填补技术短板。(如果出现错误,感谢大家指出)🌹
💖感谢大家支持!您的观看就是作者创作的动力

环境

react版本:^18.2.0
echarts版本:^5.4.3
ts版本:^6.0.0

代码

import * as echarts from 'echarts';

import {useEffect} from "react";
interface ChildProps {
    data: Option;
}
const View = (props:ChildProps)=>{
    useEffect(()=>{
        const myChart = echarts.init(document.getElementById("echarts"))
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        myChart.setOption(props.data)
    },[])
    window.addEventListener("resize",()=>{
        const myChart = echarts.init(document.getElementById("echarts"))
        myChart.resize()
    })
    return (
        <div id="echarts" style={{width:"80vw",height:"50vh "}}>

        </div>
    )
}

export default View;

接口

interface Option{
    xAxis: {
        type: string;
        data: string[];
    };
    yAxis: {
        type: string;
    };
    series: {
        data: number[];
        type: string;
    }[];
    [key: string]: unknown;
}

使用

// 导入
import Graph from "@/components/Graph"
const View = ()=>{
    const option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    // Use axis to trigger tooltip
                    type: 'shadow' // 'shadow' as default; can also be 'line' or 'shadow'
                }
            },
            legend: {},
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type: 'value'
            },
            yAxis: {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            },
            series: [
                {
                    name: 'Direct',
                    type: 'bar',
                    stack: 'total',
                    label: {
                        show: true
                    },
                    emphasis: {
                        focus: 'series'
                    },
                    data: [320, 302, 301, 334, 390, 330, 320]
                },
                {
                    name: 'Mail Ad',
                    type: 'bar',
                    stack: 'total',
                    label: {
                        show: true
                    },
                    emphasis: {
                        focus: 'series'
                    },
                    data: [120, 132, 101, 134, 90, 230, 210]
                },
                {
                    name: 'Affiliate Ad',
                    type: 'bar',
                    stack: 'total',
                    label: {
                        show: true
                    },
                    emphasis: {
                        focus: 'series'
                    },
                    data: [220, 182, 191, 234, 290, 330, 310]
                },
                {
                    name: 'Video Ad',
                    type: 'bar',
                    stack: 'total',
                    label: {
                        show: true
                    },
                    emphasis: {
                        focus: 'series'
                    },
                    data: [150, 212, 201, 154, 190, 330, 410]
                },
                {
                    name: 'Search Engine',
                    type: 'bar',
                    stack: 'total',
                    label: {
                        show: true
                    },
                    emphasis: {
                        focus: 'series'
                    },
                    data: [820, 832, 901, 934, 1290, 1330, 1320]
                }
            ]
        }

    return(
        <div className="sonPage1">
            <Graph data={option}></Graph>

        </div>
    )
}

export default View;

效果

在这里插入图片描述

后言

创作不易,要是本文章对广大读者有那么一点点帮助 不妨三连支持一下,您的鼓励就是博主创作的动力

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鋜斗

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

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

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

打赏作者

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

抵扣说明:

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

余额充值