A bar chart or bar graph is a chart that presents grouped data with rectangular bars with lengths proportional to the values that they represent.
import React, { useState } from 'react';
import { Chart } from 'primereact/chart';
const BarChartDemo = () => {
const [basicData] = useState({
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
label: 'My First dataset',
backgroundColor: '#42A5F5',
data: [65, 59, 80, 81, 56, 55, 40]
},
{
label: 'My Second dataset',
backgroundColor: '#FFA726',
data: [28, 48, 40, 19, 86, 27, 90]
}
]
});
const [multiAxisData] = useState({
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Dataset 1',
backgroundColor: [
'#EC407A',
'#AB47BC',
'#42A5F5',
'#7E57C2',
'#66BB6A',
'#FFCA28',
'#26A69A'
],
yAxisID: 'y',
data: [65, 59, 80, 81, 56, 55, 10]
}, {
label: 'Dataset 2',
backgroundColor: '#78909C',
yAxisID: 'y1',
data: [28, 48, 40, 19, 86, 27, 90]
}]
});
const [stackedData] = useState({
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
type: 'bar',
label: 'Dataset 1',
backgroundColor: '#42A5F5',
data: [
50,
25,
12,
48,
90,
76,
42
]
}, {
type: 'bar',
label: 'Dataset 2',
backgroundColor: '#66BB6A',
data: [
21,
84,
24,
75,
37,
65,
34
]
}, {
type: 'bar',
label: 'Dataset 3',
backgroundColor: '#FFA726',
data: [
41,
52,
24,
74,
23,
21,
32
]
}]
});
const getLightTheme = () => {
let basicOptions = {
maintainAspectRatio: false,
aspectRatio: .8,
plugins: {
legend: {
labels: {
color: '#495057'
}
}
},
scales: {
x: {
ticks: {
color: '#495057'
},
grid: {
color: '#ebedef'
}
},
y: {
ticks: {
color: '#495057'
},
grid: {
color: '#ebedef'
}
}
}
};
let horizontalOptions = {
indexAxis: 'y',
maintainAspectRatio: false,
aspectRatio: .8,
plugins: {
legend: {
labels: {
color: '#495057'
}
}
},
scales: {
x: {
ticks: {
color: '#495057'
},
grid: {
color: '#ebedef'
}
},
y: {
ticks: {
color: '#495057'
},
grid: {
color: '#ebedef'
}
}
}
};
let stackedOptions = {
maintainAspectRatio: false,
aspectRatio: .8,
plugins: {
tooltips: {
mode: 'index',
intersect: false
},
legend: {
labels: {
color: '#495057'
}
}
},
scales: {
x: {
stacked: true,
ticks: {
color: '#495057'
},
grid: {
color: '#ebedef'
}
},
y: {
stacked: true,
ticks: {
color: '#495057'
},
grid: {
color: '#ebedef'
}
}
}
};
let multiAxisOptions = {
maintainAspectRatio: false,
aspectRatio: .8,
plugins: {
legend: {
labels: {
color: '#495057'
}
},
tooltips: {
mode: 'index',
intersect: true
}
},
scales: {
x: {
ticks: {
color: '#495057'
},
grid: {
color: '#ebedef'
}
},
y: {
type: 'linear',
display: true,
position: 'left',
ticks: {
min: 0,
max: 100,
color: '#495057'
},
grid: {
color: '#ebedef'
}
},
y1: {
type: 'linear',
display: true,
position: 'right',
grid: {
drawOnChartArea: false,
color: '#ebedef'
},
ticks: {
min: 0,
max: 100,
color: '#495057'
}
}
}
};
return {
basicOptions,
horizontalOptions,
stackedOptions,
multiAxisOptions
}
}
const { basicOptions, horizontalOptions, multiAxisOptions, stackedOptions } = getLightTheme();
return (
<div>
<div className="card">
<h5>Vertical</h5>
<Chart type="bar" data={basicData} options={basicOptions} />
</div>
<div className="card">
<h5>Horizontal</h5>
<Chart type="bar" data={basicData} options={horizontalOptions} />
</div>
<div className="card">
<h5>Multi Axis</h5>
<Chart type="bar" data={multiAxisData} options={multiAxisOptions} />
</div>
<div className="card">
<h5>Stacked</h5>
<Chart type="bar" data={stackedData} options={stackedOptions} />
</div>
</div>
)
}
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.