Skip to content

Commit

Permalink
Merge pull request #3 from ziqiya/master
Browse files Browse the repository at this point in the history
feat: 新增大屏自适应缩放 hook
  • Loading branch information
chj-damon authored Dec 24, 2024
2 parents 037f887 + c27c126 commit 1ff66cc
Show file tree
Hide file tree
Showing 14 changed files with 13,768 additions and 14,541 deletions.
28,109 changes: 13,669 additions & 14,440 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions src/context.ts

This file was deleted.

47 changes: 0 additions & 47 deletions src/hooks/useCalcHeight.ts

This file was deleted.

70 changes: 70 additions & 0 deletions src/hooks/useScreenScale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* @文件描述: 大屏缩放自适应屏幕 hook
* @公司: thundersdata
* @作者: 阮旭松
* @Date: 2023-11-07 11:00:14
* @LastEditors: 阮旭松
* @LastEditTime: 2024-12-24 17:53:33
*/
import { useEffect, useRef, useState } from 'react';

// 初始缩放比例
const INITIAL_SCALE = 1;

export function useScreenScale(screenWidth: number, screenHeight: number) {
const [scale, setScale] = useState<number>(INITIAL_SCALE);
const [isReady, setIsReady] = useState(false);
const screenDomRef = useRef<HTMLDivElement | null>(null);

/** 对 echarts 图表进行反缩放,修复 zoom 以后 tooltip 偏移问题 */
useEffect(() => {
const canvasDoms = document.getElementsByTagName('canvas');
Array.from(canvasDoms).forEach((item: any) => {
if (!item || item.id) return;
item.style.zoom = `calc(1/${scale})`;
item.style.transformOrigin = 'left top';
item.style.transform = `scale(${scale})`;
});
}, [scale]);

/** 对 dom 进行缩放 */
const handelDomScale = (dom: HTMLElement | null, scale: number) => {
if (!dom) return;
dom.style.transform = 'translate(-50%)';
dom.style.zoom = `${scale}`;
};

/** 设置缩放比例 */
const setScreenScale = () => {
const newScale =
document.documentElement.clientWidth / document.documentElement.clientHeight < screenWidth / screenHeight
? document.documentElement.clientWidth / screenWidth
: document.documentElement.clientHeight / screenHeight;
setScale(newScale);
if (screenDomRef.current) {
handelDomScale(screenDomRef.current, newScale);
}
};

/** 设置缩放比例 */
useEffect(() => {
setScreenScale(); // 初始化时调用一次

// 监听屏幕宽度改变事件
const resizeObserver = new ResizeObserver(setScreenScale);
resizeObserver.observe(document.documentElement); // 观察 documentElement 的尺寸变化

return () => {
resizeObserver.disconnect(); // 清理 ResizeObserver
};
}, [screenWidth, screenHeight]);

useEffect(() => {
if (scale) {
// 当 scale 计算完成后再渲染 Outlet
setIsReady(true);
}
}, [scale]);

return { isReady, scale, screenDomRef };
}
9 changes: 9 additions & 0 deletions src/layouts/BasicLayout/index.module.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
height: 100vh;
width: 100vw;
position: relative;
overflow: hidden;
background-color: rgb(7, 8, 20);

.screen {
transform-origin: 0 0;
position: fixed;
left: 50%;
background: transparent;
}
}

.background {
Expand Down
10 changes: 8 additions & 2 deletions src/layouts/BasicLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Outlet } from '@umijs/max';

import { SCREEN_HEIGHT, SCREEN_WIDTH } from '@/constant';
import { useScreenScale } from '@/hooks/useScreenScale';

import styles from './index.module.less';

export default function BasicLayout() {
const { isReady, screenDomRef } = useScreenScale(SCREEN_WIDTH, SCREEN_HEIGHT);
return (
<div className={styles.screenWrapper}>
<div className={styles.background} />
<Outlet />
<div className={styles.screen} ref={screenDomRef} style={{ width: SCREEN_WIDTH, height: SCREEN_HEIGHT }}>
<div className={styles.background} />
{isReady && <Outlet />}
</div>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.container {
height: 100%;
margin-top: 100px;
}
7 changes: 1 addition & 6 deletions src/pages/homepage/components/Left/Left1/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { useContext } from 'react';

import { BarLine } from '@td-design/lego';

import { ScreenContext } from '@/context';

export default () => {
const echartsHeight = useContext(ScreenContext);
return (
<BarLine
xAxisData={['03月', '04月']}
yAxis={[{ name: '万元' }, { name: '%' }]}
lineData={{ name: '同比增长率', data: [12, 11] }}
barData={{ name: '运费', data: [500, 584] }}
style={{ width: '100%', height: echartsHeight }}
style={{ width: '100%', height: 300 }}
/>
);
};
7 changes: 1 addition & 6 deletions src/pages/homepage/components/Left/Left2/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { useContext } from 'react';

import { BarLine } from '@td-design/lego';

import { ScreenContext } from '@/context';

export default () => {
const echartsHeight = useContext(ScreenContext);
return (
<BarLine
style={{ width: '100%', height: echartsHeight }}
style={{ width: '100%', height: 300 }}
xAxisData={['03月', '04月', '05月', '06月', '07月', '08月']}
yAxis={[{ name: '万元' }, { name: '%' }]}
lineData={{ name: '同比增长率', data: [12, 11, 19, 23, 32, 45] }}
Expand Down
8 changes: 1 addition & 7 deletions src/pages/homepage/components/Left/Left3/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { useContext } from 'react';

import { BarLine } from '@td-design/lego';

import { ScreenContext } from '@/context';

export default () => {
const echartsHeight = useContext(ScreenContext);

return (
<BarLine
style={{ width: '100%', height: echartsHeight }}
style={{ width: '100%', height: 300 }}
xAxisData={['03月', '04月', '05月', '06月', '07月', '08月']}
yAxis={[{ name: '万辆' }, { name: '%' }]}
lineData={{ name: '同比增长率', data: [12, 11, 19, 23, 32, 45] }}
Expand Down
8 changes: 1 addition & 7 deletions src/pages/homepage/components/Right/Right1/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { useContext } from 'react';

import { BarLine } from '@td-design/lego';

import { ScreenContext } from '@/context';

export default () => {
const echartsHeight = useContext(ScreenContext);

return (
<BarLine
xAxisData={['03月', '04月']}
yAxis={[{ name: '万元' }, { name: '%' }]}
lineData={{ name: '同比增长率', data: [12, 11] }}
barData={{ name: '运费', data: [500, 584] }}
style={{ width: '100%', height: echartsHeight }}
style={{ width: '100%', height: 300 }}
/>
);
};
8 changes: 1 addition & 7 deletions src/pages/homepage/components/Right/Right2/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { useContext } from 'react';

import { BarLine } from '@td-design/lego';

import { ScreenContext } from '@/context';

export default () => {
const echartsHeight = useContext(ScreenContext);

return (
<BarLine
style={{ width: '100%', height: echartsHeight }}
style={{ width: '100%', height: 300 }}
xAxisData={['03月', '04月', '05月', '06月', '07月', '08月']}
yAxis={[{ name: '万元' }, { name: '%' }]}
lineData={{ name: '同比增长率', data: [12, 11, 19, 23, 32, 45] }}
Expand Down
8 changes: 1 addition & 7 deletions src/pages/homepage/components/Right/Right3/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { useContext } from 'react';

import { BarLine } from '@td-design/lego';

import { ScreenContext } from '@/context';

export default () => {
const echartsHeight = useContext(ScreenContext);

return (
<BarLine
style={{ width: '100%', height: echartsHeight }}
style={{ width: '100%', height: 300 }}
xAxisData={['03月', '04月', '05月', '06月', '07月', '08月']}
yAxis={[{ name: '万辆' }, { name: '%' }]}
lineData={{ name: '同比增长率', data: [12, 11, 19, 23, 32, 45] }}
Expand Down
14 changes: 5 additions & 9 deletions src/pages/homepage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import CustomLayout from '@/components/CustomLayout';
import Title from '@/components/Title';
import { ScreenContext } from '@/context';
import useCalcHeight from '@/hooks/useCalcHeight';

import Center1 from './components/Center/Center1';
import Center2 from './components/Center/Center2';
Expand All @@ -13,15 +11,13 @@ import Right2 from './components/Right/Right2';
import Right3 from './components/Right/Right3';

export default () => {
const { parentRef, headerRef, contentRef, echartsHeight, observe } = useCalcHeight();

return (
<ScreenContext.Provider value={echartsHeight}>
<CustomLayout ref={parentRef}>
<CustomLayout.Header ref={headerRef} bgUrl={require('../../assets/header-bg.png')} onLoad={observe}>
<>
<CustomLayout>
<CustomLayout.Header bgUrl={require('../../assets/header-bg.png')}>
<Title>雷数数据可视化大屏</Title>
</CustomLayout.Header>
<CustomLayout.Content ref={contentRef}>
<CustomLayout.Content>
<CustomLayout.Left>
<CustomLayout.Item>
<Left1 />
Expand Down Expand Up @@ -50,6 +46,6 @@ export default () => {
</CustomLayout.Right>
</CustomLayout.Content>
</CustomLayout>
</ScreenContext.Provider>
</>
);
};

0 comments on commit 1ff66cc

Please sign in to comment.