React Chart.js Component

React wrapper for Chart.js 3.0, the most popular charting library.

Installation#

If you want to use our Chart.js React wrapper you have to install an additional package.

Npm#

1npm install @coreui/react-chartjs

Yarn#

1yarn add @coreui/react-chartjs

Chart Types#

Line Chart#

A line chart is a way of plotting data points on a line. Often, it is used to show trend data, or the comparison of two data sets. Line Chart properties

1// use <CChart type="line"> or <CChartLine> component
2<CChart
3 type="line"
4 data={{
5 labels: ["January", "February", "March", "April", "May", "June", "July"],
6 datasets: [
7 {
8 label: "My First dataset",
9 backgroundColor: "rgba(220, 220, 220, 0.2)",
10 borderColor: "rgba(220, 220, 220, 1)",
11 pointBackgroundColor: "rgba(220, 220, 220, 1)",
12 pointBorderColor: "#fff",
13 data: [40, 20, 12, 39, 10, 40, 39, 80, 40]
14 },
15 {
16 label: "My Second dataset",
17 backgroundColor: "rgba(151, 187, 205, 0.2)",
18 borderColor: "rgba(151, 187, 205, 1)",
19 pointBackgroundColor: "rgba(151, 187, 205, 1)",
20 pointBorderColor: "#fff",
21 data: [50, 12, 28, 29, 7, 25, 12, 70, 60]
22 },
23 ],
24 }}
25/>

Bar Chart#

A bar chart provides a way of showing data values represented as vertical bars. It is sometimes used to show trend data, and the comparison of multiple data sets side by side. Bar Chart properties

1// use <CChart type="bar"> or <CChartBar> component
2<CChart
3 type="bar"
4 data={{
5 labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
6 datasets: [
7 {
8 label: 'GitHub Commits',
9 backgroundColor: '#f87979',
10 data: [40, 20, 12, 39, 10, 40, 39, 80, 40],
11 },
12 ],
13 }}
14 labels="months"
15/>

Radar Chart#

A radar chart is a way of showing multiple data points and the variation between them. They are often useful for comparing the points of two or more different data sets. Radar Chart properties

1// use <CChart type="radar"> or <CChartRadar> component
2<CChart
3 type="radar"
4 data={{
5 labels: [
6 'Eating',
7 'Drinking',
8 'Sleeping',
9 'Designing',
10 'Coding',
11 'Cycling',
12 'Running',
13 ],
14 datasets: [
15 {
16 label: 'My First dataset',
17 backgroundColor: 'rgba(220, 220, 220, 0.2)',
18 borderColor: 'rgba(220, 220, 220, 1)',
19 pointBackgroundColor: 'rgba(220, 220, 220, 1)',
20 pointBorderColor: '#fff',
21 pointHighlightFill: '#fff',
22 pointHighlightStroke: 'rgba(220, 220, 220, 1)',
23 data: [65, 59, 90, 81, 56, 55, 40],
24 },
25 {
26 label: 'My Second dataset',
27 backgroundColor: 'rgba(151, 187, 205, 0.2)',
28 borderColor: 'rgba(151, 187, 205, 1)',
29 pointBackgroundColor: 'rgba(151, 187, 205, 1)',
30 pointBorderColor: '#fff',
31 pointHighlightFill: '#fff',
32 pointHighlightStroke: 'rgba(151, 187, 205, 1)',
33 data: [28, 48, 40, 19, 96, 27, 100],
34 },
35 ],
36 }}
37/>

Doughnut and Pie Charts#

Pie and doughnut charts are probably the most commonly used charts. They are divided into segments, the arc of each segment shows the proportional value of each piece of data. Doughnut and Pie Charts properties

1// use <CChart type="doughnut"> or <CChartDoughnut> component
2<CChart
3 type="doughnut"
4 data={{
5 labels: ['VueJs', 'EmberJs', 'ReactJs', 'AngularJs'],
6 datasets: [
7 {
8 backgroundColor: ['#41B883', '#E46651', '#00D8FF', '#DD1B16'],
9 data: [40, 20, 80, 10],
10 },
11 ],
12 }}
13/>

Polar Area Chart#

Polar area charts are similar to pie charts, but each segment has the same angle - the radius of the segment differs depending on the value. Polar Area Chart properties

1// use <CChart type="polarArea"> or <CChartPolarArea> component
2<CChart
3 type="polarArea"
4 data={{
5 labels: ['Red', 'Green', 'Yellow', 'Grey', 'Blue'],
6 datasets: [
7 {
8 data: [11, 16, 7, 3, 14],
9 backgroundColor: ['#FF6384', '#4BC0C0', '#FFCE56', '#E7E9ED', '#36A2EB'],
10 },
11 ],
12 }}
13/>

Bubble Chart#

A bubble chart is used to display three dimensions of data at the same time. The location of the bubble is determined by the first two dimensions and the corresponding horizontal and vertical axes. The third dimension is represented by the size of the individual bubbles. Bubble Chart properties

1// use <CChart type="bubble"> or <CChartBubble> component
2<CChart
3 type="bubble"
4 data={{
5 datasets: [{
6 label: 'First Dataset',
7 data: [{
8 x: 20,
9 y: 30,
10 r: 15
11 }, {
12 x: 40,
13 y: 10,
14 r: 10
15 }],
16 backgroundColor: 'rgb(255, 99, 132)'
17 }]
18 }}
19/>

Scatter Chart#

A bubble chart is used to display three dimensions of data at the same time. The location of the bubble is determined by the first two dimensions and the corresponding horizontal and vertical axes. The third dimension is represented by the size of the individual bubbles. Scatter Chart properties

1// use <CChart type="scatter"> or <CChartScatter> component
2<CChart
3 type="scatter"
4 data={{
5 datasets: [{
6 label: 'Scatter Dataset',
7 data: [{
8 x: -10,
9 y: 0
10 }, {
11 x: 0,
12 y: 10
13 }, {
14 x: 10,
15 y: 5
16 }, {
17 x: 0.5,
18 y: 5.5
19 }],
20 backgroundColor: 'rgb(255, 99, 132)'
21 }],
22 }}
23 options={{
24 scales: {
25 x: {
26 type: 'linear',
27 position: 'bottom'
28 }
29 }
30 }}
31/>

API#

CChart#

1import { CChart } from '@coreui/Users'
2// or
3import CChart from '@coreui/Users/lukaszholeczek/CoreUI/@coreui/react-temp/node_modules/@coreui/react-chartjs/src/CChart'
PropertyDescriptionTypeDefault
classNameA string of all className you want applied to the base component.string-
customTooltipsEnables custom html based tooltips instead of standard tooltips.booleantrue
dataThe data object that is passed into the Chart.js chart (more info).ChartData<keyof ChartTypeRegistry, (number | ScatterDataPoint | BubbleDataPoint)[], unknown> | ((canvas: HTMLCanvasElement) => ChartData<...>)-
fallbackContentA fallback for when the canvas cannot be rendered. Can be used for accessible chart descriptions.

{@link https://www.chartjs.org/docs/latest/general/accessibility.html More Info}
React.ReactNode-
getDatasetAtEventProxy for Chart.js getDatasetAtEvent. Calls with dataset and triggering event.(dataset: InteractionItem[], event: React.MouseEvent<HTMLCanvasElement>) => void-
getElementAtEventProxy for Chart.js getElementAtEvent. Calls with single element array and triggering event.(element: InteractionItem[], event: React.MouseEvent<HTMLCanvasElement>) => void-
getElementsAtEventProxy for Chart.js getElementsAtEvent. Calls with element array and triggering event.(elements: InteractionItem[], event: React.MouseEvent<HTMLCanvasElement>) => void-
heightHeight attribute applied to the rendered canvas.number150
idID attribute applied to the rendered canvas.string-
optionsThe options object that is passed into the Chart.js chart.

{@link https://www.chartjs.org/docs/latest/general/options.html More Info}
_DeepPartialObject<CoreChartOptions<keyof ChartTypeRegistry> & ElementChartOptions<keyof ChartTypeRegistry> & PluginChartOptions<...> & DatasetChartOptions<...> & ScaleChartOptions<...>>-
pluginsThe plugins array that is passed into the Chart.js chart (more info)

{@link https://www.chartjs.org/docs/latest/developers/plugins.html More Info}
Plugin<keyof ChartTypeRegistry, AnyObject>[]-
redrawIf true, will tear down and redraw chart on all updates.booleanfalse
typeChart.js chart type.{'line' | 'bar' | 'radar' | 'doughnut' | 'polarArea' | 'bubble' | 'pie' | 'scatter'}-
widthWidth attribute applied to the rendered canvas.number300
wrapperPut the chart into the wrapper div element.booleantrue