UNPKG

@uiw/react-heat-map

Version:

React component create calendar heatmap to visualize time series data, a la github contribution graph.

411 lines (350 loc) 17.8 kB
<div markdown="1"> <sup>Using <a href="https://wangchujiang.com/#/app" target="_blank">my app</a> is also a way to <a href="https://wangchujiang.com/#/sponsor" target="_blank">support</a> me:</sup> <br> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6766860898" title="Zipora: Zip/RAR/7Z Unarchiver"><img alt="Zipora: Zip/RAR/7Z Unarchiver" height="52" src="https://wangchujiang.com/appicon/zipora.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6758053530" title="Scap: Screenshot & Markup Edit for macOS"><img alt="Scap: Screenshot & Markup Edit" height="52" src="https://wangchujiang.com/appicon/scap.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6757317079" title="Screen Test for macOS"><img alt="Screen Test" height="52" src="https://wangchujiang.com/appicon/screen-test.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6755948110" title="Deskmark for macOS"><img alt="Deskmark" height="52" src="https://wangchujiang.com/appicon/deskmark.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6500434773" title="Keyzer for macOS"><img alt="Keyzer" height="52" src="https://wangchujiang.com/appicon/keyzer.png"></a> <a target="_blank" href="https://github.com/jaywcjlove/vidwall-hub" title="Vidwall Hub for macOS"><img alt="Vidwall Hub" height="52" src="https://wangchujiang.com/appicon/vidwall-hub.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6752624705" title="VidCrop for macOS"><img alt="VidCrop" height="52" src="https://wangchujiang.com/appicon/vidcrop.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6747587746" title="Vidwall for macOS"><img alt="Vidwall" height="52" src="https://wangchujiang.com/appicon/vidwall.png"></a> <a target="_blank" href="https://wangchujiang.com/mousio-hint/" title="Mousio Hint for macOS"><img alt="Mousio Hint" height="52" src="https://wangchujiang.com/appicon/mousio-hint.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6746747327" title="Mousio for macOS"><img alt="Mousio" height="52" src="https://wangchujiang.com/appicon/mousio.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6745227444" title="Musicer for macOS"><img alt="Musicer" height="52" src="https://wangchujiang.com/appicon/musicer.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6743841447" title="Audioer for macOS"><img alt="Audioer" height="52" src="https://wangchujiang.com/appicon/audioer.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6744690194" title="FileSentinel for macOS"><img alt="FileSentinel" height="52" src="https://wangchujiang.com/appicon/file-sentinel.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6743495172" title="FocusCursor for macOS"><img alt="FocusCursor" height="52" src="https://wangchujiang.com/appicon/focus-cursor.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6742680573" title="Videoer for macOS"><img alt="Videoer" height="52" src="https://wangchujiang.com/appicon/videoer.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6740425504" title="KeyClicker for macOS"><img alt="KeyClicker" height="52" src="https://wangchujiang.com/appicon/key-clicker.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6739052447" title="DayBar for macOS"><img alt="DayBar" height="52" src="https://wangchujiang.com/appicon/daybar.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6739444407" title="Iconed for macOS"><img alt="Iconed" height="52" src="https://wangchujiang.com/appicon/iconed.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6737160756" title="Menuist for macOS"><img alt="Menuist" height="52" src="https://wangchujiang.com/appicon/rightmenu-master.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6723903021" title="Paste Quick for macOS"><img alt="Quick RSS" height="52" src="https://wangchujiang.com/appicon/paste-quick.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6670696072&platform=mac" title="Quick RSS for macOS/iOS"><img alt="Quick RSS" height="52" src="https://wangchujiang.com/appicon/quick-rss.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6670167443" title="Web Serve for macOS"><img alt="Web Serve" height="52" src="https://wangchujiang.com/appicon/web-serve.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6503953628&platform=mac" title="Copybook Generator for macOS/iOS"><img alt="Copybook Generator" height="52" src="https://wangchujiang.com/appicon/copybook-generator.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6471227008&platform=mac" title="DevTutor for macOS/iOS"><img alt="DevTutor for SwiftUI" height="52" src="https://wangchujiang.com/appicon/devtutor.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6479819388&platform=mac" title="RegexMate for macOS/iOS"><img alt="RegexMate" height="52" src="https://wangchujiang.com/appicon/regex-mate.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6479194014&platform=mac" title="Time Passage for macOS/iOS"><img alt="Time Passage" height="52" src="https://wangchujiang.com/appicon/time-passage.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6478772538" title="IconizeFolder for macOS"><img alt="Iconize Folder" height="52" src="https://wangchujiang.com/appicon/iconize-folder.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6478511402&platform=mac" title="Textsound Saver for macOS/iOS"><img alt="Textsound Saver" height="52" src="https://wangchujiang.com/appicon/textsound-saver.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6476924627" title="Create Custom Symbols for macOS"><img alt="Create Custom Symbols" height="52" src="https://wangchujiang.com/appicon/create-custom-symbols.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6476452351" title="DevHub for macOS"><img alt="DevHub" height="52" src="https://wangchujiang.com/appicon/devhub.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6476400184" title="Resume Revise for macOS"><img alt="Resume Revise" height="52" src="https://wangchujiang.com/appicon/resume-revise.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6472593276" title="Palette Genius for macOS"><img alt="Palette Genius" height="52" src="https://wangchujiang.com/appicon/palette-genius.png"></a> <a target="_blank" href="https://jaywcjlove.github.io/maslink/?id=6470879005" title="Symbol Scribe for macOS"><img alt="Symbol Scribe" height="52" src="https://wangchujiang.com/appicon/symbol-scribe.png"></a> </div> <hr> HeatMap 日历热图 === <!--rehype:style=text-align: center;--> [![Buy me a coffee](https://img.shields.io/badge/Buy_Me_a_Coffee-ffdd00?logo=buy-me-a-coffee&logoColor=black)](https://jaywcjlove.github.io/#/sponsor) [![Follow On X](https://img.shields.io/badge/Follow%20on%20X-333333?logo=x&logoColor=white)](https://x.com/jaywcjlove) [![Build & Deploy](https://github.com/uiwjs/react-heat-map/actions/workflows/ci.yml/badge.svg)](https://github.com/uiwjs/react-heat-map/actions/workflows/ci.yml) [![Coverage Status](https://img.shields.io/npm/dm/@uiw/react-heat-map.svg?style=flat)](https://www.npmjs.com/package/@uiw/react-heat-map) [![npm version](https://img.shields.io/npm/v/@uiw/react-heat-map.svg)](https://www.npmjs.com/package/@uiw/react-heat-map) [![Open in Gitpod](https://shields.io/badge/Open%20in-Gitpod-green?logo=Gitpod)](https://gitpod.io/#https://github.com/uiwjs/react-codemirror) <!--rehype:style=text-align: center;--> A lightweight calendar heatmap react component built on SVG, customizable version of GitHub's contribution graph. Try it out on [website example](https://uiwjs.github.io/react-heat-map/). <!--rehype:ignore:start--> [![](https://user-images.githubusercontent.com/1680273/186116433-d58c2b6d-8468-4322-943c-9b63c2e447e4.png)](https://uiwjs.github.io/react-heat-map) <!--rehype:ignore:end--> ## Install ```bash # Not dependent on uiw. npm install @uiw/react-heat-map --save ``` If using Next.js, you will need to use the [`next-remove-imports`](https://www.npmjs.com/package/next-remove-imports) package to avoid errors, see [issue #69](https://github.com/uiwjs/react-heat-map/issues/69). ## Basic Usage Basic usage example, Please pay warning to the time setting. ⚠️ Example: ~~`2016-01-11`<!--rehype:style=color: red;background-color: #ffd8d8;-->~~ -> `2016/01/11`<!--rehype:style=color: green;background: #a7e4b5;-->, Support `Safari`<!--rehype:style=background-color: #87d499;--> ```jsx mdx:preview import React from 'react'; import HeatMap from '@uiw/react-heat-map'; const value = [ { date: '2016/01/11', count: 2 }, { date: '2016/01/12', count: 20 }, { date: '2016/01/13', count: 10 }, ...[...Array(17)].map((_, idx) => ({ date: `2016/02/${idx + 10}`, count: idx, content: '' })), { date: '2016/04/11', count: 2 }, { date: '2016/05/01', count: 5 }, { date: '2016/05/02', count: 5 }, { date: '2016/05/04', count: 11 }, ]; const Demo = () => { return ( <div> <HeatMap value={value} weekLabels={['', 'Mon', '', 'Wed', '', 'Fri', '']} startDate={new Date('2016/01/01')} /> </div> ) }; export default Demo ``` ## Set Color Set the theme color style. ```jsx mdx:preview import React from 'react'; import HeatMap from '@uiw/react-heat-map'; const value = [ { date: '2016/01/11', count:2 }, { date: '2016/04/12', count:2 }, { date: '2016/05/01', count:17 }, { date: '2016/05/02', count:5 }, { date: '2016/05/03', count:27 }, { date: '2016/05/04', count:11 }, { date: '2016/05/08', count:32 }, ]; const Demo = () => { return ( <HeatMap value={value} width={600} style={{ color: '#ad001d', '--rhm-rect-active': 'red' }} startDate={new Date('2016/01/01')} panelColors={{ 0: '#f4decd', 7: '#e4b293', 14: '#d48462', 21: '#c2533a', 28: '#ad001d', 35: '#6c0012' }} /> ) }; export default Demo ``` Dynamic color based on maximum value ```jsx mdx:preview import React from 'react'; import HeatMap from '@uiw/react-heat-map'; const value = [ { date: '2016/01/11', count:2 }, { date: '2016/04/12', count:2 }, { date: '2016/05/01', count:17 }, { date: '2016/05/02', count:5 }, { date: '2016/05/03', count:27 }, { date: '2016/05/04', count:11 }, { date: '2016/05/08', count:32 }, ]; const Demo = () => { return ( <HeatMap value={value} width={600} style={{ color: '#ad001d', '--rhm-rect-active': 'red' }} startDate={new Date('2016/01/01')} panelColors={['#f4decd', '#e4b293', '#d48462', '#c2533a', '#ad001d', '#6c0012']} /> ) }; export default Demo ``` ## Set Rect Style Set the radius of the rect. ```jsx mdx:preview import React, { useState } from 'react'; import HeatMap from '@uiw/react-heat-map'; const value = [ { date: '2016/01/11', count:2 }, ...[...Array(17)].map((_, idx) => ({ date: `2016/01/${idx + 10}`, count: idx })), ...[...Array(17)].map((_, idx) => ({ date: `2016/02/${idx + 10}`, count: idx })), { date: '2016/04/12', count:2 }, { date: '2016/05/01', count:5 }, { date: '2016/05/02', count:5 }, { date: '2016/05/03', count:1 }, { date: '2016/05/04', count:11 }, { date: '2016/05/08', count:32 }, ]; const Demo = () => { const [range, setRange] = useState(5) return ( <div> <input type="range" min="0" max="5" step="0.1" value={range} onChange={(e) => setRange(e.target.value)} /> {range} <HeatMap value={value} width={600} style={{ '--rhm-rect': '#b9b9b9' }} startDate={new Date('2016/01/01')} legendRender={(props) => <rect {...props} y={props.y + 10} rx={range} />} rectProps={{ rx: range }} /> </div> ) }; export default Demo ``` ## Tooltip A simple text popup tip. ```jsx mdx:preview import React from 'react'; import Tooltip from '@uiw/react-tooltip'; import HeatMap from '@uiw/react-heat-map'; const value = [ { date: '2016/01/11', count:2 }, ...[...Array(17)].map((_, idx) => ({ date: `2016/01/${idx + 10}`, count: idx, })), ...[...Array(17)].map((_, idx) => ({ date: `2016/02/${idx + 10}`, count: idx, })), { date: '2016/04/12', count:2 }, { date: '2016/05/01', count:5 }, { date: '2016/05/02', count:5 }, { date: '2016/05/03', count:1 }, { date: '2016/05/04', count:11 }, { date: '2016/05/08', count:32 }, ]; const Demo = () => { return ( <HeatMap value={value} width={600} startDate={new Date('2016/01/01')} rectRender={(props, data) => { // if (!data.count) return <rect {...props} />; return ( <Tooltip placement="top" content={`count: ${data.count || 0}`}> <rect {...props} /> </Tooltip> ); }} /> ) }; export default Demo ``` ## Show/Hide Legend ```jsx mdx:preview import React, { useState } from 'react'; import HeatMap from '@uiw/react-heat-map'; const value = [ { date: '2016/01/11', count:2 }, ...[...Array(17)].map((_, idx) => ({ date: `2016/01/${idx + 10}`, count: idx })), ...[...Array(17)].map((_, idx) => ({ date: `2016/02/${idx + 10}`, count: idx })), { date: '2016/04/12', count:2 }, { date: '2016/05/01', count:5 }, { date: '2016/05/02', count:5 }, { date: '2016/05/03', count:1 }, { date: '2016/05/04', count:11 }, { date: '2016/05/08', count:32 }, ]; const Demo = () => { const [size, setSize] = useState(0) return ( <div> <label style={{ userSelect: 'none' }}> <input type="checkbox" checked={size === 0} onChange={(e) => setSize(e.target.checked ? 0 : 12)} /> {size === 0 ? ' Hide' : ' Show'} Legend </label> <HeatMap width={600} value={value} legendCellSize={size} startDate={new Date('2016/01/01')} /> </div> ) }; export default Demo ``` ## Selected Rect ```jsx mdx:preview import React, { useState } from 'react'; import HeatMap from '@uiw/react-heat-map'; const value = [ { date: '2016/01/11', count:2 }, ...[...Array(17)].map((_, idx) => ({ date: `2016/01/${idx + 10}`, count: idx })), ...[...Array(17)].map((_, idx) => ({ date: `2016/02/${idx + 10}`, count: idx })), { date: '2016/04/12', count:2 }, { date: '2016/05/01', count:5 }, { date: '2016/05/02', count:5 }, { date: '2016/05/03', count:1 }, { date: '2016/05/04', count:11 }, { date: '2016/05/08', count:32 }, ]; const Demo = () => { const [selected, setSelected] = useState('') return ( <div> <HeatMap width={600} value={value} startDate={new Date('2016/01/01')} rectRender={(props, data) => { if (selected !== '') { props.opacity = data.date === selected ? 1 : 0.45 } return ( <rect {...props} onClick={() => { setSelected(data.date === selected ? '' : data.date); }} /> ); }} /> </div> ) }; export default Demo ``` ## Props | Property | Description | Type | Default | | ---- | ---- | ---- | ---- | | `value` | Data to be displayed, **required** | Array | `[]` | | `rectSize` | Grid size | number | `11` | | `legendCellSize` | Size of the legend cells, in pixel. Value equal to `0` hide legend. | number | `11` | | `startDate` | Start date | Date | `new Date()` | | `endDate` | End date | Date | - | | `space` | Interval between grid sizes | number | `2` | | `monthPlacement` | position of month labels | `'top' | 'bottom'` | `top` | | `rectProps` | Grid node attribute settings | `React.SVGProps<SVGRectElement>` | `2` | | `weekLabels` | Week display | string[] | `['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']` | | `monthLabels` | Month display | string[] | `['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']` | | `panelColors` | Backgroud color of active colors | `Record<number, string>` \| `string[]` | `['var(--rhm-rect, #EBEDF0)','#C6E48B','#7BC96F', '#239A3B', '#196127']` | | `rectRender` | Single `day` block re-render | `<E = SVGRectElement>(data: E & { key: number }, valueItem: HeatMapValue & { date: string, column: number, row: number, index: number }) => React.ReactElement` | - | | `legendRender` | Single `legend` block re-render | `(props: React.SVGProps<SVGRectElement>) => React.ReactNode` | - | ## Development **`development`** Runs the project in development mode. ```bash npm install ``` ```bash # Step 1, run first, listen to the component compile and output the .js file # listen for compilation output type .d.ts file npm run watch # Step 2, development mode, listen to compile preview website instance npm run start ``` **`production`** Builds the app for production to the build folder. ```bash npm run build npm run doc ``` The build is minified and the filenames include the hashes. Your app is ready to be deployed! ## Contributors As always, thanks to our amazing contributors! <a href="https://github.com/uiwjs/react-heat-map/graphs/contributors"> <img src="https://uiwjs.github.io/react-heat-map/CONTRIBUTORS.svg" /> </a> Made with [github-action-contributors](https://github.com/jaywcjlove/github-action-contributors). ## License Licensed under the MIT License.