import { bindGlobalStyle, getGlobalStylesId, HtmlVar } from 'lupine.components'; import { chartCommonCss, getChartColor, BasicChartProps } from './chart-utils'; import { Tooltip } from '../tooltip'; export type BarChartProps = BasicChartProps & { xAxisFormatter?: (value: number) => string; }; export const BarChart = (props: BarChartProps) => { const globalCssId = getGlobalStylesId(chartCommonCss); bindGlobalStyle(globalCssId, chartCommonCss); const showLegend = props.showLegend !== false; const series = props.data.series; const labels = props.data.labels; if (!series || series.length === 0 || labels.length === 0) { return
No data
; } // Find max value for X axis scale let maxVal = 0; series.forEach((s) => { s.data.forEach((val) => { if (val > maxVal) maxVal = val; }); }); const tickCount = 5; const order = Math.floor(Math.log10(maxVal || 1)); const magnitude = Math.pow(10, order); const niceStep = Math.ceil(maxVal / magnitude / tickCount) * magnitude; const niceMax = niceStep * tickCount; const xTicks = Array.from({ length: tickCount + 1 }, (_, i) => parseFloat((i * niceStep).toPrecision(12))); const renderChart = (viewBoxWidth: number, viewBoxHeight: number) => { // Layout parameters for Bar Chart (flipped) const padding = { top: 20, right: 60, bottom: 40, left: 150 }; // larger left padding for labels const chartWidth = viewBoxWidth - padding.left - padding.right; const chartHeight = viewBoxHeight - padding.top - padding.bottom; const groupHeight = chartHeight / Math.max(1, labels.length); // Reduce barPadding a bit, ensure the whole group fits inside the allocated groupHeight height. const barPadding = 0.2 * groupHeight; const barHeight = (groupHeight - barPadding) / Math.max(1, series.length); const formatX = props.xAxisFormatter || ((val) => val.toString()); // Render X Axis Ticks & Grid (Vertical lines now) const renderXAxis = () => { return xTicks.map((val) => { const x = padding.left + (val / niceMax) * chartWidth; return ( {formatX(val)} ); }); }; // Render Y Axis Labels (Horizontal text now) const renderYAxis = () => { return labels.map((label, index) => { const y = padding.top + index * groupHeight + groupHeight / 2; return ( {label} ); }); }; // Render Bars (Horizontal rectangles now) const renderBars = () => { const bars: any[] = []; labels.forEach((label, lIndex) => { const groupY = padding.top + lIndex * groupHeight + barPadding / 2; series.forEach((s, sIndex) => { const val = s.data[lIndex] || 0; const color = s.color || getChartColor(sIndex); const barWidth = (val / niceMax) * chartWidth; const x = padding.left; const y = groupY + sIndex * barHeight; const handleMouseEnter = (e: any) => { Tooltip.show( e,
{label}
{s.name}: {formatX(val)}
, { position: 'auto' } ); e.target.style.opacity = 0.8; }; const handleMouseLeave = (e: any) => { Tooltip.hide(); e.target.style.opacity = 1; }; bars.push( ); }); }); return bars; }; return ( {/* Base Axis line (Vertical now) */} {renderXAxis()} {renderYAxis()} {renderBars()} ); }; const chartVar = new HtmlVar(renderChart(1000, Math.max(300, labels.length * 40 + 60))); const ref = { globalCssId, onLoad: async (el: Element) => { const ro = new ResizeObserver((entries) => { const { width } = entries[0].contentRect; if (width > 50) { chartVar.value = renderChart(width, Math.max(300, labels.length * 40 + 60)); } }); ro.observe(el); (el as any)._ro = ro; }, onUnload: async (el: Element) => { if ((el as any)._ro) { (el as any)._ro.disconnect(); } }, }; const ratio = props.aspectRatio ?? 16 / 9; const paddingTop = `${(1 / ratio) * 100}%`; const styleStr = `width: ${props.width || '100%'};`; return (
{props.title &&
{props.title}
}
{chartVar.node}
{showLegend && (
{series.map((s, i) => (
{s.name}
))}
)}
); };