@adecore/charts 0.53.2 → 0.54.0

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.
package/src/Root.tsx CHANGED
@@ -1,3 +1,6 @@
1
+ import { useContentSize, useOverlayScrollbars } from '@adecore/ui';
2
+ import { formatNumber, formatRounded } from '@adecore/ui/format';
3
+ import clsx from 'clsx';
1
4
  import {
2
5
  useContext,
3
6
  useEffect,
@@ -12,10 +15,7 @@ import {
12
15
  type ReactNode,
13
16
  type Ref
14
17
  } from 'react';
15
- import clsx from 'clsx';
16
18
  import { useTranslation } from 'react-i18next';
17
- import { useContentSize } from '@adecore/ui';
18
- import { formatNumber, formatRounded } from '@adecore/ui/format';
19
19
  import { readValue, warnMissingKey, warnOnce, xOf } from './access.ts';
20
20
  import { LABEL_GAP as X_AXIS_GAP } from './axes.tsx';
21
21
  import { bandLayout, GROUP_GAP } from './band.ts';
@@ -194,6 +194,7 @@ export function ChartRoot<Row>({
194
194
  // The row of group labels under the plot takes its share of the height a growing chart is given.
195
195
  const boxHeight = grow ? Math.max(leastHeight, size.height - groupsSize.height) : leastHeight;
196
196
  const scroller = useRef<HTMLDivElement>(null);
197
+ const [scrollBox, setScrollBox] = useState<HTMLDivElement | null>(null);
197
198
 
198
199
  const points = rows === 'points';
199
200
  const band = xScale === 'band' && !points;
@@ -263,6 +264,7 @@ export function ChartRoot<Row>({
263
264
  // A scrolling plot clips what hangs out of its first buckets, such as a value label or a slanted name, so the
264
265
  // scroller reaches over the y axis by its width and gives that back as padding: the plot stays where it was.
265
266
  const overhang = scrolls ? leftAxis : 0;
267
+ const scrollbars = useOverlayScrollbars(scrolls ? scrollBox : null, { axis: 'x' });
266
268
  const xRows = [...xAxes.values()];
267
269
  const valueRoom = horizontal ? Math.max(0, ...[...series.values()].map((entry) => entry.endRoom ?? 0)) : 0;
268
270
  const titleRoom = leftY?.top ?? rightY?.top ?? 0;
@@ -790,100 +792,108 @@ export function ChartRoot<Row>({
790
792
  <g ref={setLeftSlot} tabIndex={-1} />
791
793
  </svg>
792
794
  )}
793
- <div
794
- ref={scroller}
795
- className={clsx('min-w-0 flex-1', scrolls ? 'overflow-x-auto overflow-y-hidden overscroll-x-contain' : 'overflow-visible')}
796
- style={
797
- overhang > 0
798
- ? {
799
- marginLeft: -overhang,
800
- paddingLeft: overhang,
801
- // Scrolled, the columns would pass over the y axis labels; only the start may hang there.
802
- clipPath: scroll > 0 ? `inset(0 0 0 ${overhang}px)` : undefined
803
- }
804
- : undefined
805
- }
806
- onScroll={(e) => setScroll(e.currentTarget.scrollLeft)}
807
- >
808
- <svg
809
- width={width}
810
- height={height}
811
- role={interactive ? 'group' : 'img'}
812
- tabIndex={interactive ? 0 : undefined}
813
- aria-roledescription={interactive ? t('chart') : undefined}
814
- aria-label={label}
815
- aria-describedby={interactive ? `${id}-hint` : undefined}
816
- aria-activedescendant={pressable && from >= 0 ? `${id}-${from}` : undefined}
817
- className={clsx('block overflow-visible outline-none', pressable && 'cursor-pointer')}
818
- {...(horizontal ? {} : pointer)}
819
- onKeyDown={interactive ? onKeyDown : undefined}
820
- onBlur={() => {
821
- setAnnounce(false);
822
- setKeyX(null);
823
-
824
- // A bucket the keyboard left, or any point, keeps no preview open where the focus went on.
825
- if (activeIndex >= 0 && (points || announce)) {
826
- setActiveX(null);
827
- }
795
+ {/* Holds the bar of a plot that scrolls sideways, under the plot alone and not the axes beside it. */}
796
+ <div className="relative flex min-w-0 flex-1">
797
+ <div
798
+ ref={(node) => {
799
+ scroller.current = node;
800
+ setScrollBox(node);
828
801
  }}
802
+ className={clsx('min-w-0 flex-1', scrolls ? 'overflow-x-auto overflow-y-hidden overscroll-x-contain' : 'overflow-visible')}
803
+ style={
804
+ overhang > 0
805
+ ? {
806
+ marginLeft: -overhang,
807
+ paddingLeft: overhang,
808
+ // Scrolled, the columns would pass over the y axis labels; only the start may hang there.
809
+ clipPath: scroll > 0 ? `inset(0 0 0 ${overhang}px)` : undefined
810
+ }
811
+ : undefined
812
+ }
813
+ onScroll={(e) => setScroll(e.currentTarget.scrollLeft)}
829
814
  >
830
- {layout !== null && highlighted.map((index) => <rect key={index} {...bucketBox(index, false)} className="fill-surface-hover" />)}
831
- {children}
832
- <g ref={setRulesSlot} tabIndex={-1} />
833
- {continuous !== null && !points && activeIndex >= 0 && cursor !== 'none' && (
834
- <Crosshair context={context} dots={cursor === 'full'} />
835
- )}
836
- {points && <Guides context={context} />}
837
- {pressable &&
838
- points &&
839
- positions.map((position, index) =>
840
- position === null ? null : (
841
- <circle
842
- key={keys![index]}
815
+ <svg
816
+ width={width}
817
+ height={height}
818
+ role={interactive ? 'group' : 'img'}
819
+ tabIndex={interactive ? 0 : undefined}
820
+ aria-roledescription={interactive ? t('chart') : undefined}
821
+ aria-label={label}
822
+ aria-describedby={interactive ? `${id}-hint` : undefined}
823
+ aria-activedescendant={pressable && from >= 0 ? `${id}-${from}` : undefined}
824
+ className={clsx('block overflow-visible outline-none', pressable && 'cursor-pointer')}
825
+ {...(horizontal ? {} : pointer)}
826
+ onKeyDown={interactive ? onKeyDown : undefined}
827
+ onBlur={() => {
828
+ setAnnounce(false);
829
+ setKeyX(null);
830
+
831
+ // A bucket the keyboard left, or any point, keeps no preview open where the focus went on.
832
+ if (activeIndex >= 0 && (points || announce)) {
833
+ setActiveX(null);
834
+ }
835
+ }}
836
+ >
837
+ {layout !== null &&
838
+ highlighted.map((index) => <rect key={index} {...bucketBox(index, false)} className="fill-surface-hover" />)}
839
+ {children}
840
+ <g ref={setRulesSlot} tabIndex={-1} />
841
+ {continuous !== null && !points && activeIndex >= 0 && cursor !== 'none' && (
842
+ <Crosshair context={context} dots={cursor === 'full'} />
843
+ )}
844
+ {points && <Guides context={context} />}
845
+ {pressable &&
846
+ points &&
847
+ positions.map((position, index) =>
848
+ position === null ? null : (
849
+ <circle
850
+ key={keys![index]}
851
+ id={`${id}-${index}`}
852
+ role="button"
853
+ aria-pressed={selectedRows[index]}
854
+ aria-label={describe(index)}
855
+ cx={position[0]}
856
+ cy={position[1]}
857
+ r={10}
858
+ fill="transparent"
859
+ />
860
+ )
861
+ )}
862
+ {pressable &&
863
+ layout !== null &&
864
+ xs.map((key, index) => (
865
+ <rect
866
+ key={String(key)}
843
867
  id={`${id}-${index}`}
844
868
  role="button"
845
869
  aria-pressed={selectedRows[index]}
846
870
  aria-label={describe(index)}
847
- cx={position[0]}
848
- cy={position[1]}
849
- r={10}
871
+ {...bucketBox(index, true)}
850
872
  fill="transparent"
851
873
  />
852
- )
874
+ ))}
875
+ {xs.length === 0 && (
876
+ <text
877
+ x={Math.round((plot.left + plot.right) / 2)}
878
+ y={Math.round((plot.top + plot.bottom) / 2)}
879
+ dy="0.32em"
880
+ textAnchor="middle"
881
+ className="fill-text-faint text-xs"
882
+ >
883
+ {t('noData')}
884
+ </text>
853
885
  )}
854
- {pressable &&
855
- layout !== null &&
856
- xs.map((key, index) => (
857
- <rect
858
- key={String(key)}
859
- id={`${id}-${index}`}
860
- role="button"
861
- aria-pressed={selectedRows[index]}
862
- aria-label={describe(index)}
863
- {...bucketBox(index, true)}
864
- fill="transparent"
865
- />
866
- ))}
867
- {xs.length === 0 && (
868
- <text
869
- x={Math.round((plot.left + plot.right) / 2)}
870
- y={Math.round((plot.top + plot.bottom) / 2)}
871
- dy="0.32em"
872
- textAnchor="middle"
873
- className="fill-text-faint text-xs"
874
- >
875
- {t('noData')}
876
- </text>
877
- )}
878
- </svg>
879
- <div
880
- ref={(node) => {
881
- setGroupsSlot(node);
882
- measureGroups(node);
883
- }}
884
- className="empty:hidden"
885
- style={{ width }}
886
- />
886
+ </svg>
887
+ <div
888
+ ref={(node) => {
889
+ setGroupsSlot(node);
890
+ measureGroups(node);
891
+ }}
892
+ className="empty:hidden"
893
+ style={{ width }}
894
+ />
895
+ </div>
896
+ {scrollbars}
887
897
  </div>
888
898
  <svg width={rightAxis} height={height} aria-hidden className="shrink-0 overflow-visible">
889
899
  <g ref={setRightSlot} tabIndex={-1} />
@@ -1,8 +1,8 @@
1
- import { useId, useState, type ReactNode, type Ref } from 'react';
1
+ import { Button, Input, Tooltip } from '@adecore/ui';
2
2
  import clsx from 'clsx';
3
3
  import { Check, Search } from 'lucide-react';
4
+ import { useId, useState, type ReactNode, type Ref } from 'react';
4
5
  import { useTranslation } from 'react-i18next';
5
- import { Button, Input, Tooltip } from '@adecore/ui';
6
6
  import { matchingItems, toggleGroup } from './legend.ts';
7
7
 
8
8
  export interface SeriesLegendItem {
package/src/Sparkline.tsx CHANGED
@@ -1,9 +1,9 @@
1
- import type { Ref } from 'react';
1
+ import { Tooltip } from '@adecore/ui';
2
+ import { formatNumber } from '@adecore/ui/format';
2
3
  import clsx from 'clsx';
3
4
  import { line } from 'd3-shape';
5
+ import type { Ref } from 'react';
4
6
  import { useTranslation } from 'react-i18next';
5
- import { Tooltip } from '@adecore/ui';
6
- import { formatNumber } from '@adecore/ui/format';
7
7
  import { colorOf } from './color.ts';
8
8
  import { sparkColumns } from './spark.ts';
9
9
  import type { ChartColor } from './types.ts';
@@ -0,0 +1,19 @@
1
+ import { ScrollArea } from '@adecore/ui';
2
+ import type { ReactNode } from 'react';
3
+
4
+ /* The box around a table of values: a frame that scrolls while it is shown, out of sight but still read while it is not. */
5
+ export function ValuesBox({ id, shown, children }: { id: string; shown: boolean; children: ReactNode }) {
6
+ if (!shown) {
7
+ return (
8
+ <div id={id} className="sr-only">
9
+ {children}
10
+ </div>
11
+ );
12
+ }
13
+
14
+ return (
15
+ <ScrollArea axis="both" id={id} className="max-h-80 w-full rounded-md border border-border-soft">
16
+ {children}
17
+ </ScrollArea>
18
+ );
19
+ }
@@ -1,6 +1,6 @@
1
+ import clsx from 'clsx';
1
2
  import { useId, type Ref } from 'react';
2
3
  import { createPortal } from 'react-dom';
3
- import clsx from 'clsx';
4
4
  import { xOf } from './access.ts';
5
5
  import { colorOf } from './color.ts';
6
6
  import { useChartContext } from './context.ts';
package/src/axes.tsx CHANGED
@@ -1,11 +1,11 @@
1
+ import { formatRounded } from '@adecore/ui/format';
2
+ import clsx from 'clsx';
1
3
  import { useEffect, useId, useRef, useState, useSyncExternalStore, type ReactNode, type Ref } from 'react';
2
4
  import { createPortal } from 'react-dom';
3
- import clsx from 'clsx';
4
5
  import { useTranslation } from 'react-i18next';
5
- import { formatRounded } from '@adecore/ui/format';
6
6
  import { useChartContext } from './context.ts';
7
- import { decimalsOf, tickCount } from './scale.ts';
8
7
  import { useBeforePaint } from './register.ts';
8
+ import { decimalsOf, tickCount } from './scale.ts';
9
9
  import { formatTime, timeLabelOf } from './time-format.ts';
10
10
  import type { ChartX } from './types.ts';
11
11
 
package/src/overlays.tsx CHANGED
@@ -1,12 +1,13 @@
1
+ import { Button, Icon, Surface, useContentSize } from '@adecore/ui';
2
+ import clsx from 'clsx';
1
3
  import { useId, useState, type CSSProperties, type ReactNode, type Ref } from 'react';
2
4
  import { createPortal } from 'react-dom';
3
- import clsx from 'clsx';
4
5
  import { useTranslation } from 'react-i18next';
5
- import { Button, Icon, Surface, useContentSize } from '@adecore/ui';
6
6
  import { useChartContext, type ChartContextValue } from './context.ts';
7
7
  import type { SeriesEntry } from './store.ts';
8
8
  import type { ChartNote, ChartX } from './types.ts';
9
9
  import { tableClasses } from './values-table.ts';
10
+ import { ValuesBox } from './ValuesBox.tsx';
10
11
 
11
12
  export interface ChartBucketValue {
12
13
  readonly label: string;
@@ -363,7 +364,7 @@ export function ChartTable({ toggle = true, open, defaultOpen = false, onOpenCha
363
364
  {shown ? t('values.hide') : t('values.show')}
364
365
  </Button>
365
366
  )}
366
- <div id={id} className={classes.box}>
367
+ <ValuesBox id={id} shown={shown}>
367
368
  <table className={classes.table}>
368
369
  <caption className="sr-only">{t('values.caption', { label: chart.label })}</caption>
369
370
  <thead className={classes.head}>
@@ -393,7 +394,7 @@ export function ChartTable({ toggle = true, open, defaultOpen = false, onOpenCha
393
394
  ))}
394
395
  </tbody>
395
396
  </table>
396
- </div>
397
+ </ValuesBox>
397
398
  </div>,
398
399
  chart.elements.table
399
400
  );
package/src/scale.ts CHANGED
@@ -87,7 +87,7 @@ export interface YAxisScale {
87
87
  }
88
88
 
89
89
  /* Ticks every `step` from `low` to `high`, both rounded out to it. */
90
- function steppedScale(low: number, high: number, step: number, options: ChartYOptions, range: [number, number]): YAxisScale {
90
+ function steppedScale(low: number, high: number, step: number, options: ChartYOptions, range: [number, number], count: number): YAxisScale | null {
91
91
  const start = options.min ?? Math.floor(low / step) * step;
92
92
  let end = options.max ?? Math.ceil(high / step) * step;
93
93
 
@@ -95,11 +95,24 @@ function steppedScale(low: number, high: number, step: number, options: ChartYOp
95
95
  end = start + step;
96
96
  }
97
97
 
98
+ const first = Math.ceil(start / step) * step;
99
+ const intervals = Math.floor((end - first) / step + 1e-6);
100
+
101
+ if (!Number.isFinite(first) || !Number.isSafeInteger(intervals) || first + step === first) {
102
+ return null;
103
+ }
104
+
105
+ const limit = Math.max(2, Math.min(1024, Number.isFinite(count) ? Math.ceil(count) * 4 : 20));
106
+ const stride = Math.max(1, Math.ceil(intervals / (limit - 1)));
98
107
  const scale = scaleLinear().domain([start, end]).range(range);
99
108
  const ticks: number[] = [];
100
109
 
101
- for (let tick = Math.ceil(start / step) * step; tick <= end + step / 1e6; tick += step) {
102
- ticks.push(Number(tick.toFixed(10)));
110
+ for (let i = 0; i <= intervals; i += stride) {
111
+ const tick = Number((first + i * step).toFixed(10));
112
+
113
+ if (Number.isFinite(tick) && (ticks.length === 0 || tick > ticks[ticks.length - 1]!)) {
114
+ ticks.push(tick);
115
+ }
103
116
  }
104
117
 
105
118
  return { domain: [start, end], ticks, at: (value) => scale(value) };
@@ -143,8 +156,12 @@ export function yScaleOf(
143
156
  const floor = ground ? 0 : (extent?.[0] ?? 0);
144
157
  const ceiling = ground ? 0 : (extent?.[1] ?? 0);
145
158
 
146
- if (options.step !== undefined && options.step > 0) {
147
- return steppedScale(Math.min(floor, extent?.[0] ?? 0), Math.max(ceiling, extent?.[1] ?? 0), options.step, options, range);
159
+ if (options.step !== undefined && Number.isFinite(options.step) && options.step > 0) {
160
+ const stepped = steppedScale(Math.min(floor, extent?.[0] ?? 0), Math.max(ceiling, extent?.[1] ?? 0), options.step, options, range, count);
161
+
162
+ if (stepped !== null) {
163
+ return stepped;
164
+ }
148
165
  }
149
166
 
150
167
  let low = options.min ?? Math.min(floor, extent?.[0] ?? 0);
@@ -173,7 +190,7 @@ export function yScaleOf(
173
190
 
174
191
  const domain: [number, number] = [options.min ?? low, options.max ?? high];
175
192
  scale.domain(domain);
176
- let ticks = scale.ticks(count);
193
+ let ticks = [...new Set(scale.ticks(count))];
177
194
 
178
195
  if (options.integer === true) {
179
196
  ticks = ticks.filter((tick) => Number.isInteger(tick));
package/src/scatter.tsx CHANGED
@@ -1,5 +1,5 @@
1
- import type { ReactNode, Ref } from 'react';
2
1
  import clsx from 'clsx';
2
+ import type { ReactNode, Ref } from 'react';
3
3
  import { colorOf } from './color.ts';
4
4
  import { useChartContext } from './context.ts';
5
5
  import { frontierOf, spreadLabels } from './points.ts';
package/src/series.tsx CHANGED
@@ -1,7 +1,7 @@
1
- import { useContext, useEffect, useId, useRef, useState, type ReactNode, type Ref } from 'react';
2
- import clsx from 'clsx';
3
1
  import type { IconProps } from '@adecore/ui';
2
+ import clsx from 'clsx';
4
3
  import { area, curveLinear, curveMonotoneX, curveStepAfter, line } from 'd3-shape';
4
+ import { useContext, useEffect, useId, useRef, useState, type ReactNode, type Ref } from 'react';
5
5
  import { numberOf, readValue, warnOnce } from './access.ts';
6
6
  import { columnsIn, endRoundedBar, endRoundedColumn, guessLabelWidth, LABEL_INSET, LABEL_LIFT, placeValueLabels, segmentOf } from './band.ts';
7
7
  import { colorOf, seriesColor } from './color.ts';
package/src/timeline.tsx CHANGED
@@ -1,8 +1,8 @@
1
- import { createContext, useContext, useId, useState, type KeyboardEvent, type ReactNode, type Ref } from 'react';
2
- import clsx from 'clsx';
3
- import { useTranslation } from 'react-i18next';
4
1
  import { Tooltip, Tree } from '@adecore/ui';
5
2
  import { formatNumber, formatPreciseDuration } from '@adecore/ui/format';
3
+ import clsx from 'clsx';
4
+ import { createContext, useContext, useId, useState, type KeyboardEvent, type ReactNode, type Ref } from 'react';
5
+ import { useTranslation } from 'react-i18next';
6
6
  import { colorOf, seriesColor } from './color.ts';
7
7
  import {
8
8
  barOf,
@@ -1,6 +1,5 @@
1
1
  /* A table of values as it is shown; hidden, it stays for a screen reader without any of these. */
2
2
  const TABLE_CLASSES = {
3
- box: 'max-h-80 w-full overflow-auto rounded-md border border-border-soft',
4
3
  table: 'w-full border-collapse text-xs tabular-nums',
5
4
  head: 'sticky top-0 bg-surface-hover',
6
5
  column: 'h-8 px-2 text-right font-medium whitespace-nowrap text-text-muted',
@@ -9,4 +8,4 @@ const TABLE_CLASSES = {
9
8
  cell: 'h-8 px-2 text-right whitespace-nowrap text-text'
10
9
  };
11
10
 
12
- export const tableClasses = (shown: boolean): Partial<typeof TABLE_CLASSES> & { box: string } => (shown ? TABLE_CLASSES : { box: 'sr-only' });
11
+ export const tableClasses = (shown: boolean): Partial<typeof TABLE_CLASSES> => (shown ? TABLE_CLASSES : {});