import { JSDOM } from 'jsdom';
import * as d3 from 'd3';
import { Document, Page, Svg } from '@formepdf/react';
function renderBarChart(data: { label: string; value: number }[]): string {
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>');
const body = d3.select(dom.window.document.body);
const width = 400;
const height = 200;
const margin = { top: 20, right: 20, bottom: 40, left: 40 };
const svg = body.append('svg')
.attr('xmlns', 'http://www.w3.org/2000/svg')
.attr('width', width)
.attr('height', height);
const x = d3.scaleBand()
.domain(data.map(d => d.label))
.range([margin.left, width - margin.right])
.padding(0.2);
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value) || 0])
.range([height - margin.bottom, margin.top]);
// Bars
svg.selectAll('rect')
.data(data)
.join('rect')
.attr('x', d => x(d.label) || 0)
.attr('y', d => y(d.value))
.attr('width', x.bandwidth())
.attr('height', d => height - margin.bottom - y(d.value))
.attr('fill', '#3b82f6');
// X axis labels
svg.selectAll('.label')
.data(data)
.join('text')
.attr('x', d => (x(d.label) || 0) + x.bandwidth() / 2)
.attr('y', height - margin.bottom + 16)
.attr('text-anchor', 'middle')
.attr('font-size', '10')
.attr('fill', '#64748b')
.text(d => d.label);
return body.html();
}
export default function Report(data: any) {
const chartSvg = renderBarChart(data.chartData);
return (
<Document>
<Page size="Letter" margin={54}>
<Svg width={400} height={200} content={chartSvg} />
</Page>
</Document>
);
}