React Components for the popular Chart.js libary
npm install --save @jhonnold/react-chart.js chart.js
import { BarChart } from '@jhonnold/react-chart.js';
<BarChart data={...} />
Direct examples of each chart type can be found in examples
.
Live Examples: Github Pages
import ChartComponent, {
LineChart,
BarChart,
HorizontalBarChart,
RadarChart,
DoughnutChart,
PolarAreaChart,
BubbleChart,
PieChart,
ScatterChart,
} from '@jhonnold/react-chart.js';
const {
id, // string
height, // number
width, // number
redraw, // boolean
type, // string (required only on ChartComponent)
data, // chart.js dataObj or fn that returns dataObj (required)
options, // chart.js options
plugins, // chart.js plugins
} = props;
Type: string
Default: undefined
ID attribute applied to the rendered canvas
Type: number
Default: 150
Height attribute applied to the rendered canvas
Type: number
Default: 300
Width attribute applied to the rendered canvas
Type: boolean
Default: false
If true, will tear down and redraw chart on all updates
Type: 'line' | 'bar' | 'horizontalBar' | 'radar' | 'doughnut' | 'polarArea' | 'bubble' | 'pie' | 'scatter'
Chart.js chart type (required only on ChartComponent)
Type: ((canvas: HTMLCanvasElement | null): Chart.ChartData) | 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.
const data = canvas => {
const ctx = canvas.getContext('2d');
const g = ctx.createLinearGradient(...);
// ...
return {
datasets: [{
backgroundColor: g,
//. ..
}],
// ...
};
}
Type: Chart.ChartOptions
The options object that is passed into the Chart.js chart (more info)
Type: Chart.PluginServiceRegistrationOptions[]
The plugins array that is passed into the Chart.js chart (more info)
MIT © jhonnold