@uiw/react-heat-map
Version:
React component create calendar heatmap to visualize time series data, a la github contribution graph.
103 lines • 4.01 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, oneDayTime, convertPanelColors } 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 || {},
{
rectSize = 11,
legendCellSize = 11,
space = 2,
monthPlacement = 'top',
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 = ['var(--rhm-rect, #EBEDF0)', '#C6E48B', '#7BC96F', '#239A3B', '#196127'],
style
} = _ref,
other = _objectWithoutPropertiesLoose(_ref, _excluded);
var maxCount = Math.max(...value.map(item => item.count), 0);
var panelColorsObject = Array.isArray(panelColors) ? convertPanelColors(panelColors, maxCount) : panelColors;
var [gridNum, setGridNum] = useState(0);
var [leftPad, setLeftPad] = useState(!!weekLabels ? 28 : 5);
var defaultTopPad = monthPlacement === 'top' ? 20 : 5;
var [topPad, setTopPad] = useState(!!monthLabels ? defaultTopPad : 5);
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 !startDate.getDay() ? startDate : new Date(startDate.getTime() - startDate.getDay() * oneDayTime);
} else {
var newDate = new Date();
return new Date(newDate.getTime() - newDate.getDay() * oneDayTime);
}
}, [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
})]
}));
}