import { BarElement, CategoryScale, Chart as ChartJS, Legend, LinearScale, Title, Tooltip } from 'chart.js'; import ChartDataLabels from 'chartjs-plugin-datalabels'; import 'chartjs-plugin-style'; import { draw } from 'patternomaly'; import { Chart } from 'react-chartjs-2'; import ChartTitle from '../ChartTitle'; import { GrossAnualChartView } from './GrossAnualChartView'; ChartJS.register( CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend, ChartDataLabels, ); interface SingleBarInterface { title: string, subtitle: string, dataProps: any, label: Array, dataset: string, barLabel?: boolean | undefined, miniature?: boolean | undefined, bruta?: boolean | undefined } export function GrossAnualChart({ title, subtitle, dataProps = [], label, dataset, barLabel, miniature, bruta }: SingleBarInterface) { function spacement(string) { const spaces = string.length === 1 ? '' : string.length === 2 ? '' : string.length === 3 ? ' ' : string.length === 4 ? ' ' : string.length === 5 ? ' ' : '' return spaces } const options: any = { responsive: true, scales: { x: { stacked: true, grid: { display: false }, ticks: { font: { size: !miniature ? window.innerWidth / 90 : window.innerWidth / 125 } }, }, y: { stacked: false, max: Number.parseInt(dataProps.reduce((prev, current) => prev.economia_acumulada < current.economia_acumulada ? prev.economia_acumulada : current.economia_acumulada, 0)) + 350, min: 0, grid: { display: false }, ticks: { font: { size: !miniature ? window.innerWidth / 90 : window.innerWidth / 125 } }, }, }, axisY: { }, series: { downsample: { threshold: 1000 } }, plugins: { datalabels: { formatter: (value, ctx) => { let sum = 0; const dataArr = ctx.chart.data.datasets[0].data; dataArr.map(data => { sum += data; }); const percentage = (dataProps[ctx.dataIndex]?.econ_percentual * 100).toFixed(0) + "%"; const result = `${spacement(parseInt(value).toLocaleString('pt-br'))}${percentage}\n${parseInt(value).toLocaleString('pt-br')}${spacement(parseInt(value).toLocaleString('pt-br'))}` return value == null ? null : result }, display: true, anchor: "end", align: "end", font: { weight: 'bold', size: !miniature ? window.innerWidth / 80 : window.innerWidth / 125, }, color: (value) => { return value.dataset.label === 'Consolidada' ? '#fff' : '#255488' }, }, legend: { position: 'bottom' as const, }, title: { display: false, text: '', }, }, }; const labels: string[] = label.filter((item, pos) => { return label.indexOf(item) == pos; }); const data: any = { labels, datasets: [ { type: 'bar', label: dataset, stacked: true, data: dataProps.filter(value => value.dad_estimado === false).map((value, index) => { return parseFloat(value.economia_acumulada) }), datalabels: { backgroundColor: '#255488', borderRadius: 8, opacity: .8, }, borderRadius: 10, backgroundColor: '#255488', }, { type: 'bar', stacked: true, label: 'Estimado', spanGaps: true, datalabels: { offset: dataProps.filter(value => value.dad_estimado === true).map((value, index) => { if (index === 1) { return 30 } return 0 }) }, data: [].concat(dataProps.filter(value => value.dad_estimado === true).slice(0, 7).map((value, index) => { return parseFloat(value?.economia_acumulada) })), borderRadius: 10, backgroundColor: draw('diagonal-right-left', '#C2d5fb'), }, ], } return ( ) }