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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | 1x 1x 1x 17x 17x 17x 1x 1x 10235x 10235x 10235x 10235x 3868x 381x 6x 375x 373x 2x 76x 68x 8x 4x 4x 7090x 17384x 401x 6689x 55x 55x 55x 21x 19x 19x 18x 29x 29x 29x 29x 3007x 19x 2988x 3007x 19x 2988x 3007x 19x 2988x 3007x 19x 2988x 3017x 3017x 10x 3007x 449x 403x 46x 19x 17x 2x 19x 17x 2x 19x 17x 2x 19x 17x 2x 3007x 2705x 302x 14x 7x 7x 372x 328x 44x 44x 3143x 3143x 136x 3007x 3007x 3007x 3007x 3007x 449x 3143x 449x 19x 2x 17x 17x 372x 372x 372x 19x 17x 2x 14x 14x 14x 19x 1x 1x 3007x | import React from 'react'; import PropTypes from 'prop-types'; import { DAYS_IN_WEEK, MILLISECONDS_IN_ONE_DAY, DAY_LABELS, MONTH_LABELS } from './constants'; import { dateNDaysAgo, shiftDate, getBeginningTimeForDate, convertToDate, getRange } from './helpers'; const SQUARE_SIZE = 10; const MONTH_LABEL_GUTTER_SIZE = 4; const CSS_PSEDUO_NAMESPACE = 'react-calendar-heatmap-'; class CalendarHeatmap extends React.Component { constructor(props) { super(props); this.latestProps = props; this.state = { valueCache: this.getValueCache(this.latestProps.values), }; } componentWillReceiveProps(nextProps) { this.latestProps = nextProps; this.setState({ valueCache: this.getValueCache(this.latestProps.values), }); } getDateDifferenceInDays() { const { startDate, endDate, numDays } = this.latestProps; Iif (numDays) { // eslint-disable-next-line no-console console.warn('numDays is a deprecated prop. It will be removed in the next release. Consider using the startDate prop instead.'); return numDays; } const timeDiff = this.getEndDate() - convertToDate(startDate); return Math.ceil(timeDiff / MILLISECONDS_IN_ONE_DAY); } getSquareSizeWithGutter() { return SQUARE_SIZE + this.latestProps.gutterSize; } getMonthLabelSize() { if (!this.latestProps.showMonthLabels) { return 0; } else if (this.latestProps.horizontal) { return SQUARE_SIZE + MONTH_LABEL_GUTTER_SIZE; } return 2 * (SQUARE_SIZE + MONTH_LABEL_GUTTER_SIZE); } getWeekdayLabelSize() { if (!this.latestProps.showWeekdayLabels) { return 0; } else if (this.latestProps.horizontal) { return 30; } return SQUARE_SIZE * 1.5; } getStartDate() { return shiftDate(this.getEndDate(), -this.getDateDifferenceInDays() + 1); // +1 because endDate is inclusive } getEndDate() { return getBeginningTimeForDate(convertToDate(this.latestProps.endDate)); } getStartDateWithEmptyDays() { return shiftDate(this.getStartDate(), -this.getNumEmptyDaysAtStart()); } getNumEmptyDaysAtStart() { return this.getStartDate().getDay(); } getNumEmptyDaysAtEnd() { return (DAYS_IN_WEEK - 1) - this.getEndDate().getDay(); } getWeekCount() { const numDaysRoundedToWeek = this.getDateDifferenceInDays() + this.getNumEmptyDaysAtStart() + this.getNumEmptyDaysAtEnd(); return Math.ceil(numDaysRoundedToWeek / DAYS_IN_WEEK); } getWeekWidth() { return DAYS_IN_WEEK * this.getSquareSizeWithGutter(); } getWidth() { return (this.getWeekCount() * this.getSquareSizeWithGutter()) - (this.latestProps.gutterSize - this.getWeekdayLabelSize()); } getHeight() { return this.getWeekWidth() + (this.getMonthLabelSize() - this.latestProps.gutterSize) + this.getWeekdayLabelSize(); } getValueCache(values) { return values.reduce((memo, value) => { const date = convertToDate(value.date); const index = Math.floor((date - this.getStartDateWithEmptyDays()) / MILLISECONDS_IN_ONE_DAY); memo[index] = { value, className: this.latestProps.classForValue(value), title: this.latestProps.titleForValue ? this.latestProps.titleForValue(value) : null, tooltipDataAttrs: this.getTooltipDataAttrsForValue(value), }; return memo; }, {}); } getValueForIndex(index) { if (this.state.valueCache[index]) { return this.state.valueCache[index].value; } return null; } getClassNameForIndex(index) { if (this.state.valueCache[index]) { return this.state.valueCache[index].className; } return this.latestProps.classForValue(null); } getTitleForIndex(index) { if (this.state.valueCache[index]) { return this.state.valueCache[index].title; } return this.latestProps.titleForValue ? this.latestProps.titleForValue(null) : null; } getTooltipDataAttrsForIndex(index) { if (this.state.valueCache[index]) { return this.state.valueCache[index].tooltipDataAttrs; } return this.getTooltipDataAttrsForValue({ date: null, count: null }); } getTooltipDataAttrsForValue(value) { const { tooltipDataAttrs } = this.latestProps; if (typeof tooltipDataAttrs === 'function') { return tooltipDataAttrs(value); } return tooltipDataAttrs; } getTransformForWeek(weekIndex) { if (this.latestProps.horizontal) { return `translate(${weekIndex * this.getSquareSizeWithGutter()}, 0)`; } return `translate(0, ${weekIndex * this.getSquareSizeWithGutter()})`; } getTransformForWeekdayLabels() { if (this.latestProps.horizontal) { return `translate(${SQUARE_SIZE}, ${this.getMonthLabelSize()})`; } return null; } getTransformForMonthLabels() { if (this.latestProps.horizontal) { return `translate(${this.getWeekdayLabelSize()}, 0)`; } return `translate(${this.getWeekWidth() + MONTH_LABEL_GUTTER_SIZE}, ${this.getWeekdayLabelSize()})`; } getTransformForAllWeeks() { if (this.latestProps.horizontal) { return `translate(${this.getWeekdayLabelSize()}, ${this.getMonthLabelSize()})`; } return `translate(0, ${this.getWeekdayLabelSize()})`; } getViewBox() { if (this.latestProps.horizontal) { return `0 0 ${this.getWidth()} ${this.getHeight()}`; } return `0 0 ${this.getHeight()} ${this.getWidth()}`; } getSquareCoordinates(dayIndex) { if (this.latestProps.horizontal) { return [0, dayIndex * this.getSquareSizeWithGutter()]; } return [dayIndex * this.getSquareSizeWithGutter(), 0]; } getWeekdayLabelCoordinates(dayIndex) { if (this.latestProps.horizontal) { return [ 0, ((dayIndex + 1) * SQUARE_SIZE) + (dayIndex * this.latestProps.gutterSize), ]; } return [ (dayIndex * SQUARE_SIZE) + (dayIndex * this.latestProps.gutterSize), SQUARE_SIZE, ]; } getMonthLabelCoordinates(weekIndex) { if (this.latestProps.horizontal) { return [ weekIndex * this.getSquareSizeWithGutter(), this.getMonthLabelSize() - MONTH_LABEL_GUTTER_SIZE, ]; } const verticalOffset = -2; return [ 0, ((weekIndex + 1) * this.getSquareSizeWithGutter()) + verticalOffset, ]; } handleClick(value) { if (this.latestProps.onClick) { this.latestProps.onClick(value); } } handleMouseOver(e, value) { if (this.latestProps.onMouseOver) { this.latestProps.onMouseOver(e, value); } } handleMouseLeave(e, value) { if (this.latestProps.onMouseLeave) { this.latestProps.onMouseLeave(e, value); } } renderSquare(dayIndex, index) { const indexOutOfRange = index < this.getNumEmptyDaysAtStart() || index >= this.getNumEmptyDaysAtStart() + this.getDateDifferenceInDays(); if (indexOutOfRange && !this.latestProps.showOutOfRangeDays) { return null; } const [x, y] = this.getSquareCoordinates(dayIndex); const value = this.getValueForIndex(index); const rect = ( <rect key={index} width={SQUARE_SIZE} height={SQUARE_SIZE} x={x} y={y} className={this.getClassNameForIndex(index)} onClick={this.handleClick.bind(this, value)} onMouseOver={e => this.handleMouseOver(e, value)} onMouseLeave={e => this.handleMouseLeave(e, value)} {...this.getTooltipDataAttrsForIndex(index)} > <title>{this.getTitleForIndex(index)}</title> </rect> ); const { transformDayElement } = this.latestProps; return transformDayElement ? transformDayElement(rect, value, index) : rect; } renderWeek(weekIndex) { return ( <g key={weekIndex} transform={this.getTransformForWeek(weekIndex)} className={`${CSS_PSEDUO_NAMESPACE}week`}> {getRange(DAYS_IN_WEEK).map(dayIndex => this.renderSquare(dayIndex, (weekIndex * DAYS_IN_WEEK) + dayIndex))} </g> ); } renderAllWeeks() { return getRange(this.getWeekCount()).map(weekIndex => this.renderWeek(weekIndex)); } renderMonthLabels() { if (!this.latestProps.showMonthLabels) { return null; } const weekRange = getRange(this.getWeekCount() - 1); // don't render for last week, because label will be cut off return weekRange.map((weekIndex) => { const endOfWeek = shiftDate(this.getStartDateWithEmptyDays(), (weekIndex + 1) * DAYS_IN_WEEK); const [x, y] = this.getMonthLabelCoordinates(weekIndex); return (endOfWeek.getDate() >= 1 && endOfWeek.getDate() <= DAYS_IN_WEEK) ? ( <text key={weekIndex} x={x} y={y} className={`${CSS_PSEDUO_NAMESPACE}month-label`}> {this.latestProps.monthLabels[endOfWeek.getMonth()]} </text> ) : null; }); } renderWeekdayLabels() { if (!this.latestProps.showWeekdayLabels) { return null; } return this.latestProps.weekdayLabels.map((weekdayLabel, dayIndex) => { const [x, y] = this.getWeekdayLabelCoordinates(dayIndex); const cssClasses = `${this.latestProps.horizontal ? '' : `${CSS_PSEDUO_NAMESPACE}small-text`} ${CSS_PSEDUO_NAMESPACE}weekday-label`; // eslint-disable-next-line no-bitwise return dayIndex & 1 ? ( <text key={`${x}${y}`} x={x} y={y} className={cssClasses}> {weekdayLabel} </text> ) : null; }); } render() { return ( <svg className="react-calendar-heatmap" viewBox={this.getViewBox()}> <g transform={this.getTransformForMonthLabels()} className={`${CSS_PSEDUO_NAMESPACE}month-labels`}> {this.renderMonthLabels()} </g> <g transform={this.getTransformForAllWeeks()} className={`${CSS_PSEDUO_NAMESPACE}all-weeks`}> {this.renderAllWeeks()} </g> <g transform={this.getTransformForWeekdayLabels()} className={`${CSS_PSEDUO_NAMESPACE}weekday-labels`}> {this.renderWeekdayLabels()} </g> </svg> ); } } CalendarHeatmap.propTypes = { values: PropTypes.arrayOf(PropTypes.shape({ date: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.instanceOf(Date)]).isRequired, }).isRequired).isRequired, // array of objects with date and arbitrary metadata numDays: PropTypes.number, // number of days back from endDate to show startDate: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.instanceOf(Date)]), // start of date range endDate: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.instanceOf(Date)]), // end of date range gutterSize: PropTypes.number, // size of space between squares horizontal: PropTypes.bool, // whether to orient horizontally or vertically showMonthLabels: PropTypes.bool, // whether to show month labels showWeekdayLabels: PropTypes.bool, // whether to show weekday labels showOutOfRangeDays: PropTypes.bool, // whether to render squares for extra days in week after endDate, and before start date tooltipDataAttrs: PropTypes.oneOfType([PropTypes.object, PropTypes.func]), // data attributes to add to square for setting 3rd party tooltips, e.g. { 'data-toggle': 'tooltip' } for bootstrap tooltips titleForValue: PropTypes.func, // function which returns title text for value classForValue: PropTypes.func, // function which returns html class for value monthLabels: PropTypes.arrayOf(PropTypes.string), // An array with 12 strings representing the text from janurary to december weekdayLabels: PropTypes.arrayOf(PropTypes.string), // An array with 7 strings representing the text from Sun to Sat onClick: PropTypes.func, // callback function when a square is clicked onMouseOver: PropTypes.func, // callback function when mouse pointer is over a square onMouseLeave: PropTypes.func, // callback function when mouse pointer is left a square transformDayElement: PropTypes.func, // function to further transform the svg element for a single day }; CalendarHeatmap.defaultProps = { startDate: dateNDaysAgo(200), endDate: new Date(), gutterSize: 1, horizontal: true, showMonthLabels: true, showWeekdayLabels: false, showOutOfRangeDays: false, monthLabels: MONTH_LABELS, weekdayLabels: DAY_LABELS, classForValue: value => (value ? 'color-filled' : 'color-empty'), }; export default CalendarHeatmap; |