Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | 4x 4x | import React, {useMemo, useEffect, useRef, useState, MouseEvent} from 'react';
import moment from 'moment';
import {debounce} from 'lodash';
import {Badge, Tooltip} from '../..';
import './index.less';
import scopedClasses from '../../utils/scopedClasses';
import DEFAULT from './default';
import {Data, Status, PathInfo, LLSliceFigureProps, TypeEnum} from './index.d';
const sc = scopedClasses('ll-slice-figure');
const LLSliceFigure = (props: LLSliceFigureProps) => {
const {
type = DEFAULT.type,
format,
auxiliaryText = DEFAULT.auxiliaryText,
auxiliaryRange = DEFAULT.auxiliaryRange,
data,
statusParams = DEFAULT.statusParams,
timeLine,
timeRange = DEFAULT.timeRange,
style = DEFAULT.style
} = props;
const [tooltipContent, setTooltip] = useState<{
isShow: boolean;
time: string;
left: number;
status: string;
}>({
isShow: false,
left: -10000,
time: '',
status: ''
});
const paths = useRef<Array<PathInfo> | null>(null);
const sliceFigureCanvas = useRef<HTMLCanvasElement | null>(null);
const sliceFigureWrapper = useRef<HTMLDivElement | null>(null);
const [canvasWidth, setCanvasWidth] = useState<number>(0);
const duration = useMemo(
() => moment(timeRange[1], format).diff(moment(timeRange[0], format), 'seconds'),
[timeRange]
);
const dpr = window.devicePixelRatio || 1;
const getCanvasPixel = (val = 0) => val * dpr;
const getTruePixel = (val = 0) => val / dpr;
const config = useMemo(
() =>
({
[TypeEnum.RICH]: {
textColor: 'rgba(255, 255, 255, 0.65)',
fontSize: `${getCanvasPixel(14)}px`,
height: getCanvasPixel(128),
contentHeight: getCanvasPixel(40),
contentTop: getCanvasPixel(58),
auxiliaryBarHeight: getCanvasPixel(8),
auxiliaryBarTop: getCanvasPixel(34),
auxiliaryTextTop: getCanvasPixel(10),
timeLineTop: getCanvasPixel(110)
},
[TypeEnum.SIMPLE]: {
textColor: 'rgba(255, 255, 255, 0.45)',
fontSize: `${getCanvasPixel(12)}px`,
height: getCanvasPixel(75),
contentHeight: getCanvasPixel(15),
contentTop: getCanvasPixel(36),
auxiliaryBarHeight: getCanvasPixel(4),
auxiliaryBarTop: getCanvasPixel(24),
auxiliaryTextTop: getCanvasPixel(4),
timeLineTop: getCanvasPixel(59)
}
}[type]),
[type, dpr]
);
/**
* FUN: 初始化canvas
*/
const initCanvas = () => {
const canvas = sliceFigureCanvas.current as HTMLCanvasElement;
canvas.style.width = '100%';
canvas.style.height = `${getTruePixel(config.height)}px`;
canvas.width = canvasWidth;
canvas.height = config.height || 0;
const ctx = canvas?.getContext('2d') as CanvasRenderingContext2D;
ctx.beginPath();
ctx.strokeStyle = 'rgba(89, 112, 221, 0.45)';
ctx.lineWidth = 1;
ctx.strokeRect(0, config.contentTop || 0, canvasWidth - 2, config.contentHeight || 0);
ctx.closePath();
};
/**
* FUN: 清空canvas
*/
const clearCanvas = () => {
const canvas = sliceFigureCanvas.current as HTMLCanvasElement;
const ctx = canvas?.getContext('2d') as CanvasRenderingContext2D;
ctx?.clearRect(0, 0, canvas.width, canvas.height);
};
/**
* FUN: 绘制辅助信息
*/
const paintAuxiliary = () => {
const canvas = sliceFigureCanvas.current as HTMLCanvasElement;
const ctx = canvas?.getContext('2d') as CanvasRenderingContext2D;
// 辅助信息
const auxiliaryLeft =
(moment(auxiliaryRange[0], format).diff(moment(timeRange[0], format), 'seconds') / duration) *
canvasWidth;
const auxiliaryRightToBegin =
(moment(auxiliaryRange[1], format).diff(moment(timeRange[0], format), 'seconds') / duration) *
canvasWidth;
const auxiliaryWidth = auxiliaryRightToBegin - auxiliaryLeft;
ctx.beginPath();
// 辅助线
ctx.fillStyle = 'rgba(89, 112, 221, 0.30)';
ctx.fillRect(
auxiliaryLeft,
config.auxiliaryBarTop || 0,
auxiliaryWidth,
config.auxiliaryBarHeight || 0
);
// 辅助线文字
ctx.font = `${config.fontSize} Microsoft Arial`;
ctx.textBaseline = 'top';
ctx.fillStyle = config.textColor || 'rgba(255, 255, 255, 0.65)';
ctx.fillText(auxiliaryText, auxiliaryLeft, config.auxiliaryTextTop || 0);
ctx.closePath();
};
/**
* FUN: 绘制时间轴
*/
const paintTimeline = () => {
const canvas = sliceFigureCanvas.current as HTMLCanvasElement;
const ctx = canvas?.getContext('2d') as CanvasRenderingContext2D;
timeLine?.forEach((item: string | number, index: number) => {
const left =
(moment(item, format).diff(moment(timeRange[0], format), 'seconds') / duration) *
canvasWidth;
if (index === 0) {
ctx.textAlign = 'left';
} else if (index === timeLine?.length - 1) {
ctx.textAlign = 'right';
} else {
ctx.textAlign = 'center';
}
ctx.font = `${config.fontSize} Microsoft Arial`;
ctx.textBaseline = 'top';
ctx.fillStyle = config.textColor || 'rgba(255, 255, 255, .65)';
ctx.fillText(`${item}`, left, config.timeLineTop || 0);
});
};
/**
* FUN: 绘制主信息
*/
const paintData = () => {
const canvas = sliceFigureCanvas.current as HTMLCanvasElement;
const ctx = canvas?.getContext('2d') as CanvasRenderingContext2D;
paths.current = [];
data?.forEach((item: Data) => {
const {beginTime: ibt, endTime: iet, status} = item;
const itemDuration = moment(iet, format).diff(moment(ibt, format), 'seconds');
const left =
(moment(ibt, format).diff(moment(timeRange[0], format), 'seconds') / duration) *
canvasWidth;
const width = (canvasWidth * itemDuration) / duration;
const path = new Path2D();
ctx.fillStyle = statusParams[status]?.color || 'transparent';
path.rect(left, config.contentTop || 0, width, config.contentHeight || 0);
ctx.fill(path);
paths?.current?.push({
path,
status
});
});
};
useEffect(() => {
const handleResize = () => {
// 自适应宽度:获取宽度
const wrapperBody = sliceFigureWrapper.current as HTMLDivElement;
setCanvasWidth(getCanvasPixel(wrapperBody.getBoundingClientRect().width || DEFAULT.width));
};
setTimeout(() => {
handleResize();
}, 0);
const debounceResize = debounce(handleResize, 150);
window.addEventListener('resize', debounceResize);
return () => {
window.removeEventListener('resize', debounceResize);
};
}, []);
useEffect(() => {
if (
canvasWidth > 0 &&
Array.isArray(data) &&
data.length > 0 &&
statusParams &&
Array.isArray(timeRange) &&
timeRange[0] &&
timeRange[1]
) {
initCanvas();
paintAuxiliary();
paintTimeline();
paintData();
}
return () => {
clearCanvas();
};
}, [canvasWidth, data, format, statusParams, timeLine, timeRange, type]);
// tooltip
useEffect(() => {
const canvas = sliceFigureCanvas.current as HTMLCanvasElement;
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
const pick = (event: MouseEvent) => {
// 获取鼠标坐标
const x = event.clientX - canvas.getBoundingClientRect().left;
const y = event.clientY - canvas.getBoundingClientRect().top;
if (
y >= getTruePixel(config.contentTop) &&
y <= getTruePixel(config.contentTop) + getTruePixel(config.contentHeight)
) {
const time = moment(timeRange[0], format)
.add((x / getTruePixel(canvasWidth)) * duration, 'seconds')
.format(format);
const status =
paths?.current?.find(item =>
ctx.isPointInPath(item?.path, getCanvasPixel(x), getCanvasPixel(y))
)?.status || '';
if (status && time) {
setTooltip({
left: x,
isShow: true,
time: `${time}`,
status
});
return;
}
}
setTooltip({
left: -10000,
isShow: false,
time: '',
status: ''
});
};
const debouncePick: any = debounce(pick, 100);
canvas?.addEventListener('mousemove', debouncePick);
return () => {
canvas?.removeEventListener('mousemove', debouncePick);
};
}, [canvasWidth]);
return (
<div ref={sliceFigureWrapper} className={`ll-slice-figure ${type}`} style={style}>
<canvas ref={sliceFigureCanvas} className={sc('canvas')}>
您的浏览器暂不支持canvas
</canvas>
{/* 提示文字 */}
{type === TypeEnum.RICH && (
<Tooltip
getPopupContainer={(triggerNode): HTMLElement => triggerNode}
arrowPointAtCenter
placement="top"
title={
<div className={sc('auxiliary-tooltip')}>
<span className={sc('auxiliary-tooltip-time')}>{tooltipContent?.time}</span>
<span className={sc('auxiliary-tooltip-status')}>
<Badge
color={statusParams[tooltipContent.status]?.color}
text={statusParams[tooltipContent.status]?.text}
/>
</span>
</div>
}
visible={!!tooltipContent.isShow}
>
<div
className={sc('auxiliary-tooltip-container')}
style={{
top: `${getTruePixel(config.contentTop)}px`,
height: `${getTruePixel(config.contentHeight)}px`,
left: `${tooltipContent?.left}px`
}}
/>
</Tooltip>
)}
{/* 状态栏 */}
{type === TypeEnum.RICH && (
<div className={sc('status')}>
{Object.values(statusParams)?.map((item: Status) => (
<Badge key={item.text} color={item.color} text={item.text} />
))}
</div>
)}
</div>
);
};
export default LLSliceFigure;
export * from './index.d';
|