Convert Figma logo to code with AI

kevinsqi logoreact-calendar-heatmap

An svg calendar heatmap inspired by github's contribution graph

1,213
156
1,213
53

Top Related Projects

Cal-Heatmap is a javascript charting library to create a time-series calendar heatmap

:bar_chart: Embed your GitHub calendar everywhere.

Quick Overview

React-calendar-heatmap is a lightweight and customizable calendar heatmap component for React applications. It allows developers to create GitHub-style contribution graphs or any other calendar-based data visualization with ease. The component is highly flexible and can be styled to fit various design requirements.

Pros

  • Easy to integrate into existing React projects
  • Highly customizable with various props for styling and behavior
  • Supports tooltips and click handlers for interactive data exploration
  • Lightweight with minimal dependencies

Cons

  • Limited to calendar year view, no built-in support for custom date ranges
  • Requires additional setup for responsive design
  • Documentation could be more comprehensive, especially for advanced use cases
  • No built-in support for data fetching or state management

Code Examples

  1. Basic usage with default props:
import CalendarHeatmap from 'react-calendar-heatmap';
import 'react-calendar-heatmap/dist/styles.css';

function MyComponent() {
  return (
    <CalendarHeatmap
      startDate={new Date('2023-01-01')}
      endDate={new Date('2023-12-31')}
      values={[
        { date: '2023-01-01', count: 1 },
        { date: '2023-01-22', count: 2 },
        // ... more data
      ]}
    />
  );
}
  1. Customizing colors and tooltips:
<CalendarHeatmap
  startDate={new Date('2023-01-01')}
  endDate={new Date('2023-12-31')}
  values={values}
  classForValue={(value) => {
    if (!value) {
      return 'color-empty';
    }
    return `color-scale-${value.count}`;
  }}
  tooltipDataAttrs={(value) => {
    return {
      'data-tip': `${value.date} has count: ${value.count}`,
    };
  }}
/>
  1. Adding click handlers:
<CalendarHeatmap
  startDate={new Date('2023-01-01')}
  endDate={new Date('2023-12-31')}
  values={values}
  onClick={(value) => alert(`Clicked on ${value.date} with count: ${value.count}`)}
/>

Getting Started

  1. Install the package:

    npm install react-calendar-heatmap
    
  2. Import the component and styles in your React application:

    import CalendarHeatmap from 'react-calendar-heatmap';
    import 'react-calendar-heatmap/dist/styles.css';
    
  3. Use the component in your JSX:

    <CalendarHeatmap
      startDate={new Date('2023-01-01')}
      endDate={new Date('2023-12-31')}
      values={[
        { date: '2023-01-01', count: 1 },
        { date: '2023-01-22', count: 2 },
        // ... more data
      ]}
    />
    
  4. Customize the component using props as needed for your specific use case.

Competitor Comparisons

Cal-Heatmap is a javascript charting library to create a time-series calendar heatmap

Pros of cal-heatmap

  • More customizable with a wide range of options for appearance and behavior
  • Supports various data sources, including CSV, JSON, and API endpoints
  • Offers multiple visualization types beyond just heatmaps (e.g., calendar, timeline)

Cons of cal-heatmap

  • Steeper learning curve due to more complex configuration options
  • Requires additional setup for React integration, as it's not a React-specific library
  • Larger bundle size due to more features and dependencies

Code Comparison

react-calendar-heatmap:

<CalendarHeatmap
  startDate={new Date('2022-01-01')}
  endDate={new Date('2022-12-31')}
  values={[
    { date: '2022-01-01', count: 1 },
    { date: '2022-01-22', count: 2 },
    // ...
  ]}
/>

cal-heatmap:

const cal = new CalHeatmap();
cal.paint({
  data: {
    source: './data.json',
    type: 'json',
  },
  date: { start: new Date('2022-01-01') },
  range: 12,
});

Both libraries offer calendar heatmap visualizations, but they differ in their approach and feature set. react-calendar-heatmap is more focused and easier to integrate into React projects, while cal-heatmap provides more flexibility and customization options at the cost of increased complexity.

:bar_chart: Embed your GitHub calendar everywhere.

Pros of github-calendar

  • Lightweight and simple implementation
  • Supports fetching data directly from GitHub API
  • Can be easily integrated into vanilla JavaScript projects

Cons of github-calendar

  • Less customizable compared to react-calendar-heatmap
  • Limited to GitHub-specific data and styling
  • Lacks React integration, which may be a drawback for React-based projects

Code Comparison

react-calendar-heatmap:

<CalendarHeatmap
  startDate={new Date('2016-01-01')}
  endDate={new Date('2016-04-01')}
  values={[
    { date: '2016-01-01', count: 1 },
    { date: '2016-01-22', count: 2 },
    { date: '2016-01-30', count: 3 },
  ]}
/>

github-calendar:

GitHubCalendar(".calendar", "your-username");
GitHubCalendar(".calendar", "your-username", { responsive: true });

The react-calendar-heatmap offers more flexibility in data structure and customization, while github-calendar provides a simpler setup specifically for GitHub contribution data. react-calendar-heatmap is better suited for React projects and custom data visualization, whereas github-calendar is ideal for quickly displaying GitHub activity in vanilla JavaScript applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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.

NPM DownloadsLast 30 Days