165 lines
5.1 KiB
JavaScript
165 lines
5.1 KiB
JavaScript
import React, { useRef, useEffect } from 'react';
|
|
|
|
import {
|
|
Chart, BarController, BarElement, LinearScale, CategoryScale, Tooltip, Legend,
|
|
} from 'chart.js';
|
|
import 'chartjs-adapter-moment';
|
|
|
|
// Import utilities
|
|
import { tailwindConfig, formatValue } from '../utils/Utils';
|
|
|
|
// Import images
|
|
import revolutIcon from '../images/company-icon-01.svg';
|
|
import hsbcIcon from '../images/company-icon-02.svg';
|
|
import qontoIcon from '../images/company-icon-03.svg';
|
|
import n26Icon from '../images/company-icon-04.svg';
|
|
|
|
Chart.register(BarController, BarElement, LinearScale, CategoryScale, Tooltip, Legend);
|
|
const images = [revolutIcon, hsbcIcon, qontoIcon, n26Icon];
|
|
|
|
function BarChart06({
|
|
data,
|
|
width,
|
|
height
|
|
}) {
|
|
|
|
const canvas = useRef(null);
|
|
const legend = useRef(null);
|
|
|
|
useEffect(() => {
|
|
const ctx = canvas.current;
|
|
// eslint-disable-next-line no-unused-vars
|
|
const chart = new Chart(ctx, {
|
|
type: 'bar',
|
|
data: data,
|
|
options: {
|
|
indexAxis: 'y',
|
|
layout: {
|
|
padding: {
|
|
top: 12,
|
|
bottom: 16,
|
|
left: 72,
|
|
right: 20,
|
|
},
|
|
},
|
|
scales: {
|
|
y: {
|
|
grid: {
|
|
display: false,
|
|
drawBorder: false,
|
|
drawTicks: false,
|
|
},
|
|
ticks: {
|
|
display: false,
|
|
},
|
|
},
|
|
x: {
|
|
grid: {
|
|
drawBorder: false,
|
|
},
|
|
ticks: {
|
|
maxTicksLimit: 3,
|
|
align: 'end',
|
|
callback: (value) => formatValue(value),
|
|
},
|
|
},
|
|
},
|
|
plugins: {
|
|
legend: {
|
|
display: false,
|
|
},
|
|
tooltip: {
|
|
callbacks: {
|
|
title: () => false, // Disable tooltip title
|
|
label: (context) => formatValue(context.parsed.x),
|
|
},
|
|
},
|
|
},
|
|
interaction: {
|
|
intersect: false,
|
|
mode: 'nearest',
|
|
},
|
|
animation: {
|
|
duration: 500,
|
|
},
|
|
maintainAspectRatio: false,
|
|
resizeDelay: 200,
|
|
},
|
|
plugins: [
|
|
{
|
|
id: 'htmlLegend',
|
|
afterUpdate(c, args, options) {
|
|
const ul = legend.current;
|
|
if (!ul) return;
|
|
// Remove old legend items
|
|
while (ul.firstChild) {
|
|
ul.firstChild.remove();
|
|
}
|
|
// Reuse the built-in legendItems generator
|
|
const items = c.options.plugins.legend.labels.generateLabels(c);
|
|
items.forEach((item) => {
|
|
const li = document.createElement('li');
|
|
li.style.marginRight = tailwindConfig().theme.margin[4];
|
|
// Button element
|
|
const button = document.createElement('button');
|
|
button.style.display = 'inline-flex';
|
|
button.style.alignItems = 'center';
|
|
button.style.opacity = item.hidden ? '.3' : '';
|
|
button.onclick = () => {
|
|
c.setDatasetVisibility(item.datasetIndex, !c.isDatasetVisible(item.datasetIndex));
|
|
c.update();
|
|
};
|
|
// Color box
|
|
const box = document.createElement('span');
|
|
box.style.display = 'block';
|
|
box.style.width = tailwindConfig().theme.width[3];
|
|
box.style.height = tailwindConfig().theme.height[3];
|
|
box.style.borderRadius = tailwindConfig().theme.borderRadius.full;
|
|
box.style.marginRight = tailwindConfig().theme.margin[2];
|
|
box.style.borderWidth = '3px';
|
|
box.style.borderColor = item.fillStyle;
|
|
box.style.pointerEvents = 'none';
|
|
// Label
|
|
const label = document.createElement('span');
|
|
label.style.color = tailwindConfig().theme.colors.slate[500];
|
|
label.style.fontSize = tailwindConfig().theme.fontSize.sm[0];
|
|
label.style.lineHeight = tailwindConfig().theme.fontSize.sm[1].lineHeight;
|
|
const labelText = document.createTextNode(item.text);
|
|
label.appendChild(labelText);
|
|
li.appendChild(button);
|
|
button.appendChild(box);
|
|
button.appendChild(label);
|
|
ul.appendChild(li);
|
|
});
|
|
},
|
|
afterDraw(c) {
|
|
const xAxis = c.scales.x;
|
|
const yAxis = c.scales.y;
|
|
yAxis.ticks.forEach((value, index) => {
|
|
const y = yAxis.getPixelForTick(index);
|
|
const image = new Image();
|
|
image.src = images[index];
|
|
c.ctx.drawImage(image, xAxis.left - 52, y - 18);
|
|
});
|
|
},
|
|
},
|
|
],
|
|
});
|
|
return () => chart.destroy();
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
return (
|
|
<React.Fragment>
|
|
<div className="px-5 py-4">
|
|
<ul ref={legend} className="flex flex-wrap"></ul>
|
|
</div>
|
|
<div className="grow">
|
|
<canvas ref={canvas} width={width} height={height}></canvas>
|
|
</div>
|
|
</React.Fragment>
|
|
);
|
|
}
|
|
|
|
export default BarChart06;
|