@freecodecamp/react-calendar-heatmap

中文文档
1.1.0发布于 2023/02/08MITESM + CJS近 30 天下载 1970

A calendar heatmap component built on SVG, inspired by Github's commit calendar graph.

原始使用文档
文档目录6 节
  1. Setup
  2. Usage
  3. Props
  4. Configuring colors
  5. Contributing
  6. License

React Calendar Heatmap

A calendar heatmap component built on SVG, inspired by github's commit calendar graph. The component expands to size of container and is super configurable. Try it out on CodeSandbox.

npm version Build Status bundle size

react-calendar-heatmap screenshot

Setup

Install the npm module with yarn or npm:

yarn add react-calendar-heatmap

Usage

Import the component:

import CalendarHeatmap from 'react-calendar-heatmap';

Import styles. You can directly import from the module, which requires a CSS loader:

import 'react-calendar-heatmap/dist/styles.css';

A CSS loader is included by default in create-react-app. If you don't have a CSS loader, you can simply copy the stylesheet into a file in your project and import it instead.

To show a basic heatmap from January 1st to April 1st:

<CalendarHeatmap
  startDate={new Date('2016-01-01')}
  endDate={new Date('2016-04-01')}
  values={[
    { date: '2016-01-01', count: 12 },
    { date: '2016-01-22', count: 122 },
    { date: '2016-01-30', count: 38 },
    // ...and so on
  ]}
/>

Props

NameTypeDescription
valuesRequired, Array of ObjectRequired array of objects which each have a date property, which can be a Date object, parseable string, or millisecond timestamp. Example: [{ date: '2016-01-01', count: 6 }]
startDateString, Number, or DateStart of date range.
endDateString, Number, or DateEnd of date range - a Date object, parseable string, or millisecond timestamp.
showMonthLabelsBooleanToggle for removing month labels.
showWeekdayLabelsBooleanToggle for removing weekday labels.
showOutOfRangeDaysBooleanToggle display of extra days in week that are past endDate and before beginning of range.
horizontalBooleanWhether to orient horizontally or vertically. Can be used in combination with numDays/endDate to show just the current month.
gutterSizeNumberSize of gutters relative to squares.
onClickFunctionCallback to invoke when a square is clicked, e.g. (value) => alert(value)
onMouseOverFunctionCallback to invoke when mouse pointer is over a square, e.g. (event, value) => console.log(event, value)
onMouseLeaveFunctionCallback to invoke when mouse pointer leaves a square, e.g. (event, value) => console.log(event, value)
titleForValueFunctionFunction to determine each square's title attribute, for generating 3rd party hover tooltips (may also need to configure tooltipDataAttrs). Example: (value) => Date is ${value.date}`
tooltipDataAttrsObject or FunctionSet data attributes for all squares, for generating 3rd party hover tooltips. Either an object like { 'data-tooltip': 'tooltip' } or a function like (value) => { return { 'data-tooltip': 'Tooltip: ' + value } }
classForValueFunctionCallback for determining CSS class to apply to each value, e.g. (value) => (value.count > 0 ? 'blue' : 'white').
monthLabelsArray of StringAn array with 12 strings representing the text from January to December, e.g. ['01', '02', ..., '12']
weekdayLabelsArray of StringAn array with 7 strings representing the text from Sunday to Saturday
transformDayElementFunctionA function to further transform generated svg element for a single day. Can be used to attach event handlers, add tooltips and more. Example: (element, value, index) => React.cloneElement(element, { title: value.date }).

Configuring colors

To use the color scale shown in the live demo based on the github contribution graph, you can set the classForValue prop, a function that determines which CSS class to apply to each value:

<CalendarHeatmap
  values={[
    { date: '2016-01-01', count: 1 },
    { date: '2016-01-03', count: 4 },
    { date: '2016-01-06', count: 2 },
    // ...and so on
  ]}
  classForValue={(value) => {
    if (!value) {
      return 'color-empty';
    }
    return `color-scale-${value.count}`;
  }}
/>

Then you use CSS to set colors for each class:

.react-calendar-heatmap .color-scale-1 { fill: #d6e685; }
.react-calendar-heatmap .color-scale-2 { fill: #8cc665; }
.react-calendar-heatmap .color-scale-3 { fill: #44a340; }
.react-calendar-heatmap .color-scale-4 { fill: #1e6823; }

Contributing

Take a look at CONTRIBUTING.md to see how to develop on react-calendar-heatmap.

License

react-calendar-heatmap is Copyright © 2016 PatientsLikeMe, Inc. and is released under an MIT License. See COPYING for details.

版本与发布记录

@freecodecamp/react-calendar-heatmap 共发布 2 个版本,首次发布于 2020/03/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2023/02/083 年前
1.0.02020/03/106 年前

关于 @freecodecamp/react-calendar-heatmap 的常见问题

@freecodecamp/react-calendar-heatmap 是什么?

A calendar heatmap component built on SVG, inspired by Github's commit calendar graph.

如何安装 @freecodecamp/react-calendar-heatmap?

在项目目录运行 npm install @freecodecamp/react-calendar-heatmap,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^0.14.0 || ^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@freecodecamp/react-calendar-heatmap 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/freecodecamp__react-calendar-heatmap 是否存在。

@freecodecamp/react-calendar-heatmap 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@freecodecamp/react-calendar-heatmap 可以商用吗?

@freecodecamp/react-calendar-heatmap 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@freecodecamp/react-calendar-heatmap 的下载量有多少?

根据 npm 官方下载统计,@freecodecamp/react-calendar-heatmap 在 2026/10/16 – 2026/11/14 这 30 天内被下载 1,970 次,日均约 66 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@freecodecamp/react-calendar-heatmap 还在维护吗?

最新版本 1.1.0 发布于 2023/02/08(3 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。