@uiw/react-heat-map
Version:
React component create calendar heatmap to visualize time series data, a la github contribution graph.
115 lines • 4.82 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
var _excluded = ["rectSize", "legendCellSize", "space", "monthPlacement", "startDate", "endDate", "rectProps", "rectRender", "legendRender", "value", "weekLabels", "monthLabels", "panelColors", "style"];
import React, { useEffect, useMemo, useState } from 'react';
import { LabelsWeek } from "./LabelsWeek.js";
import { LabelsMonth } from "./LabelsMonth.js";
import { isValidDate, convertPanelColors, getStartOfWeek } from "./utils.js";
import Legend from "./Legend.js";
import { Day } from "./Day.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export default function SVG(props) {
var _ref = props || {},
_ref$rectSize = _ref.rectSize,
rectSize = _ref$rectSize === void 0 ? 11 : _ref$rectSize,
_ref$legendCellSize = _ref.legendCellSize,
legendCellSize = _ref$legendCellSize === void 0 ? 11 : _ref$legendCellSize,
_ref$space = _ref.space,
space = _ref$space === void 0 ? 2 : _ref$space,
_ref$monthPlacement = _ref.monthPlacement,
monthPlacement = _ref$monthPlacement === void 0 ? 'top' : _ref$monthPlacement,
_ref$startDate = _ref.startDate,
startDate = _ref$startDate === void 0 ? new Date() : _ref$startDate,
endDate = _ref.endDate,
rectProps = _ref.rectProps,
rectRender = _ref.rectRender,
legendRender = _ref.legendRender,
_ref$value = _ref.value,
value = _ref$value === void 0 ? [] : _ref$value,
_ref$weekLabels = _ref.weekLabels,
weekLabels = _ref$weekLabels === void 0 ? ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] : _ref$weekLabels,
_ref$monthLabels = _ref.monthLabels,
monthLabels = _ref$monthLabels === void 0 ? ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] : _ref$monthLabels,
_ref$panelColors = _ref.panelColors,
panelColors = _ref$panelColors === void 0 ? ['var(--rhm-rect, #EBEDF0)', '#C6E48B', '#7BC96F', '#239A3B', '#196127'] : _ref$panelColors,
style = _ref.style,
other = _objectWithoutPropertiesLoose(_ref, _excluded);
var maxCount = Math.max(...value.map(item => item.count), 0);
var panelColorsObject = Array.isArray(panelColors) ? convertPanelColors(panelColors, maxCount) : panelColors;
var _useState = useState(0),
gridNum = _useState[0],
setGridNum = _useState[1];
var _useState2 = useState(!!weekLabels ? 28 : 5),
leftPad = _useState2[0],
setLeftPad = _useState2[1];
var defaultTopPad = monthPlacement === 'top' ? 20 : 5;
var _useState3 = useState(!!monthLabels ? defaultTopPad : 5),
topPad = _useState3[0],
setTopPad = _useState3[1];
var svgRef = /*#__PURE__*/React.createRef();
useEffect(() => setLeftPad(!!weekLabels ? 28 : 5), [weekLabels]);
useEffect(() => {
if (svgRef.current) {
var width = svgRef.current.clientWidth - leftPad || 0;
setGridNum(Math.floor(width / (rectSize + space)) || 0);
}
}, [rectSize, svgRef, space, leftPad]);
useEffect(() => {
setTopPad(!!monthLabels ? defaultTopPad : 5);
}, [monthLabels]);
var initStartDate = useMemo(() => {
if (isValidDate(startDate)) {
return getStartOfWeek(startDate);
} else {
return getStartOfWeek(new Date());
}
}, [startDate]);
var styl = {
color: 'var(--rhm-text-color, #24292e)',
userSelect: 'none',
display: 'block',
fontSize: 10
};
var monthRectY = monthPlacement === 'top' ? 15 : 15 * 7 + space;
var legendTopPad = monthPlacement === 'top' ? topPad + rectSize * 8 + 6 : (!!monthLabels ? topPad + rectSize + space : topPad) + rectSize * 8 + 6;
return /*#__PURE__*/_jsxs("svg", _extends({
ref: svgRef,
style: _extends({}, styl, style)
}, other, {
children: [legendCellSize !== 0 && /*#__PURE__*/_jsx(Legend, {
legendRender: legendRender,
panelColors: panelColorsObject,
rectSize: rectSize,
rectY: legendTopPad,
legendCellSize: legendCellSize,
leftPad: leftPad,
topPad: topPad,
space: space
}), /*#__PURE__*/_jsx(LabelsWeek, {
weekLabels: weekLabels,
rectSize: rectSize,
space: space,
topPad: topPad
}), /*#__PURE__*/_jsx(LabelsMonth, {
monthLabels: monthLabels,
rectSize: rectSize,
space: space,
leftPad: leftPad,
colNum: gridNum,
rectY: monthRectY,
startDate: initStartDate,
endDate: endDate
}), /*#__PURE__*/_jsx(Day, {
transform: "translate(" + leftPad + ", " + topPad + ")",
gridNum: gridNum,
initStartDate: initStartDate,
endDate: endDate,
rectProps: rectProps,
rectSize: rectSize,
rectRender: rectRender,
panelColors: panelColorsObject,
value: value,
space: space
})]
}));
}