0.1.0 • Published 6 years ago

react-light-heatmap v0.1.0

Weekly downloads
42
License
MIT
Repository
github
Last release
6 years ago

react-light-heatmap

NPM JavaScript Style Guide

A React component for heatmap in grid layout using div.

Demo

alt tag

Installation

yarn add react-light-heatmap  

or

npm install react-light-heatmap --save  

Usage

Mandatory fields

NameTypeSample
xLabelsArray of string['1am', '2am', '3am']
yLabelsArray of string['Sun', 'Mon']
data2D Array of numbers having yLabels.length rows and xLabels.length rows[[2,3,5][5,6,9]]
const xLabels = new Array(24).fill(0).map((_, i) => `${i}`);  
const yLabels = ['Sun', 'Mon', 'Tue'];  
const data = new Array(yLabels.length)  
  .fill(0)  
  .map(() => new Array(xLabels.length).fill(0).map(() => Math.floor(Math.random() * 100)));  
  
ReactDOM.render(  
  <HeatMap  
    xLabels={xLabels}  
    yLabels={yLabels}  
    data={data}  
  />,  
  document.getElementById('app')  
);  

Configuration

NameTypeDescriptionDefault Value
componentsobjectReplace default componentdefault components
backgroundstringThe base color for the heatmap"#239a3b"
heightnumberHeight of each cell of the heatmap in px30
onClickfunctionAdds an handler to cell clickundefined
squaresbooleanIf set to true will render cells as squarefalse
xLabelWidthnumberWidth of the x label area in pixel60
xLabelsLocationstringLocation of y labels. It can be top or bottom"top"
unitstringUnit to display next to the value on hover
cellStylefunctionTo set custom cell style. It is useful for using own colour scheme
getValuefunctionTo get value from provided datavalue => value

Example

  
const xLabels = new Array(24).fill(0).map((_, i) => `${i}`);  
  
const yLabels = ["Sun", "Mon", "Tue"];  
const data = new Array(yLabels.length)  
  .fill(0)  
  .map(() =>  
    new Array(xLabels.length).fill(0).map(() => Math.floor(Math.random() * 100))  
  );  
  
ReactDOM.render(  
  <HeatMap  
    xLabels={xLabels}  
    yLabels={yLabels}  
    xLabelWidth={50}  
    data={data}  
    squares  
    onClick={(x, y) => alert(`Clicked ${x}, ${y}`)}  
    cellStyle={(background, value, min, max, data, x, y) => ({  
      background: `rgb(66, 86, 244, ${1 - (max - value) / (max - min)})`,  
      fontSize: "11px",  
    })}  
    cellRender={value => value && `${value}%`}  
  />,  
  document.getElementById("app")  
);  

Replacing components

Cell Component responsible for displaing a cell in grid. Props:

type = {
	 value: number,
	 x: number,
	 y: number,
	 height: number
}

All other props will be passed to the div component

CellContent Component responsible for displaing a content inside cell component. Props:

type = {
	 value: number
}

By default display nothing

XLabel Component responsible for displaing X label Props:

type = {
	 value: string|number,
	 squares: boolean,
	 index: number,
	 height: number
}

Default render

<div  
  style={{  
	  flex: squares ? 'none' : 1,  
	  textAlign: 'center',  
	  width: squares ? `${height + 1}px` : 'undefined',  
	  ...style  
  }}  
>  
 {value}  
</div>

YLabel Component responsible for displaing Y label Props:

type = {
	 value: string|number,
	 squares: boolean,
	 index: number,
	 height: number
}

Default render

<div  
  style={{  
	  textAlign: 'center',  
	  paddingRight: '5px',  
	  paddingTop: `${height / 3.7}px`,  
	  ...style  
  }} 
>  
 {value}  
</div>

For developers

New build

yarn build  

Run dev server

yarn start

Run test

npm run test  
0.1.0

6 years ago

0.0.5-1

6 years ago

0.0.5-0

6 years ago

0.0.4

6 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago