最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Echarts實現(xiàn)點擊列表聯(lián)動餅圖的示例代碼

 更新時間:2023年05月09日 10:20:53   作者:前端養(yǎng)生專家  
本文主要介紹了Echarts實現(xiàn)點擊列表聯(lián)動餅圖的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

簡單易懂的Echars案例,實現(xiàn)點擊列表聯(lián)動餅圖

  • 安裝 echarts-for-react
  • 項目中引入 import ReactECharts from 'echarts-for-react';
  • 開始寫案例啦?。?!

效果圖

1. 首先我們先寫一個左側(cè)列表 StageLine

利用StageItem 將 data 中的數(shù)據(jù)遍歷并展示。

import React = require('react');
import { StageItem } from './StageItem';
const data = [
  { name: '糖', value: '12' },
  { name: '蛋白質(zhì)', 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.接下來我們寫右側(cè)餅圖

我們利用 forwardRef 來接受從父組件傳遞過來的 ref,并將 ref 賦給餅圖組件 ReactECharts。拿到餅圖的ref 我么 就可以實現(xiàn)左右聯(lián)動的效果啦!

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: '蛋白質(zhì)', 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: ` <br/>wppm3vysvbp% `,
      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. 在最外層父級,寫一些方法,進行聯(lián)動操作。

handleClickPie 方法是當我們點擊左側(cè)列表時,對應(yīng)部分的右邊餅圖高亮顯示,點擊當前項,同時要取消上一次點擊項的高亮。那么此時,我們就要拿到,之前高亮的項的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

到此這篇關(guān)于Echarts實現(xiàn)點擊列表聯(lián)動餅圖的示例代碼的文章就介紹到這了,更多相關(guān)Echarts列表聯(lián)動餅圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JS+CSS實現(xiàn)簡易的滑動門效果代碼

    JS+CSS實現(xiàn)簡易的滑動門效果代碼

    這篇文章主要介紹了JS+CSS實現(xiàn)簡易的滑動門效果代碼,涉及JavaScript動態(tài)遍歷及修改頁面元素屬性的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • javascript實現(xiàn)動態(tài)CSS換膚技術(shù)的腳本

    javascript實現(xiàn)動態(tài)CSS換膚技術(shù)的腳本

    javascript實現(xiàn)動態(tài)CSS換膚技術(shù)的腳本...
    2007-06-06
  • 小程序跨頁面交互的作用與方法詳解

    小程序跨頁面交互的作用與方法詳解

    這篇文章主要介紹了小程序跨頁面交互的作用與方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • JS實現(xiàn)頁面數(shù)據(jù)無限加載

    JS實現(xiàn)頁面數(shù)據(jù)無限加載

    這篇文章主要為大家詳細介紹了JS實現(xiàn)頁面數(shù)據(jù)無限加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • JS 事件冒泡 示例代碼

    JS 事件冒泡 示例代碼

    JS 事件冒泡 示例代碼,大家可以參考下。
    2009-07-07
  • Echarts地圖添加引導(dǎo)線效果(labelLine)

    Echarts地圖添加引導(dǎo)線效果(labelLine)

    這篇文章主要介紹了Echarts地圖添加引導(dǎo)線效果(labelLine),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 給before和after偽元素設(shè)置js效果的方法

    給before和after偽元素設(shè)置js效果的方法

    本文通過實例給大家介紹給before和after偽元素設(shè)置js效果的方法,對js偽元素相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • javascript瀏覽器用戶代理檢測腳本實現(xiàn)方法

    javascript瀏覽器用戶代理檢測腳本實現(xiàn)方法

    下面小編就為大家?guī)硪黄猨avascript瀏覽器用戶代理檢測腳本實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • js模擬C#中List的簡單實例

    js模擬C#中List的簡單實例

    本篇文章主要是對js模擬C#中List的簡單實例進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-03-03
  • Javascript中的call()方法介紹

    Javascript中的call()方法介紹

    這篇文章主要介紹了Javascript中的call()方法介紹,本文講解了Call() 語法、Call() 參數(shù)、Javascript中的call()方法、Call()方法的實例等內(nèi)容,需要的朋友可以參考下
    2015-03-03

最新評論

嘉义市| 双柏县| 兴海县| 昌吉市| 东莞市| 黄龙县| 新邵县| 广南县| 嘉黎县| 慈利县| 浦县| 石景山区| 永康市| 莲花县| 余姚市| 鄂尔多斯市| 峡江县| 宁都县| 辉南县| 西林县| 子洲县| 丹阳市| 乐山市| 固原市| 鄄城县| 获嘉县| 襄樊市| 奇台县| 尼勒克县| 明水县| 全椒县| 白城市| 白水县| 靖远县| 壶关县| 延安市| 隆林| 营口市| 勐海县| 伊金霍洛旗| 吉隆县|