Echarts实现点击列表联动饼图的示例代码

 更新时间:2023年05月09日 10:20:53   作者:前端养生专家  
本文主要介绍了Echarts实现点击列表联动饼图的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

简单易懂的Echars案例,实现点击列表联动饼图

  • 安装 echarts-for-react
  • 项目中引入 import ReactECharts from 'echarts-for-react';
  • 开始写案例啦!!!

效果图

1. 首先我们先写一个左侧列表 StageLine

利用StageItem 将 data 中的数据遍历并展示。

import React = require('react');
import { StageItem } from './StageItem';
const data = [
  { name: '糖', value: '12' },
  { name: '蛋白质', value: '24' },
  { name: '脂肪', value: '15' },
];
type StageLineProps = {
  handleClickPie?: (index: number) => void;
  setCurrentIndex?: (o: number) => void;
};
export default function StageLine({
  handleClickPie,
  setCurrentIndex,
}: StageLineProps) {
  return (
    <div>
      {data?.map((item, index) => {
        return (
          <div
            key={index}
            onClick={() => {
              handleClickPie?.(index);
              setCurrentIndex?.(index);
            }}
          >
            <StageItem title={item.name} value={item.value} />
          </div>
        );
      })}
    </div>
  );
}

StageLine 子组件 StageItem

import { Group, Text } from '@mantine/core';
import React = require('react');
type StageItemProps = { title: string; value: string };
export function StageItem({ title, value }: StageItemProps) {
  return (
    <Group
      spacing={2}
      noWrap
      align="center"
      sx={{
        cursor: 'pointer',
        width: 200,
        display: 'flex',
        flexDirection: 'row',
        flexWrap: 'nowrap',
        justifyContent: 'center',
        alignItems: 'stretch',
        alignContent: 'stretch',
        alignSelf: 'normal',
      }}
    >
      <Text
        sx={{
          backgroundColor: '#004399',
          padding: '6px 8px',
          color: 'white',
          minWidth: 40,
          maxWidth: 85,
          lineHeight: '18px',
          alignItems: 'stretch',
          display: 'inline-block',
          flexShrink: 0,
        }}
        size={12}
        weight={600}
      >
        {title}
      </Text>
      <Text
        sx={{
          backgroundColor: 'rgba(0, 67, 153, 0.06)',
          padding: '6px 8px',
          color: '#004399',
          flexGrow: 1,
          '.react-katex ': {
            // height: '26px',
          },
        }}
        size={12}
        weight={600}
      >
        {value ?? '-'}
        {/* <div style={{ display: 'inline-block' }}>{unit}</div> */}
      </Text>
    </Group>
  );
}

2.接下来我们写右侧饼图

我们利用 forwardRef 来接受从父组件传递过来的 ref,并将 ref 赋给饼图组件 ReactECharts。拿到饼图的ref 我么 就可以实现左右联动的效果啦!

import { EChartsInstance } from 'echarts-for-react';
import { log } from 'echarts/types/src/util/log';
import { forwardRef, lazy } from 'react';
import React = require('react');
const ReactECharts = lazy(() => import('echarts-for-react'));
const data = [
  { name: '糖', value: '12' },
  { name: '蛋白质', value: '24' },
  { name: '脂肪', value: '15' },
];
export const Pie = forwardRef<EChartsInstance, any>((_, ref) => {
  // console.log(ref,2)
  const options = {
    color: [
      '#3F72BE',
      '#1F3C88',
      '#2E4FA3',
      '#6B90D8',
      '#8DBEEB',
      '#B6D0F8',
      '#DAEBFB',
      '#F0F8FD',
    ],
    title: {
      text: '能量',
      left: 'center',
      textStyle: {
        fontSize: '16px',
        fontWeight: 700,
      },
    },
    tooltip: {
      trigger: 'item',
      formatter: `{b} <br/>{d}% `,
      showContent: true,
      alwaysShowContent: false,
      displayMode: 'single',
      renderMode: 'auto',
      confine: null,
      showDelay: 0,
      hideDelay: 100,
      transitionDuration: 0.4,
      enterable: true,
      textStyle: {
        fontSize: 14,
        color: '#333',
      },
    },
    legend: {
      itemStyle: {
        borderWidth: 0,
      },
      orient: 'vertical',
      backgroundColor: 'rgba(0,67,153,0.06)',
      bottom: 100 - 17 * (data?.length ?? 0),
      borderRadius: 4,
      itemWidth: 10,
      itemHeight: 10,
      padding: [12, 20],
      itemGap: 7,
      icon: 'rect',
      textStyle: {
        padding: [0, 0, 0, 4],
        color: '#111',
        fontWeight: 400,
        fontFamily: 'sans-serif',
        lineHeight: 15,
        fontSize: 12,
      },
    },
    series: [
      {
        itemStyle: {
          borderWidth: 1,
          borderRadius: 0,
          borderColor: '#fff',
        },
        name: '',
        type: 'pie',
        radius: '80%',
        top: -115,
        label: {
          show: false,
          position: 'center',
        },
        data: data,
        labelLine: {
          show: false,
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
    ],
  };
  return (
    <div>
      <ReactECharts
        ref={ref}
        option={options}
        style={{ width: 250, height: 400, margin: '25px auto' }}
        opts={{ renderer: 'svg' }}
      />
    </div>
  );
});

3. 在最外层父级,写一些方法,进行联动操作。

handleClickPie 方法是当我们点击左侧列表时,对应部分的右边饼图高亮显示,点击当前项,同时要取消上一次点击项的高亮。那么此时,我们就要拿到,之前高亮的项的index(prePieRef)和当前的高亮的项的index(currentIndex)。

clickOutsidePieRef:点击屏幕其他位置时,取消当前高亮

注意📢:这个ref必须给饼图的外层容器,否则不能生效!

import { Box, Group } from '@mantine/core';
import * as React from 'react';
import { useRef, useState } from 'react';
import { Pie } from './Pie';
import StageLine from './StageLine';
import './style.css';
import ReactECharts from 'echarts-for-react';
import { useClickOutside } from '@mantine/hooks';
export default function App() {
  const pieRef = useRef<ReactECharts>(null);
  const prePieRef = useRef(-1);
  const [currentIndex, setCurrentIndex] = useState(-1);
  const handleClickPie = (index: number) => {
    if (prePieRef.current >= 0) {
      pieRef?.current?.getEchartsInstance()?.dispatchAction({
        type: 'downplay',
        seriesIndex: 0,
        dataIndex: prePieRef.current,
      });
    }
    pieRef?.current?.getEchartsInstance()?.dispatchAction({
      type: 'highlight',
      seriesIndex: 0,
      dataIndex: index,
    });
    prePieRef.current = index;
    pieRef?.current?.getEchartsInstance()?.dispatchAction({
      type: 'showTip',
      seriesIndex: 0,
      dataIndex: index,
    });
  };
  const clickOutsidePieRef = useClickOutside(() => {
    const downplay = pieRef?.current?.getEchartsInstance()?.dispatchAction({
      type: 'downplay',
      seriesIndex: 0,
      dataIndex: currentIndex,
    });
    const hideTip = pieRef?.current?.getEchartsInstance()?.dispatchAction({
      type: 'hideTip',
    });
    return { downplay, hideTip };
  });
  console.log(pieRef)
  return (
    <Group>
      <StageLine
        handleClickPie={handleClickPie}
        setCurrentIndex={setCurrentIndex}
      />
      <Box ref={clickOutsidePieRef}>
        <Pie ref={pieRef} />
      </Box>
    </Group>
  );
}

最后附上源码:https://stackblitz.com/edit/react-ts-reghex?file=App.tsx,Pie.tsx,StageItem.tsx,StageLine.tsx

到此这篇关于Echarts实现点击列表联动饼图的示例代码的文章就介绍到这了,更多相关Echarts列表联动饼图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JS如何循环遍历JSON数据

    JS如何循环遍历JSON数据

    这篇文章主要介绍了JS如何循环遍历JSON数据的方法,本文提供了 JS 循环 JSON 数据列,以及 JS 循环遍历 JSON 数据的例子,需要的朋友可以参考下
    2024-01-01
  • Ucren Virtual Desktop V2.0

    Ucren Virtual Desktop V2.0

    Ucren Virtual Desktop V2.0...
    2006-11-11
  • Js和JQuery获取鼠标指针坐标的实现代码分享

    Js和JQuery获取鼠标指针坐标的实现代码分享

    这篇文章主要介绍了Js和JQuery获取鼠标指针坐标的实现代码分享,本文直接给出实现的代码,需要的朋友可以参考下
    2015-05-05
  • Chart.js 轻量级HTML5图表绘制工具库(知识整理)

    Chart.js 轻量级HTML5图表绘制工具库(知识整理)

    这篇文章主要介绍了Chart.js 轻量级HTML5图表绘制工具库,Chart.js基于HTML5 canvas技术支持所有现代浏览器,并且针对IE7/8提供了降级替代方案,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • JavaScript实现HSL拾色器

    JavaScript实现HSL拾色器

    这篇文章主要为大家详细介绍了JavaScript实现HSL拾色器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • javascript实现移动端 HTML5 图片上传预览和压缩功能示例

    javascript实现移动端 HTML5 图片上传预览和压缩功能示例

    这篇文章主要介绍了javascript实现移动端 HTML5 图片上传预览和压缩功能,结合实例形式分析了javascript移动端 HTML5 图片上传预览和压缩功能具体实现方法与操作注意事项,需要的朋友可以参考下
    2020-05-05
  • JavaScript 提升运行速度之循环篇 译文

    JavaScript 提升运行速度之循环篇 译文

    根据Nicholas 的说法,有四种代码 会拖慢脚本的运行,并最终导致脚本失控。分别是次数过多的同步循环、庞大的函数体、不恰当的递归和不合理的DOM 调用。
    2009-08-08
  • js取值中form.all和不加all的区别介绍

    js取值中form.all和不加all的区别介绍

    在js里取值,可以用form.xx.value,也可以用form.all.xx.value,那么js取值中form.all和不加all有什么区别呢?下面就为大家详细介绍下
    2014-01-01
  • window.open打开新页面失效解决方案

    window.open打开新页面失效解决方案

    这篇文章主要给大家介绍了关于window.open打开新页面失效的解决方案,移动端和PC端全部通过window.open()来跳转页面窗口,文中给出了详细的解决方案,需要的朋友可以参考下
    2023-07-07
  • 客户端验证用户名和密码的方法详解

    客户端验证用户名和密码的方法详解

    这篇文章主要介绍了客户端验证用户名和密码 的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06

最新评论