@diagramers/admin
Version:
Diagramers Admin Template - React starter for admin dashboards.
126 lines (119 loc) • 3.28 kB
JavaScript
/* eslint-disable no-underscore-dangle,no-unused-vars */
import React, { useEffect, useRef } from 'react';
import { Chart, registerables } from 'chart.js';
import { useSelector } from 'react-redux';
const ChartRoundedBar = () => {
const { themeValues } = useSelector((state) => state.settings);
const chartContainer = useRef(null);
const LegendLabels = React.useMemo(() => {
return {
font: {
size: 14,
family: themeValues.font,
},
padding: 20,
usePointStyle: true,
boxWidth: 10,
};
}, [themeValues]);
const ChartTooltip = React.useMemo(() => {
return {
enabled: true,
position: 'nearest',
backgroundColor: themeValues.foreground,
titleColor: themeValues.primary,
titleFont: themeValues.font,
bodyColor: themeValues.body,
bodyFont: themeValues.font,
bodySpacing: 10,
padding: 15,
borderColor: themeValues.separator,
borderWidth: 1,
cornerRadius: parseInt(themeValues.borderRadiusMd, 10),
displayColors: false,
intersect: true,
mode: 'point',
};
}, [themeValues]);
const data = React.useMemo(() => {
return {
labels: ['January', 'February', 'March', 'April'],
datasets: [
{
label: 'Breads',
borderColor: themeValues.primary,
backgroundColor: `rgba(${themeValues.primaryrgb},0.1)`,
data: [456, 479, 424, 569],
},
{
label: 'Patty',
borderColor: themeValues.secondary,
backgroundColor: `rgba(${themeValues.secondaryrgb},0.1)`,
data: [364, 504, 605, 400],
},
],
};
}, [themeValues]);
const config = React.useMemo(() => {
return {
type: 'bar',
options: {
elements: {
bar: {
borderWidth: 1.5,
borderRadius: parseInt(themeValues.borderRadiusMd, 10),
borderSkipped: false,
},
},
plugins: {
crosshair: false,
datalabels: false,
legend: {
position: 'bottom',
labels: LegendLabels,
},
tooltip: ChartTooltip,
streaming: false,
},
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
min: 300,
max: 800,
grid: {
display: true,
lineWidth: 1,
color: themeValues.separatorLight,
drawBorder: false,
},
ticks: {
beginAtZero: true,
stepSize: 100,
padding: 8,
fontColor: themeValues.alternate,
},
},
x: {
grid: { display: false, drawBorder: false },
},
},
},
data,
};
}, [data, LegendLabels, ChartTooltip, themeValues]);
useEffect(() => {
let myChart = null;
if (chartContainer && chartContainer.current) {
Chart.register(...registerables);
myChart = new Chart(chartContainer.current, config);
}
return () => {
if (myChart) {
myChart.destroy();
}
};
}, [config]);
return <canvas ref={chartContainer} />;
};
export default React.memo(ChartRoundedBar);