import { bindGlobalStyle, getGlobalStylesId, HtmlVar } from 'lupine.components';
import { chartCommonCss, getChartColor, BasicChartProps } from './chart-utils';
import { Tooltip } from '../tooltip';
export type ColumnChartProps = BasicChartProps & {
yAxisFormatter?: (value: number) => string;
};
export const ColumnChart = (props: ColumnChartProps) => {
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 Y axis scale
let maxVal = 0;
series.forEach((s) => {
s.data.forEach((val) => {
if (val > maxVal) maxVal = val;
});
});
// Calculate nice Y axis ticks (simple approach)
const tickCount = 5;
// const tickStep = maxVal > 0 ? Math.ceil(maxVal / tickCount / 10) * 10 : 10;
// maxVal = Math.max(maxVal, tickStep * tickCount);
// Actually, a better simple nice step:
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 yTicks = Array.from({ length: tickCount + 1 }, (_, i) => parseFloat((i * niceStep).toPrecision(12)));
const formatY = props.yAxisFormatter || ((val) => val.toString());
const renderChart = (viewBoxWidth: number, viewBoxHeight: number) => {
// Layout parameters
const padding = { top: 20, right: 20, bottom: 40, left: 50 };
const chartWidth = viewBoxWidth - padding.left - padding.right;
const chartHeight = viewBoxHeight - padding.top - padding.bottom;
const groupWidth = chartWidth / labels.length;
const groupPadding = 0.2 * groupWidth;
const columnWidth = (groupWidth - groupPadding) / series.length;
// Render X Axis
const renderXAxis = () => {
return labels.map((label, index) => {
const x = padding.left + index * groupWidth + groupWidth / 2;
const y = viewBoxHeight - padding.bottom + 25;
return (
{label}
);
});
};
// Render Y Axis Labels
const renderYAxis = () => {
return yTicks.map((val) => {
const y = padding.top + chartHeight - (val / niceMax) * chartHeight;
return (
{formatY(val)}
);
});
};
// Render Columns
const renderColumns = () => {
const columns: any[] = [];
labels.forEach((label, lIndex) => {
const groupX = padding.left + lIndex * groupWidth + groupPadding / 2;
series.forEach((s, sIndex) => {
const val = s.data[lIndex] || 0;
const color = s.color || getChartColor(sIndex);
const colHeight = (val / niceMax) * chartHeight;
const x = groupX + sIndex * columnWidth;
const y = padding.top + chartHeight - colHeight;
const handleMouseEnter = (e: any) => {
Tooltip.show(
e,
,
{ position: 'auto' }
);
e.target.style.opacity = 0.8;
};
const handleMouseLeave = (e: any) => {
Tooltip.hide();
e.target.style.opacity = 1;
};
columns.push(
);
});
});
return columns;
};
return (
);
};
const chartVar = new HtmlVar(renderChart(1000, 300)); // Default wide ratio initial paint
const ref = {
globalCssId,
onLoad: async (el: Element) => {
const ro = new ResizeObserver((entries) => {
const { width, height } = entries[0].contentRect;
if (width > 50 && height > 50) {
chartVar.value = renderChart(width, height);
}
});
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) => (
))}
)}
);
};