@pushwoosh/kit-charts 0.0.6 → 0.0.7

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.
@@ -94,8 +94,15 @@ never falls below 1.
94
94
 
95
95
  `labels.allowOverlap` defaults to false on both axes and on data labels. A label
96
96
  whose box would run into one already placed loses its text — Highcharts hides
97
- the label and keeps the tick, and so do we. A data-label formatter returning an
98
- element cannot be measured, so it is always drawn.
97
+ the label and keeps the tick, and so do we.
98
+
99
+ An axis or data-label `formatter` may return a `ReactNode` and not just text; the
100
+ axis draws the node and measures the text inside it, so an edge label still turns
101
+ inwards and the y gutter still widens. A node the walk cannot read text out of —
102
+ a bare shape, a component — measures as a point: it is always drawn, and a text
103
+ label too close to it is dropped as it would be beside any other label. Inside an
104
+ axis label the node lands in an SVG `<text>`, so `<tspan>` and strings render and
105
+ an HTML element does not.
99
106
 
100
107
  ## Plot bands and lines
101
108
 
@@ -14,6 +14,13 @@ const ANCHOR = {
14
14
  center: 'middle',
15
15
  right: 'end'
16
16
  };
17
+ /** What a tick draws: its own node if a formatter made one, its text otherwise. */
18
+ const tickContent = tick => {
19
+ if (tick.node !== undefined && tick.node !== null) {
20
+ return tick.node;
21
+ }
22
+ return tick.label === '' ? null : tick.label;
23
+ };
17
24
  /** A tick label, wrapped in its explanation when the axis gave it one. */
18
25
  function TickLabel({
19
26
  hint,
@@ -125,23 +132,26 @@ export function XAxis({
125
132
  y2: crisp(baseline, options.lineWidth ?? 1),
126
133
  stroke: lineColor,
127
134
  strokeWidth: options.lineWidth ?? 1
128
- }), axis.ticks.map(tick => _jsxs("g", {
129
- children: [tickLength > 0 && tickWidth > 0 ? _jsx("line", {
130
- x1: crisp(tick.position, tickWidth),
131
- x2: crisp(tick.position, tickWidth),
132
- y1: crisp(baseline, tickWidth),
133
- y2: crisp(baseline + direction * tickLength, tickWidth),
134
- stroke: options.tickColor ?? lineColor,
135
- strokeWidth: tickWidth
136
- }) : null, labelsEnabled && tick.label ? _jsx(TickLabel, {
137
- hint: tick.hint,
138
- x: tick.position + (options.labels?.x ?? 0),
139
- y: labelY,
140
- anchor: tick.anchor,
141
- style: options.labels?.style,
142
- children: tick.label
143
- }) : null]
144
- }, `x-${tick.value}`)), _jsx(AxisTitle, {
135
+ }), axis.ticks.map(tick => {
136
+ const content = tickContent(tick);
137
+ return _jsxs("g", {
138
+ children: [tickLength > 0 && tickWidth > 0 ? _jsx("line", {
139
+ x1: crisp(tick.position, tickWidth),
140
+ x2: crisp(tick.position, tickWidth),
141
+ y1: crisp(baseline, tickWidth),
142
+ y2: crisp(baseline + direction * tickLength, tickWidth),
143
+ stroke: options.tickColor ?? lineColor,
144
+ strokeWidth: tickWidth
145
+ }) : null, labelsEnabled && content !== null ? _jsx(TickLabel, {
146
+ hint: tick.hint,
147
+ x: tick.position + (options.labels?.x ?? 0),
148
+ y: labelY,
149
+ anchor: tick.anchor,
150
+ style: options.labels?.style,
151
+ children: content
152
+ }) : null]
153
+ }, `x-${tick.value}`);
154
+ }), _jsx(AxisTitle, {
145
155
  title: options.title,
146
156
  x: plot.x + plot.width / 2,
147
157
  y: labelY + direction * ((options.title?.offset ?? 0) + X_AXIS_TITLE_GAP),
@@ -173,14 +183,17 @@ export function YAxis({
173
183
  const titleShift = outwards ? Math.ceil(columnWidth * (anchor === 'middle' ? 0.5 : 1)) + AXIS_TITLE_GAP : 0;
174
184
  const titleX = onRight ? edge + titleShift + (options.title?.offset ?? 0) : edge - titleShift - (options.title?.offset ?? 0);
175
185
  return _jsxs("g", {
176
- children: [labelsEnabled ? axis.ticks.map(tick => tick.label ? _jsx(TickLabel, {
177
- x: edge + (options.labels?.x ?? 0),
178
- y: tick.position + TEXT_BASELINE_SHIFT,
179
- anchor: anchor,
180
- hint: tick.hint,
181
- style: options.labels?.style,
182
- children: tick.label
183
- }, `y-${tick.value}`) : null) : null, _jsx(AxisTitle, {
186
+ children: [labelsEnabled ? axis.ticks.map(tick => {
187
+ const content = tickContent(tick);
188
+ return content === null ? null : _jsx(TickLabel, {
189
+ x: edge + (options.labels?.x ?? 0),
190
+ y: tick.position + TEXT_BASELINE_SHIFT,
191
+ anchor: anchor,
192
+ hint: tick.hint,
193
+ style: options.labels?.style,
194
+ children: content
195
+ }, `y-${tick.value}`);
196
+ }) : null, _jsx(AxisTitle, {
184
197
  title: options.title,
185
198
  x: titleX,
186
199
  y: titleY(options, plot),
@@ -0,0 +1,3 @@
1
+ import { type ReactNode } from 'react';
2
+ /** The text a node prints, for the layout that has to measure it. */
3
+ export declare function nodeText(node: ReactNode): string;
@@ -0,0 +1,14 @@
1
+ import { isValidElement } from 'react';
2
+ /** The text a node prints, for the layout that has to measure it. */
3
+ export function nodeText(node) {
4
+ if (typeof node === 'string' || typeof node === 'number') {
5
+ return String(node);
6
+ }
7
+ if (Array.isArray(node)) {
8
+ return node.map(child => nodeText(child)).join('');
9
+ }
10
+ if (isValidElement(node)) {
11
+ return nodeText(node.props.children);
12
+ }
13
+ return '';
14
+ }
@@ -1,3 +1,4 @@
1
+ import { type ReactNode } from 'react';
1
2
  import { type NormalizedPoint } from '../data/points';
2
3
  import { type TitleEntry } from '../layout/title-layout';
3
4
  import { type Rect, type Size, type SlotGeometry, type Spacing } from '../types/geometry';
@@ -24,6 +25,8 @@ export interface AxisTick {
24
25
  value: number | string;
25
26
  position: number;
26
27
  label: string;
28
+ /** What the axis draws where a formatter returned something other than text. */
29
+ node?: ReactNode;
27
30
  anchor: TickAnchor;
28
31
  /** Sentence this tick explains itself with, opened from the label on hover. */
29
32
  hint?: string;
@@ -4,6 +4,7 @@ import { applyValueFormat, formatTime } from '../format/time';
4
4
  import { measureLegend } from '../layout/legend-layout';
5
5
  import { measureText, textHeight } from '../layout/measure-text';
6
6
  import { AXIS_GUTTER_PADDING, AXIS_LABEL_GAP, AXIS_TITLE_GAP, MARKER_REACH, VALUE_LABEL_HEADROOM, X_AXIS_LABEL_DROP } from '../layout/metrics';
7
+ import { nodeText } from '../layout/node-text';
7
8
  import { computeSlots } from '../layout/point-width';
8
9
  import { layoutTitles } from '../layout/title-layout';
9
10
  import { resolveAxes, resolveSeries, isSeriesVisible } from '../options/normalize';
@@ -17,6 +18,10 @@ export const hiddenKey = (seriesIndex, pointIndex) => pointIndex === undefined ?
17
18
  const DEFAULT_SPACING = [10, 10, 15, 10];
18
19
  const isBand = scale => scale.kind === 'band';
19
20
  const isContinuous = scale => scale.kind === 'linear' || scale.kind === 'time';
21
+ const asText = label => ({
22
+ label,
23
+ node: undefined
24
+ });
20
25
  function tickLabel(value, axis, timePattern, index, count, kind, numberFormat) {
21
26
  const formatter = axis.labels?.formatter;
22
27
  if (formatter) {
@@ -32,17 +37,20 @@ function tickLabel(value, axis, timePattern, index, count, kind, numberFormat) {
32
37
  }
33
38
  });
34
39
  if (typeof node === 'string' || typeof node === 'number') {
35
- return String(node);
40
+ return asText(String(node));
36
41
  }
37
- return '';
42
+ return {
43
+ label: nodeText(node),
44
+ node
45
+ };
38
46
  }
39
47
  if (axis.labels?.format) {
40
- return applyValueFormat(axis.labels.format, value);
48
+ return asText(applyValueFormat(axis.labels.format, value));
41
49
  }
42
50
  if (typeof value === 'number') {
43
- return timePattern ? formatTime(timePattern, value) : numberFormat(value);
51
+ return asText(timePattern ? formatTime(timePattern, value) : numberFormat(value));
44
52
  }
45
- return String(value);
53
+ return asText(String(value));
46
54
  }
47
55
  function collectCategories(axis, series, numeric = false) {
48
56
  if (axis.categories?.length) {
@@ -177,14 +185,17 @@ function dropOverlapping(ticks, axis, along) {
177
185
  // rather than order keeps one rule for both directions.
178
186
  let kept = null;
179
187
  return ticks.map(tick => {
180
- if (!tick.label) {
188
+ if (!tick.label && tick.node === undefined) {
181
189
  return tick;
182
190
  }
183
- const half = (along === 'x' ? measureText(tick.label, style) : textHeight(style)) / 2;
184
- if (kept && Math.abs(tick.position - kept.position) < half + kept.half + AXIS_LABEL_GAP) {
191
+ // A node with no text to read measures as a point rather than dropping out of
192
+ // the walk: it is always drawn, so a label after it still has to clear it.
193
+ const half = tick.label ? (along === 'x' ? measureText(tick.label, style) : textHeight(style)) / 2 : 0;
194
+ if (tick.label && kept && Math.abs(tick.position - kept.position) < half + kept.half + AXIS_LABEL_GAP) {
185
195
  return {
186
196
  ...tick,
187
- label: ''
197
+ label: '',
198
+ node: undefined
188
199
  };
189
200
  }
190
201
  kept = {
@@ -212,11 +223,15 @@ function axisTicks(axis, scale, hint, timePattern, along, numberFormat, bounds)
212
223
  const offset = isBand(scale) ? scale.bandwidth() / 2 : 0;
213
224
  const built = values.map((value, index) => {
214
225
  const position = (isBand(scale) ? scale.map(String(value)) : scale.map(Number(value))) + offset;
215
- const label = tickLabel(value, axis, timePattern, index, values.length, scale.kind, numberFormat);
226
+ const {
227
+ label,
228
+ node
229
+ } = tickLabel(value, axis, timePattern, index, values.length, scale.kind, numberFormat);
216
230
  return {
217
231
  value,
218
232
  position,
219
233
  label,
234
+ node,
220
235
  anchor: tickAnchor(label, position, axis, bounds),
221
236
  hint: axis.labels?.hint?.({
222
237
  value,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushwoosh/kit-charts",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Small React chart library on SVG, with a Highcharts-shaped configuration model",
5
5
  "license": "MIT",
6
6
  "repository": {