@milktop/react-ui 0.3.17 → 0.3.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,5 +15,6 @@ export type ChartProps = Omit<HTMLAttributes<HTMLDivElement>, 'children'> & {
15
15
  };
16
16
  export type AreaChartProps = ChartProps & {
17
17
  curve?: 'smooth' | 'linear';
18
+ zero?: boolean;
18
19
  };
19
- export declare function AreaChart({ data, x, y, series, height, format, label: labelProp, grid, curve, stacked, classes, ...rest }: AreaChartProps): import("react").JSX.Element;
20
+ export declare function AreaChart({ data, x, y, series, height, format, label: labelProp, grid, curve, stacked, zero, classes, ...rest }: AreaChartProps): import("react").JSX.Element;
@@ -9,6 +9,8 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
9
9
  * - `y`: the value property, or `series` for several, each `{ key, label, color }`
10
10
  * (colours from the theme's --ui-chart-1…5 by default)
11
11
  * - `curve`: 'smooth' (default) or 'linear'; `stacked`: series sit on top of one another
12
+ * - `zero`: the value axis starts at 0 (default); false fits it to the values, so a small change
13
+ * on a large value shows (a weight); stacked charts always start at 0
12
14
  * - `height` (240 px), `format(value)` for ticks and tooltips, `grid` (lines; true)
13
15
  * - `label`: what it shows, for assistive tech (a hidden table of the data)
14
16
  */
@@ -17,9 +19,9 @@ import { useId, useState } from 'react';
17
19
  import { scalePoint } from 'd3-scale';
18
20
  import { area, line, curveMonotoneX, curveLinear } from 'd3-shape';
19
21
  import { ChartFrame } from './ChartFrame.js';
20
- import { seriesOf, formatter, pad, yScale, leftFor, labelStep, num } from './chart.js';
22
+ import { seriesOf, formatter, pad, yScale, fitScale, leftFor, labelStep, num } from './chart.js';
21
23
  import { partsOf } from '../utils.js';
22
- export function AreaChart({ data = [], x = 'label', y = null, series = null, height = 240, format = null, label: labelProp, grid = true, curve = 'smooth', stacked = false, classes, ...rest }) {
24
+ export function AreaChart({ data = [], x = 'label', y = null, series = null, height = 240, format = null, label: labelProp, grid = true, curve = 'smooth', stacked = false, zero = true, classes, ...rest }) {
23
25
  const t = useMessages();
24
26
  const label = labelProp ?? t.chart.label;
25
27
  const part = partsOf('chart', classes);
@@ -40,14 +42,17 @@ export function AreaChart({ data = [], x = 'label', y = null, series = null, hei
40
42
  base[i] = low + value;
41
43
  return [low, low + value];
42
44
  }));
43
- const ys = yScale(Math.max(0, ...bands.flatMap((b) => b.map((v) => v[1]))), innerH);
45
+ const highs = bands.flatMap((b) => b.map((v) => v[1]));
46
+ const fitted = !zero && !stacked && highs.length > 0;
47
+ const ys = fitted ? fitScale(Math.min(...highs), Math.max(...highs), innerH) : yScale(Math.max(0, ...highs), innerH);
44
48
  const ticks = ys.ticks(4);
45
49
  const left = leftFor(ticks, fmt);
46
50
  const innerW = Math.max(0, width - left - pad.right);
47
51
  const xs = scalePoint().domain(rows.map((_, i) => i)).range([0, innerW]);
48
52
  const at = (i) => xs(i) ?? 0;
49
53
  const shape = curve === 'linear' ? curveLinear : curveMonotoneX;
50
- const fill = area().x((_, i) => at(i)).y0((d) => ys(d[0])).y1((d) => ys(d[1])).curve(shape);
54
+ // Fitted, the fill runs down to the axis' foot rather than to 0, which is off the chart
55
+ const fill = area().x((_, i) => at(i)).y0((d) => (fitted ? innerH : ys(d[0]))).y1((d) => ys(d[1])).curve(shape);
51
56
  const stroke = line().x((_, i) => at(i)).y((d) => ys(d[1])).curve(shape);
52
57
  const step = labelStep(rows.length, innerW);
53
58
  function move(e) {
@@ -18,6 +18,7 @@ export declare const pad: {
18
18
  right: number;
19
19
  };
20
20
  export declare const yScale: (max: number, innerH: number) => import("d3-scale").ScaleLinear<number, number, never>;
21
+ export declare const fitScale: (min: number, max: number, innerH: number) => import("d3-scale").ScaleLinear<number, number, never>;
21
22
  export declare const leftFor: (ticks: number[], fmt: (value: unknown) => string) => number;
22
23
  export declare const labelStep: (count: number, innerW: number) => number;
23
24
  export declare const num: (value: unknown) => number;
@@ -12,6 +12,13 @@ export const formatter = (format, locale) => (value) => format ? format(value) :
12
12
  // The plot area inside the axes.
13
13
  export const pad = { top: 8, bottom: 24, right: 8 };
14
14
  export const yScale = (max, innerH) => scaleLinear().domain([0, max || 1]).nice().range([innerH, 0]);
15
+ // Fitted to the values, with a little room either side, for small changes on a large value (a weight).
16
+ // Never below 0 when nothing is; a flat line sits mid-height.
17
+ export const fitScale = (min, max, innerH) => {
18
+ const room = (max - min) * 0.1 || Math.abs(max) * 0.1 || 1;
19
+ const low = min >= 0 ? Math.max(0, min - room) : min - room;
20
+ return scaleLinear().domain([low, max + room]).nice().range([innerH, 0]);
21
+ };
15
22
  // Room for the longest tick label.
16
23
  export const leftFor = (ticks, fmt) => Math.max(...ticks.map((tick) => fmt(tick).length), 1) * 7 + 12;
17
24
  // Every nth x label, so they don't collide.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milktop/react-ui",
3
- "version": "0.3.17",
3
+ "version": "0.3.18",
4
4
  "description": "React components on Ark UI (Zag state machines), with imba-ui's API and design",
5
5
  "license": "MIT",
6
6
  "author": "André Goldstein",