React+antd Carousel 走馬燈動態(tài)渲染echarts圖表并自動切換

          使用id掛載echarts圖表,每個echarts圖表數(shù)據(jù)給定一個唯一的id。


           const lineBoxRef = useRef<any>();
           const lineCarouselRef = useRef<any>(null);

            // setLineData()存放多個echarts圖表數(shù)據(jù)
           const [lineData, setLineData] = useState<any>([]);

           const [botClientHeight, setBotClientHeight] = useState<any>([]);
           const [botClientWidth, setBotClientWidth] = useState<any>([]);

             <div style={{  width: '300px',height: "260px"}} ref={lineBoxRef}>
                <Carousel
                  infinite={false}
                  ref={lineCarouselRef}
                  dotPosition={ lineData && lineData.length > 1 ? 'right' : undefined }
                  autoplay
                  key={topClientWidth + topClientHeight}
                  afterChange={(current) => {
                    if (lineData.length - 1 == current) {
                      setTimeout(() => {
                        if (!lineCarouselRef) return;
                        lineCarouselRef.current?.goTo(0, false);
                      }, 2000);
                    }
                  }}
                >
                  {lineData?.map((item: any) => (
                    <div key={item.paltoId}>
                      <div
                        style={{
                          width: centerClientWidth,
                          height: centerClientHeight,
                        }}
                        id={'lineChartId' + item.lineId}
                      ></div>
                    </div>
                  ))}
                </Carousel>
              </div>
 useEffect(() => {
    lineData.map((item: any) => {
      let rateChart = document.getElementById('lineChartId' + item.lineId);
      if (!rateChart) return;
      let rateLineChart = echarts.init(rateChart);
      if (!rateLineChart) return;
      rateLineChart.clear();
      rateLineChart.resize();

      item && rateLineChart.setOption(item);
    });
  }, [botClientHeight, botClientWidth]);

 // flex布局使用clientWidth,clientHeight獲取走馬燈父容器寬高
  useEffect(() => {
    if (!lineData || lineData.length == 0) return;
    setBotClientHeight(lineBoxRef.current.clientHeight);
    setBotClientWidth(lineBoxRef.current.clientWidth);
  }, [lineData]);

  // echarts 數(shù)據(jù)處理
  setLineData(echarts數(shù)據(jù));
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容