React components for Chart.js
npm install test-react-chartjs-2



React wrapper for Chart.js
``bash
npm install --save react-chartjs-2 chart.js
yarn add react-chartjs-2 chart.js
`
###### We recommend using chart.js ^3.0.0
`jsx
import { Doughnut } from 'react-chartjs-2';
`
Live: reactchartjs.github.io/react-chartjs-2
See these examples for more information
`js`
id?: string;
className?: string;
height?: number;
width?: number;
redraw?: boolean;
type: Chart.ChartType
data: Chart.ChartData | (canvas: HTMLCanvasElement | null) => Chart.ChartData;
options?: Chart.ChartOptions;
plugins?: Chart.PluginServiceRegistrationOptions[];
getDatasetAtEvent?: (dataset: Array<{}>, event: React.MouseEvent
getElementAtEvent?: (element: [{}], event: React.MouseEvent
getElementsAtEvent?: (elements: Array<{}>, event: React.MouseEvent
#### id
Type stringundefined
Default:
ID attribute applied to the rendered canvas
#### className
Type stringundefined
Default:
class attribute applied to the rendered canvas
#### height
Type: number150
Default:
Height attribute applied to the rendered canvas
#### width
Type: number300
Default:
Width attribute applied to the rendered canvas
#### redraw
Type: booleanfalse
Default:
If true, will tear down and redraw chart on all updates
#### type
Type: 'line' | 'bar' | 'horizontalBar' | 'radar' | 'doughnut' | 'polarArea' | 'bubble' | 'pie' | 'scatter'
Chart.js chart type (required only on ChartComponent)
#### data (required)
Type: Chart.ChartData | (canvas: HTMLCanvasElement | null) => Chart.ChartData
The data object that is passed into the Chart.js chart (more info).
This can also be a function, that receives a canvas element and returns the data object.
`tsx
const data = canvas => {
const ctx = canvas.getContext('2d');
const g = ctx.createLinearGradient(...);
return {
datasets: [{
backgroundColor: g,
// ...the rest
}],
};
}
`
#### options
Type: Chart.ChartOptions
The options object that is passed into the Chart.js chart (more info)
#### plugins
Type: Chart.PluginServiceRegistrationOptions[]
The plugins array that is passed into the Chart.js chart (more info)
#### getDatasetAtEvent
Type: (dataset: Array<{}>, event: React.MouseEventundefined
Default:
Proxy for Chart.js getDatasetAtEvent. Calls with dataset and triggering event
#### getElementAtEvent
Type: (element: [{}], event: React.MouseEventundefined
Default:
Proxy for Chart.js getElementAtEvent. Calls with single element array and triggering event
#### getElementsAtEvent
Type: (elements: Array<{}>, event: React.MouseEventundefined
Default:
Proxy for Chart.js getElementsAtEvent. Calls with element array and triggering event
In order for Chart.js to obey the custom size you need to set maintainAspectRatio to false
`tsx`
width={100}
height={50}
options={{ maintainAspectRatio: false }}
/>
The Chart.js instance can be accessed by placing a ref to the element as:
`tsx
const App => {
const ref = useRef();
return
};
`
The canvas node and hence context can be accessed within the data function.
This approach is useful when you want to keep your components pure.
`tsx
render() {
const data = (canvas) => {
const ctx = canvas.getContext('2d')
const gradient = ctx.createLinearGradient(0,0,100,0);
return {
backgroundColor: gradient
// ...the rest
}
}
return
}
`
Chart.js defaults can be set by importing the defaults object:
`tsx
import { defaults } from 'react-chartjs-2';
// Disable animating charts by default.
defaults.animation = false;
`
If you want to bulk set properties, try using the lodash.merge function. This function will do a deep recursive merge preserving previously set values that you don't want to update.
``tsx
import { defaults } from 'react-chartjs-2';
import merge from 'lodash.merge';
merge(defaults, {
animation: false,
line: {
borderColor: '#F85F73',
}
});
` -->