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

简介: 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;

接口

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;


效果


后言

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


目录
相关文章
|
3月前
|
资源调度 前端开发 JavaScript
React 的antd-mobile 组件库,嵌套路由
React 的antd-mobile 组件库,嵌套路由
89 0
|
1月前
|
前端开发
Vue3 【仿 react 的 hook】封装 useTitle
Vue3 【仿 react 的 hook】封装 useTitle
17 0
|
1月前
|
前端开发 API
Vue3 【仿 react 的 hook】封装 useLocation
Vue3 【仿 react 的 hook】封装 useLocation
16 0
|
2月前
|
前端开发 API
|
3月前
|
SQL 存储 前端开发
React&Nest.js全栈社区平台(五)——👋封装通用分页Service实现文章流与详情
React&Nest.js全栈社区平台(五)——👋封装通用分页Service实现文章流与详情
React&Nest.js全栈社区平台(五)——👋封装通用分页Service实现文章流与详情
|
3月前
|
前端开发 JavaScript 定位技术
Docusaurus框架——react+antd+echarts自定义mdx生成图表代码解释文档
Docusaurus框架——react+antd+echarts自定义mdx生成图表代码解释文档
83 0
|
3月前
|
存储 前端开发 中间件
React组件间的通信
React组件间的通信
37 1
|
3月前
|
前端开发 JavaScript API
React组件生命周期
React组件生命周期
100 1
|
3月前
|
存储 前端开发 JavaScript
探索 React Hooks 的世界:如何构建出色的组件(下)
探索 React Hooks 的世界:如何构建出色的组件(下)
探索 React Hooks 的世界:如何构建出色的组件(下)
|
3月前
|
前端开发 应用服务中间件 数据库
react服务端组件
react服务端组件
39 0

热门文章

最新文章