@uiw/react-heat-map
Version:
React component create calendar heatmap to visualize time series data, a la github contribution graph.
163 lines (152 loc) • 6.13 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
var _excluded = ["rectSize", "legendCellSize", "space", "startDate", "endDate", "rectProps", "rectRender", "legendRender", "value", "weekLables", "monthLables", "panelColors"];
import React, { useEffect, useMemo, useState } from 'react';
import { LablesWeek } from './LablesWeek';
import { LablesMonth } from './LablesMonth';
import { Rect } from './Rect';
import { formatData, getDateToString, existColor, numberSort, isValidDate, oneDayTime } from './utils';
import Legend from './Legend';
import { jsx as _jsx } from "react/jsx-runtime";
import { 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$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$weekLables = _ref.weekLables,
weekLables = _ref$weekLables === void 0 ? ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] : _ref$weekLables,
_ref$monthLables = _ref.monthLables,
monthLables = _ref$monthLables === void 0 ? ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] : _ref$monthLables,
_ref$panelColors = _ref.panelColors,
panelColors = _ref$panelColors === void 0 ? {
0: '#EBEDF0',
8: '#7BC96F',
4: '#C6E48B',
12: '#239A3B',
32: '#196127'
} : _ref$panelColors,
other = _objectWithoutPropertiesLoose(_ref, _excluded);
var _useState = useState(0),
gridNum = _useState[0],
setGridNum = _useState[1];
var _useState2 = useState(!!weekLables ? 28 : 5),
leftPad = _useState2[0],
setLeftPad = _useState2[1];
var _useState3 = useState(!!monthLables ? 20 : 5),
topPad = _useState3[0],
setTopPad = _useState3[1];
var svgRef = /*#__PURE__*/React.createRef();
var nums = useMemo(function () {
return numberSort(Object.keys(panelColors).map(function (item) {
return parseInt(item, 10);
}));
}, [panelColors]);
var data = useMemo(function () {
return formatData(value);
}, [value]);
useEffect(function () {
return setLeftPad(!!weekLables ? 28 : 5);
}, [weekLables]);
useEffect(function () {
if (svgRef.current) {
var width = svgRef.current.clientWidth - leftPad || 0;
setGridNum(Math.floor(width / (rectSize + space)) || 0);
}
}, [rectSize, svgRef, space, leftPad]);
useEffect(function () {
setTopPad(!!monthLables ? 20 : 5);
}, [monthLables]);
var initStartDate = useMemo(function () {
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]);
return /*#__PURE__*/_jsxs("svg", _extends({
ref: svgRef
}, other, {
children: [legendCellSize !== 0 && /*#__PURE__*/_jsx(Legend, {
legendRender: legendRender,
panelColors: panelColors,
rectSize: rectSize,
legendCellSize: legendCellSize,
leftPad: leftPad,
topPad: topPad,
space: space
}), /*#__PURE__*/_jsx(LablesWeek, {
weekLables: weekLables,
rectSize: rectSize,
space: space,
topPad: topPad
}), /*#__PURE__*/_jsx(LablesMonth, {
monthLables: monthLables,
rectSize: rectSize,
space: space,
leftPad: leftPad,
colNum: gridNum,
startDate: initStartDate
}), /*#__PURE__*/_jsx("g", {
transform: "translate(" + leftPad + ", " + topPad + ")",
children: gridNum > 0 && [].concat(Array(gridNum)).map(function (_, idx) {
return /*#__PURE__*/_jsx("g", {
"data-column": idx,
children: [].concat(Array(7)).map(function (_, cidx) {
var dayProps = _extends({}, rectProps, {
key: cidx,
fill: '#EBEDF0',
width: rectSize,
height: rectSize,
x: idx * (rectSize + space),
y: (rectSize + space) * cidx
});
var currentDate = new Date(initStartDate.getTime() + oneDayTime * (idx * 7 + cidx));
var date = getDateToString(currentDate);
var dataProps = _extends({}, 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') {
var elm = rectRender(_extends({}, dayProps, {
key: cidx
}), dataProps);
if (elm && /*#__PURE__*/React.isValidElement(elm)) {
return elm;
}
}
return /*#__PURE__*/_jsx(Rect, _extends({}, dayProps, {
"data-date": date,
"data-index": dataProps.index,
"data-row": dataProps.row,
"data-column": dataProps.column
}));
})
}, idx);
})
})]
}));
}
//# sourceMappingURL=SVG.js.map