feat: enhance Tauri charts with deduped gridline labels and add tests for bar and line charts

This commit is contained in:
hamed
2026-07-14 14:08:17 +03:30
parent d9f96b68cd
commit 24969173bb
4 changed files with 62 additions and 11 deletions
@@ -13,7 +13,8 @@ export interface ChartPoint {
value: number;
}
/** ~5 rounded gridline ticks covering [0, max], top → bottom. */
/** ~5 rounded gridline ticks covering [0, max], top → bottom. Deduped so tiny
* integer ranges (e.g. max=1) never render repeated labels. */
function niceTicks(max: number, count = 4): number[] {
const safeMax = max > 0 ? max : 1;
const rawStep = safeMax / count;
@@ -21,10 +22,11 @@ function niceTicks(max: number, count = 4): number[] {
const norm = rawStep / mag;
const niceNorm = norm <= 1 ? 1 : norm <= 2 ? 2 : norm <= 5 ? 5 : 10;
const step = niceNorm * mag;
const top = step * count;
const ticks: number[] = [];
for (let i = count; i >= 0; i--) ticks.push(Math.round(step * i));
return ticks; // e.g. [400,300,200,100,0]
// collapse duplicate rounded labels (keeps gridlines proportional via ticks[0])
const deduped = ticks.filter((t, i) => i === 0 || t !== ticks[i - 1]);
return deduped.length >= 2 ? deduped : [ticks[0], 0];
}
const faNum = new Intl.NumberFormat('fa-IR');
@@ -85,7 +87,7 @@ function ChartFrame({
/** Bar chart — thin #5559CE columns (source: BarPlot, categoryGapRatio 0.7). */
export function TauriBarChart({ data }: { data: ChartPoint[] }) {
if (!data.length) {
if (!data.length || !data.some((d) => d.value > 0)) {
return <EmptyChart />;
}
const max = Math.max(...data.map((d) => d.value), 1);
@@ -115,7 +117,7 @@ export function TauriBarChart({ data }: { data: ChartPoint[] }) {
/** Line + area chart — #5559CE line over a #3A6FF8 gradient (source: LinePlot). */
export function TauriLineChart({ data }: { data: ChartPoint[] }) {
if (data.length < 2) {
if (data.length < 2 || !data.some((d) => d.value > 0)) {
return <EmptyChart />;
}
const values = data.map((d) => d.value);