Skip to content
15 changes: 15 additions & 0 deletions .changeset/37053-chart-axis-measure-units.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@object-ui/plugin-charts': patch
---

fix(plugin-charts): a chart's value axis prints its dataset measure's percent and currency units

Clause-②: yes
Metadata-protocol: none

- **A value axis over a dataset measure reads in the measure's units.** The chart renderer's internal series props carry one more producer-filled key, `series[].measureFormat` (the measure's `format`, `currency` and `percentScale`), beside `series[].aggregate`. The report chart and the dashboard widget fill it from the dataset answer; it is not meant to be authored, and objectstack#37214 refuses it on a static chart node. A value axis whose series all carry the same one prints its ticks, and the data labels that share its format, in that measure's units:
- a percent pattern prints through `formatMeasure`, the function the table uses, at the column's own `percentScale`. A measure stored in percentage points (`percentScale: 'whole'`) reads `0.0%` … `36.0%`, where the axis printed `0` … `36`;
- a measure with one ISO currency prints the compact default with that currency's symbol (`CN¥3K` … `CN¥12K`, `¥30万` in Chinese), where the axis printed `3K` … `12K`.
- A declared axis `format` still wins over the measure's units. A measure with a per-row currency (`currencyField`) has no single currency, so its axis keeps plain numbers. Two series on one axis that are formatted differently also keep the default axis.

Nothing to migrate: no metadata key changes, and the widened series type is the renderer's internal one.
12 changes: 12 additions & 0 deletions .changeset/37053-dashboard-chart-measure-format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@object-ui/plugin-dashboard': patch
---

fix(plugin-dashboard): a dashboard chart widget's value axis prints its measure's percent and currency units

Clause-②: yes
Metadata-protocol: none

A dataset chart widget's series now carry the format facts of the measure they plot (`format`, `percentScale` and its single currency) as the chart's internal `series[].measureFormat`. The value axis then prints a percent measure as percents (`0.0%` … `36.0%` for a rate in percentage points, which drew `0` … `36` before) and a currency amount with its symbol, as the widget's table prints them. A measure with a per-row currency (`currencyField`) keeps plain numbers. A dimensionless chart that plots several measures carries a format only when they all share it.

Nothing to migrate: no metadata key changes.
12 changes: 12 additions & 0 deletions .changeset/37053-report-chart-measure-format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@object-ui/plugin-report': patch
---

fix(plugin-report): a report chart hands its chart the measure's format, so the value axis reads `33.3%` and `¥` beside the table

Clause-②: yes
Metadata-protocol: none

A report chart's series now carry the plotted measure's `format`, `percentScale` and single currency as the chart's internal `series[].measureFormat`. The chart's value axis then prints a measure stored in percentage points (`percentScale: 'whole'`) as percents and a currency amount with its symbol, as the table beside it does. A measure with a per-row currency (`currencyField`) hands no currency, so its axis keeps plain numbers. The fraction-scale percent pattern still travels on the value-axis `format` key as before.

Nothing to migrate: no metadata key changes.
198 changes: 198 additions & 0 deletions ui/packages/plugin-charts/src/AdvancedChartImpl.axisLabels.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import React from 'react';
import { describe, it, expect, afterEach, vi } from 'vitest';
import { render, cleanup } from '@testing-library/react';
import { formatMeasure } from '@object-ui/core';
import { LocalizationProvider } from '@object-ui/i18n';

vi.mock('recharts', async () => {
const actual = await vi.importActual<any>('recharts');
Expand Down Expand Up @@ -134,3 +136,199 @@ describe('AdvancedChartImpl — value-axis ticks on a small domain (objectstack#
expect(ticks).toEqual(['0%', '0.85%', '1.7%', '2.55%', '3.4%']);
});
});

/**
* objectstack#37053 — a value axis prints its dataset measure's UNITS: a
* whole-scale percent and a currency amount, beside a table that prints them
* through `formatMeasure`.
*
* The chart's value-axis `format` key is a d3-style pattern (`formatterFor`):
* it reads a `%` pattern as a 0–1 fraction and has no slot for an ISO
* currency, so a report or dashboard chart over a `percentScale: 'whole'` rate
* or a `CNY` amount printed bare numbers (`0` … `34`, `0` … `12K`) beside a
* table printing `33.3%` and `¥12,000.00`. The producer now hands each series
* the measure's `{ format, currency, percentScale }` (`series[].measureFormat`,
* renderer-internal like `series[].aggregate`), and an axis whose series all
* carry the same one prints that measure's units:
*
* - a percent pattern through `formatMeasure` itself, at the column's scale;
* - a currency as the compact default carrying the currency's symbol.
*
* A per-row currency column (`currencyField`) has no single currency, so its
* producer hands no `currency` and the axis keeps distinct plain numbers — the
* control. Every chart here is built the way `ChartRenderer` builds it: the
* schema through `normalizeChartSchema`, whose series reader is where the
* carrier is kept or dropped.
*
* A new top-level `describe` in this axis-tick harness, beside the
* objectstack#36988 one above it.
*/
describe('AdvancedChartImpl — a value axis prints its dataset measure’s units (objectstack#37053)', () => {
/** The display locale the chart resolves with no provider mounted. */
const LOCALE = 'en';

const CATEGORIES = new Set(['High', 'Medium', 'Low']);

/** The value tick labels the left axis draws, in order. */
const valueTicks = (c: HTMLElement) =>
[...c.querySelectorAll('text.recharts-cartesian-axis-tick-value')]
.filter((t) => t.getAttribute('orientation') === 'left')
.map((t) => (t.textContent ?? '').trim())
.filter((t) => !CATEGORIES.has(t));

/** The data labels the bars print (`showDataLabels`). */
const dataLabels = (c: HTMLElement) =>
[...c.querySelectorAll('.recharts-label-list text')].map((t) => (t.textContent ?? '').trim());

/**
* Draw one column chart over `rows`, its `value` series carrying `measureFormat`
* exactly as a producer hands it, through the translation `ChartRenderer`
* applies to every schema.
*/
function draw(
rows: Array<{ priority: string; value: number }>,
measureFormat: Record<string, unknown> | undefined,
extra: Record<string, unknown> = {},
locale?: string,
) {
const spec = normalizeChartSchema({
type: 'chart',
chartType: 'column',
xAxisKey: 'priority',
series: [{ dataKey: 'value', label: 'Value', ...(measureFormat ? { measureFormat } : {}) }],
...extra,
});
const { container } = render(
<AdvancedChartImpl
chartType="column"
data={rows}
xAxisKey="priority"
series={spec.series}
yAxes={spec.yAxes}
showDataLabels={spec.showDataLabels}
isAnimationActive={false}
/>,
// The display locale the chart reads (`useDisplayLocale`), when a case names one.
locale
? { wrapper: ({ children }: { children: React.ReactNode }) => <LocalizationProvider value={{ locale }}>{children}</LocalizationProvider> }
: undefined,
);
return container;
}

// A rate stored in percentage points (`percentScale: 'whole'`): one bucket at 33.3%.
const WHOLE_RATE = [
{ priority: 'High', value: 100 / 3 },
{ priority: 'Medium', value: 0 },
{ priority: 'Low', value: 0 },
];
// An amount column, in thousands of yuan.
const AMOUNTS = [
{ priority: 'High', value: 12000 },
{ priority: 'Medium', value: 4500 },
{ priority: 'Low', value: 0 },
];

it('SUBJECT: a whole-scale percent measure’s ticks read percents, in the table’s own format', () => {
const measure = { format: '0.0%', percentScale: 'whole' };
const ticks = valueTicks(draw(WHOLE_RATE, measure));
expect(ticks).toEqual(['0.0%', '9.0%', '18.0%', '27.0%', '36.0%']);
// The table prints the bucket's value through `formatMeasure` at the same
// scale and pattern; each tick is that function's reading of its tick.
expect(formatMeasure(100 / 3, '0.0%', undefined, 'whole', LOCALE)).toBe('33.3%');
expect(ticks).toEqual([0, 9, 18, 27, 36].map((t) => formatMeasure(t, '0.0%', undefined, 'whole', LOCALE)));
});

it('SUBJECT: a fixed-currency measure’s ticks carry its symbol', () => {
const ticks = valueTicks(draw(AMOUNTS, { format: '0,0.00', currency: 'CNY' }));
expect(ticks).toEqual(['CN¥0', 'CN¥3K', 'CN¥6K', 'CN¥9K', 'CN¥12K']);
// The symbol is the one the table prints the amount with.
expect(formatMeasure(12000, '0,0.00', 'CNY', undefined, LOCALE)).toBe('CN¥12,000.00');
});

it('a second currency carries its own symbol (`USD` → `$12K`)', () => {
const ticks = valueTicks(draw(AMOUNTS, { format: '0,0.00', currency: 'USD' }));
expect(ticks).toEqual(['$0', '$3K', '$6K', '$9K', '$12K']);
expect(formatMeasure(12000, '0,0.00', 'USD', undefined, LOCALE)).toBe('$12,000.00');
});

it('a zh-CN session reads the currency in its own compact units (`¥30万`)', () => {
const large = AMOUNTS.map((r) => ({ ...r, value: r.value * 100 }));
const ticks = valueTicks(draw(large, { format: '0,0.00', currency: 'CNY' }, {}, 'zh-CN'));
expect(ticks).toEqual(['¥0', '¥30万', '¥60万', '¥90万', '¥120万']);
expect(formatMeasure(1200000, '0,0.00', 'CNY', undefined, 'zh-CN')).toBe('¥1,200,000.00');
});

it('CONTROL: a per-row currency measure (no single currency) keeps distinct plain-number ticks', () => {
// A `currencyField` column's producer hands its pattern and no currency.
const ticks = valueTicks(draw(AMOUNTS, { format: '0,0.00' }));
expect(ticks).toEqual(['0', '3K', '6K', '9K', '12K']);
expect(new Set(ticks).size).toBe(ticks.length);
expect(ticks.some((t) => /[¥$€£%]/.test(t))).toBe(false);
});

it('a fraction-scale percent measure reads percents through the carrier alone (the dashboard’s case)', () => {
const rate = WHOLE_RATE.map((r) => ({ ...r, value: r.value / 100 }));
expect(valueTicks(draw(rate, { format: '0.0%' }))).toEqual(['0.0%', '8.5%', '17.0%', '25.5%', '34.0%']);
});

it('a declared axis `format` wins over the carrier: a `0,0` axis over a `CNY` measure prints no symbol', () => {
// The two readings differ here, so swapping their order in
// `valueAxisTickFormatter` turns this red.
const ticks = valueTicks(draw(AMOUNTS, { format: '0,0.00', currency: 'CNY' }, { yAxis: [{ format: '0,0' }] }));
expect(ticks).toEqual(['0', '3,000', '6,000', '9,000', '12,000']);
expect(ticks.some((t) => t.includes('CN¥'))).toBe(false);
});

it('the report’s fraction-scale `yAxis[0].format` path still reads percents beside its carrier (objectstack#36988)', () => {
const rate = [
{ priority: 'High', value: 1 / 3 },
{ priority: 'Medium', value: 0 },
{ priority: 'Low', value: 0 },
];
const ticks = valueTicks(draw(rate, { format: '0.0%' }, { yAxis: [{ format: '0.0%' }] }));
expect(ticks).toEqual(['0.0%', '8.5%', '17.0%', '25.5%', '34.0%']);
});

it('a pattern too coarse for the step gains the places the step needs, so no two ticks share a label', () => {
const rate = [
{ priority: 'High', value: 3.4 },
{ priority: 'Medium', value: 0 },
{ priority: 'Low', value: 0 },
];
const ticks = valueTicks(draw(rate, { format: '0%', percentScale: 'whole' }));
// `formatMeasure` prints a pattern's places as a fixed width, so every tick
// carries the two the 0.85-point step needs.
expect(ticks).toEqual(['0.00%', '0.85%', '1.70%', '2.55%', '3.40%']);
expect(new Set(ticks).size).toBe(ticks.length);
});

it('data labels print with the axis’ measure format', () => {
const labels = dataLabels(draw(WHOLE_RATE, { format: '0.0%', percentScale: 'whole' }, { showDataLabels: true }));
expect(labels).toContain('33.3%');
});

it('two series formatted differently on one axis keep the default axis', () => {
const spec = normalizeChartSchema({
type: 'chart',
chartType: 'column',
xAxisKey: 'priority',
series: [
{ dataKey: 'value', measureFormat: { format: '0.0%', percentScale: 'whole' } },
{ dataKey: 'other', measureFormat: { format: '0,0.00', currency: 'CNY' } },
],
});
const { container } = render(
<AdvancedChartImpl
chartType="column"
data={WHOLE_RATE.map((r) => ({ ...r, other: r.value }))}
xAxisKey="priority"
series={spec.series}
isAnimationActive={false}
/>,
);
const ticks = valueTicks(container);
expect(ticks.length).toBeGreaterThanOrEqual(3);
expect(ticks.some((t) => /[¥%]/.test(t))).toBe(false);
});
});
22 changes: 14 additions & 8 deletions ui/packages/plugin-charts/src/AdvancedChartImpl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import {
ChartContainerConfig
} from './ChartContainerImpl';
import { mapScatterClick, mapTreemapClick, mapSankeyClick } from './chartDrillEvents';
import { formatterFor, compactTickFormatter, valueTickExponent, domainFor, ticksFor, plotsWholeNumbers, wholeNumberTickCount, RENDERABLE, SINGLE_VALUE_CHART_TYPES, TABULAR_CHART_TYPES, effectiveChartFamily, comboBaseFamily, placeYAxes, type DeclaredChartFamily, type NormalizedAxis, type NormalizedSeries, type ValueAxisSlot, type YAxisPlacement } from './normalizeChartSchema';
import { formatterFor, compactTickFormatter, measureTickFormatter, sharedMeasureFormat, valueTickExponent, domainFor, ticksFor, plotsWholeNumbers, wholeNumberTickCount, RENDERABLE, SINGLE_VALUE_CHART_TYPES, TABULAR_CHART_TYPES, effectiveChartFamily, comboBaseFamily, placeYAxes, type DeclaredChartFamily, type NormalizedAxis, type NormalizedSeries, type ValueAxisSlot, type YAxisPlacement } from './normalizeChartSchema';
import { buildCategoryRank, chartRowBucketId, formatMonthBucketLabel, isRealCalendarDate, toDateInputValue, toDisplayDate, type ChartSegmentClickEvent } from '@object-ui/core';
import { createSafeTranslation, useDisplayLocale, useSafeTranslate } from '@object-ui/i18n';

Expand Down Expand Up @@ -2593,18 +2593,24 @@ function AdvancedChartImplInner({

/**
* The tick formatter of one value axis: the author's `format` where it
* declares one, else the compact default (1,200,000 → `1.2M`) — either
* printed to the decimal place the axis' tick step needs, so two ticks never
* share a label (objectstack#36988; see `valueTickExponent`). The step is
* measured over the values plotted in the slot, with the tick count
* `yAxisSpecProps` hands recharts for it.
* declares one, else the units of the dataset measure its series plot
* (objectstack#37053: a percent at its own scale, a currency's symbol — see
* `measureTickFormatter`), else the compact default (1,200,000 → `1.2M`) —
* each printed to the decimal place the axis' tick step needs, so two ticks
* never share a label (objectstack#36988; see `valueTickExponent`). The step
* is measured over the values plotted in the slot, with the tick count
* `yAxisSpecProps` hands recharts for it. Data labels print with the same
* formatter (`valueTickFormatterOf`), so they carry the measure's units too.
*/
const valueAxisTickFormatter = React.useCallback(
(axis: NormalizedAxis | undefined, side: ValueAxisSlot) => {
const plotted = slotSeries(side);
const values = axisValues(side);
const tickCount = plotsWholeNumbers(slotSeries(side)) ? wholeNumberTickCount(axis, values) : undefined;
const tickCount = plotsWholeNumbers(plotted) ? wholeNumberTickCount(axis, values) : undefined;
const exponent = valueTickExponent(axis, values, tickCount);
return formatterFor(axis?.format, displayLocale, exponent) ?? compactTickFormatter(displayLocale, exponent);
return formatterFor(axis?.format, displayLocale, exponent)
?? measureTickFormatter(sharedMeasureFormat(plotted), displayLocale, exponent)
?? compactTickFormatter(displayLocale, exponent);
},
[axisValues, slotSeries, displayLocale],
);
Expand Down
10 changes: 7 additions & 3 deletions ui/packages/plugin-charts/src/ChartRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Skeleton } from '@object-ui/components';
import type { ChartContainerConfig } from './ChartContainerImpl';
import type { ChartSegmentClickEvent } from '@object-ui/core';
import { useObjectTranslation } from '@object-ui/i18n';
import { normalizeChartSchema } from './normalizeChartSchema';
import { normalizeChartSchema, type MeasureValueFormat } from './normalizeChartSchema';

// 🚀 Lazy load the implementation files
const LazyChart = React.lazy(() => import('./ChartImpl'));
Expand Down Expand Up @@ -76,9 +76,13 @@ export interface ChartRendererProps {
* the measure's aggregate, carried by `buildChartSeries` from the dataset
* answer, so a value axis over counts draws whole-number ticks. The
* authoring face refuses it by name (`ChartDataSeriesSchema.aggregate`).
* `measureFormat` is the same kind of carrier (objectstack#37053): the
* measure's `format` / `currency` / `percentScale`, filled by a
* dataset-bound producer so the value axis prints the measure's units —
* see `NormalizedSeries.measureFormat`.
*/
series?: Array<
| { dataKey: string; label?: string; type?: string; variant?: 'current' | 'comparison'; opacity?: number; dashArray?: string; chartType?: 'bar' | 'line' | 'area'; stack?: string; yAxis?: 'left' | 'right'; color?: string; aggregate?: string }
| { dataKey: string; label?: string; type?: string; variant?: 'current' | 'comparison'; opacity?: number; dashArray?: string; chartType?: 'bar' | 'line' | 'area'; stack?: string; yAxis?: 'left' | 'right'; color?: string; aggregate?: string; measureFormat?: MeasureValueFormat }
| { name: string; label?: unknown; type?: string; variant?: 'current' | 'comparison' | 'primary'; opacity?: number; dashArray?: string; stack?: string; yAxis?: 'left' | 'right'; color?: string }
>;
/** Spec `ChartConfig` shape — honored via `normalizeChartSchema`
Expand Down Expand Up @@ -162,7 +166,7 @@ export const ChartRenderer: React.FC<ChartRendererProps> = ({ schema, onChartCli
// `normalizeSeries` is a no-op on a well-formed internal-shaped entry apart
// from `type`: it round-trips every other key the internal arm of
// `ChartRendererProps.series` declares (`dataKey`/`label`/`chartType`/
// `variant`/`opacity`/`dashArray`/`stack`/`yAxis`/`color`/`aggregate`) unchanged, and
// `variant`/`opacity`/`dashArray`/`stack`/`yAxis`/`color`/`aggregate`/`measureFormat`) unchanged, and
// translates `type` to `chartType` — the translation this routing exists
// for. So always taking the normalized array
// is not a second read site for `type` (AGENTS.md #0.1) — it is routing
Expand Down
Loading
Loading