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


    前言

    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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    接口

    interface Option{
        xAxis: {
            type: string;
            data: string[];
        };
        yAxis: {
            type: string;
        };
        series: {
            data: number[];
            type: string;
        }[];
        [key: string]: unknown;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    使用

    // 导入
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98

    效果

    在这里插入图片描述

    后言

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

  • 相关阅读:
    问遍了身边的面试官朋友,我整理出这份 Java 集合高频面试题(2021年最新版)
    Elasticsearch实战:Centos 8 安装 Elasticsearch 8
    OpenGL ES glfw 下载和使用
    【JMeter】后置处理器的分类以及场景介绍
    2024年跳槽面试心得
    剑指offer-62-圆圈中最后剩下的数字
    存档&改造【07】多表查询和可操控对象的存储
    数字孪生景区丨直观展示景区整体态势,提高运行效益及管理效率
    m基于MATLAB的通信系统仿真,包括信号源,载波信号,放大器,带宽滤波器,接收端包括放大器,带宽滤波器,载波解调,低通滤波器等
    Dubbo笔记
  • 原文地址:https://blog.csdn.net/weixin_68537796/article/details/134515110