forked from uiwjs/react-heat-map
/
SVG.tsx
169 lines (161 loc) · 5.76 KB
/
SVG.tsx
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
import React, { useEffect, useMemo, useState } from 'react';
import { LabelsWeek } from './LabelsWeek';
import { LabelsMonth } from './LabelsMonth';
import { Rect } from './Rect';
import { formatData, getDateToString, existColor, numberSort, isValidDate, oneDayTime } from './utils';
import Legend, { LegendProps } from './Legend';
export type HeatMapValue = {
date: string;
content: string | string[] | React.ReactNode;
count: number;
};
export type RectProps<T = SVGRectElement> = React.SVGProps<T>;
export interface SVGProps extends React.SVGProps<SVGSVGElement> {
startDate?: Date;
endDate?: Date;
rectSize?: number;
legendCellSize?: number;
showLegend?: boolean;
space?: number;
rectProps?: RectProps;
legendRender?: LegendProps['legendRender'];
rectRender?: <E = SVGRectElement>(
data: E & { key: number },
valueItem: HeatMapValue & {
column: number;
row: number;
index: number;
},
) => React.ReactElement | void;
value?: Array<HeatMapValue>;
weekLabels?: string[] | false;
monthLabels?: string[] | false;
panelColors?: Record<number, string>;
}
export default function SVG(props: SVGProps) {
const {
rectSize = 11,
legendCellSize = 11,
showLegend = true,
space = 2,
startDate = new Date(),
endDate,
rectProps,
rectRender,
legendRender,
value = [],
weekLabels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
monthLabels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
panelColors = { 0: '#EBEDF0', 8: '#7BC96F', 4: '#C6E48B', 12: '#239A3B', 32: '#196127' },
...other
} = props || {};
const [gridNum, setGridNum] = useState(0);
const [leftPad, setLeftPad] = useState(!!weekLabels ? 28 : 5);
const [topPad, setTopPad] = useState(!!monthLabels ? 20 : 5);
const svgRef = React.createRef<SVGSVGElement>();
const nums = useMemo(() => numberSort(Object.keys(panelColors).map((item) => parseInt(item, 10))), [panelColors]);
const data = useMemo(() => formatData(value), [value]);
useEffect(() => setLeftPad(!!weekLabels ? 28 : 5), [weekLabels]);
useEffect(() => {
if (svgRef.current) {
const width = svgRef.current.clientWidth - leftPad || 0;
setGridNum(Math.floor(width / (rectSize + space)) || 0);
}
}, [rectSize, svgRef, space, leftPad]);
useEffect(() => {
setTopPad(!!monthLabels ? 20 : 5);
}, [monthLabels]);
const initStartDate = useMemo(() => {
if (isValidDate(startDate)) {
console.log('startDate=', startDate);
return new Date(startDate.getTime())
} else {
const newDate = new Date();
const s = new Date(newDate.getTime() - newDate.getDay() * oneDayTime);
console.log('s=', s);
return s;
}
}, [startDate]);
return (
<div style={{ position: 'relative' }}>
<div style={{ position: 'absolute', top: 0, right: 20, height: '30px' }}>
{showLegend && legendCellSize !== 0 && (
<Legend
legendRender={legendRender}
panelColors={panelColors}
rectSize={rectSize}
legendCellSize={legendCellSize}
leftPad={leftPad}
topPad={topPad}
space={space}
/>
)}
</div>
<svg ref={svgRef} {...other} height={180}>
<LabelsWeek weekLabels={weekLabels} rectSize={rectSize} space={space} topPad={topPad} />
<LabelsMonth
monthLabels={monthLabels}
rectSize={rectSize}
space={space}
leftPad={leftPad}
colNum={gridNum}
startDate={initStartDate}
endDate={endDate}
/>
<g transform={`translate(${leftPad}, ${topPad})`}>
{gridNum > 0 &&
[...Array(gridNum)].map((_, idx) => {
return (
<g key={idx} data-column={idx}>
{[...Array(7)].map((_, cidx) => {
const dayProps: RectProps = {
...rectProps,
key: cidx,
fill: '#EBEDF0',
width: rectSize,
height: rectSize,
x: idx * (rectSize + space) + 25,
y: (rectSize + space) * cidx + 15,
};
const currentDate = new Date(initStartDate.getTime() + oneDayTime * (idx * 7 + cidx));
const date = getDateToString(currentDate);
console.log('daten=', date);
const dataProps = {
...data[date],
date: date,
row: cidx,
column: idx,
index: idx * 7 + cidx,
};
if (endDate instanceof Date && currentDate.getTime() > endDate.getTime()) {
return null;
}
if (date && data[date] && panelColors && Object.keys(panelColors).length > 0) {
dayProps.fill = existColor(data[date].count || 0, nums, panelColors);
} else if (panelColors && panelColors[0]) {
dayProps.fill = panelColors[0];
}
if (rectRender && typeof rectRender === 'function') {
const elm = rectRender({ ...dayProps, key: cidx }, dataProps);
if (elm && React.isValidElement(elm)) {
return elm;
}
}
return (
<Rect
{...dayProps}
data-date={date}
data-index={dataProps.index}
data-row={dataProps.row}
data-column={dataProps.column}
/>
);
})}
</g>
);
})}
</g>
</svg>
</div>
);
}