react+echarts

获取返回值画echart

<ReactEcharts option={option} notMerge={true} lazyUpdate={true} />

<Button type="primary" onClick={doLine}>查询</Button>

ReactEcharts --->图表

Button--->按钮查询图表

option 值动态赋值。

const [option, setOption] = useState({})

const doCreatLine = async ()=> {
    lineOption(data)
}

const lineOption = (data: any) => {
  let option = {
      title: {
        text: 'Stacked Line',
        textStyle: {
          color: textColor
        },
        x: 'center'
      },
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['Email', 'Union Ads'],
        textStyle: {
          color: textColor
        },
        y: 'bottom'
      },
      grid: {
        left: '6%',
        right: '6%',
        bottom: '8%',
        containLabel: true
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: data.map((item: { req: any }, index: any) => {  //react 遍历写法和juqery不同
          return item.req
        }),
        axisLine: {
          lineStyle: {
            color: axisLineColor
          }
        }
      },
      yAxis: {
        type: 'value',
        axisLine: {
          lineStyle: {
            color: axisLineColor
          }
        },
        splitLine: {
          lineStyle: {
            color: splitLineColor
          }
        }
      },
      series: [
        {
          name: 'Email',
          type: 'line',
          stack: 'Total',
          data: data.map((item: { email: any }, index: any) => {
            return item.email
          })
        },
        {
          name: 'Union Ads',
          type: 'line',
          stack: 'Total',
          data: data.map((item: { UnionAds: any }, index: any) => {
            return item.UnionAds
          })
        }
      ]
    }
 setOption(option)   // option赋值,调用
}

以下是使用 ReactECharts 和 Slick 实现图表切换的详细步骤及示例代码: ### 1. 创建 React 项目 首先,确保已经安装了 Node.js 和 npm。使用以下命令创建一个新的 React 项目: ```bash npx create-react-app chart-switching-demo cd chart-switching-demo ``` ### 2. 安装依赖 安装 ECharts 和 Slick 的相关依赖: ```bash npm install echarts react-slick slick-carousel ``` ### 3. 编写代码 在 `src` 目录下创建一个 `ChartSwitching.js` 文件,内容如下: ```jsx import React, { useEffect, useRef } from &#39;react&#39;; import echarts from &#39;echarts&#39;; import Slider from &#39;react-slick&#39;; import &#39;slick-carousel/slick/slick.css&#39;; import &#39;slick-carousel/slick/slick-theme.css&#39;; const ChartSwitching = () => { const chartRefs = useRef([]); const settings = { dots: true, infinite: true, speed: 500, slidesToShow: 1, slidesToScroll: 1 }; useEffect(() => { // 初始化每个图表 chartRefs.current.forEach((ref, index) => { const myChart = echarts.init(ref.current); const option = { title: { text: `Chart ${index + 1}` }, xAxis: { type: &#39;category&#39;, data: [&#39;Mon&#39;, &#39;Tue&#39;, &#39;Wed&#39;, &#39;Thu&#39;, &#39;Fri&#39;, &#39;Sat&#39;, &#39;Sun&#39;] }, yAxis: { type: &#39;value&#39; }, series: [{ data: [820, 932, 901, 934, 1290, 1330, 1320], type: &#39;line&#39; }] }; myChart.setOption(option); }); }, []); return ( <Slider {...settings}> {[1, 2, 3].map((_, index) => ( <div key={index}> <div ref={(el) => chartRefs.current[index] = el} style={{ width: &#39;100%&#39;, height: &#39;400px&#39; }}></div> </div> ))} </Slider> ); }; export default ChartSwitching; ``` ### 4. 在 `App.js` 中使用组件 ```jsx import React from &#39;react&#39;; import ChartSwitching from &#39;./ChartSwitching&#39;; const App = () => { return ( <div className="App"> <h1>Chart Switching Demo</h1> <ChartSwitching /> </div> ); }; export default App; ``` ### 5. 运行项目 ```bash npm start ``` ### 代码解释 - **创建 React 项目**:使用 `create-react-app` 工具创建一个新的 React 项目。 - **安装依赖**:安装 `echarts`、`react-slick` 和 `slick-carousel` 库。 - **编写代码**: - 在 `ChartSwitching` 组件中,使用 `useRef` 来保存每个图表的 DOM 引用。 - 使用 `useEffect` 钩子在组件挂载后初始化每个 ECharts 实例,并设置图表选项。 - 使用 `react-slick` 组件实现图表的切换效果。 - **在 `App.js` 中使用组件**:将 `ChartSwitching` 组件引入并渲染到页面中。 ### 注意事项 - 确保 ECharts 和 Slick 的 CSS 文件正确引入,以保证样式正常显示。 - 根据实际需求修改 ECharts 的选项,以展示不同类型的图表。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值