@pushwoosh/kit-charts 0.0.13 → 0.0.15

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.
@@ -99,13 +99,16 @@ filling the plot. An end pinned with `max` cannot be stretched, so a tick amount
99
99
  alongside one is ignored.
100
100
 
101
101
  An x axis measures `labels.y` from the axis line itself, as Highcharts does: a
102
- config that sets it replaces the default drop rather than adding to it. Left
103
- unset, the label falls the tick's length plus a fixed drop below the line.
102
+ config that sets it replaces the default drop rather than adding to it. Left unset,
103
+ the label falls `labels.distance` below the line.
104
104
 
105
105
  `minPadding`/`maxPadding` add a share of the data range beyond an end the caller
106
- left open — Highcharts' axis air, off unless asked for. `labels.padding` is the
107
- gap Highcharts leaves between the y label column and the plot; it defaults to 5
108
- and widens the gutter, so a migrated config keeps its left edge.
106
+ left open — Highcharts' axis air. There they default to 0.01 on x and 0.05 on y;
107
+ here they default to 0, because switching them on would move every chart already
108
+ drawn. `labels.distance` is Highcharts' one number for how far a label sits from the
109
+ plot perimeter (15), and both the gutter reserve and the drawn column read it, so
110
+ they cannot drift apart. `labels.x` on a y axis and `labels.y` on an x axis override
111
+ it, exactly as `pick(labels.x, distance)` does there.
109
112
 
110
113
  The x axis line is drawn after the series, so a bar or an area sitting on zero
111
114
  cannot paint over the line it stands on.
@@ -1,8 +1,8 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Hinted } from './Hinted';
3
3
  import { DEFAULT_FONT_SIZE, measureText } from '../layout/measure-text';
4
- import { AXIS_LABEL_GAP, AXIS_TITLE_GAP, crisp, TEXT_BASELINE_SHIFT, X_AXIS_TITLE_GAP } from '../layout/metrics';
5
- import { xLabelOffset } from '../model/build-model';
4
+ import { AXIS_TITLE_GAP, crisp, TEXT_BASELINE_SHIFT, X_AXIS_TITLE_GAP } from '../layout/metrics';
5
+ import { xLabelOffset, yLabelGap } from '../model/build-model';
6
6
  import { DEFAULT_AXIS_COLOR, DEFAULT_GRID_COLOR, DEFAULT_MUTED_TEXT_COLOR } from '../model/palette';
7
7
  const textProps = style => ({
8
8
  fill: style?.color ?? DEFAULT_MUTED_TEXT_COLOR,
@@ -140,7 +140,7 @@ export function XAxis({
140
140
  strokeWidth: tickWidth
141
141
  }) : null, labelsEnabled && content !== null ? _jsx(TickLabel, {
142
142
  hint: tick.hint,
143
- x: tick.position + (options.labels?.x ?? 0),
143
+ x: tick.position + tick.shift + (options.labels?.x ?? 0),
144
144
  y: labelY,
145
145
  anchor: tick.anchor,
146
146
  style: options.labels?.style,
@@ -192,7 +192,8 @@ export function YAxis({
192
192
  const onRight = options.opposite === true;
193
193
  const offset = options.offset ?? 0;
194
194
  // `offset` pushes the whole axis — labels and title — away from the plot.
195
- const edge = onRight ? plot.x + plot.width + AXIS_LABEL_GAP + offset : plot.x - AXIS_LABEL_GAP - offset;
195
+ const gap = yLabelGap(options);
196
+ const edge = onRight ? plot.x + plot.width + gap + offset : plot.x - gap - offset;
196
197
  const anchor = (options.labels?.align && ANCHOR[options.labels.align]) ?? (onRight ? 'start' : 'end');
197
198
  const columnWidth = labelsEnabled ? axis.ticks.reduce((widest, tick) => Math.max(widest, measureText(tick.label, options.labels?.style)), 0) : 0;
198
199
  // Where the labels grow away from the plot, the title has to clear the whole
@@ -204,7 +205,7 @@ export function YAxis({
204
205
  children: [labelsEnabled ? axis.ticks.map(tick => {
205
206
  const content = tickContent(tick);
206
207
  return content === null ? null : _jsx(TickLabel, {
207
- x: edge + (options.labels?.x ?? 0),
208
+ x: edge,
208
209
  y: tick.position + TEXT_BASELINE_SHIFT,
209
210
  anchor: anchor,
210
211
  hint: tick.hint,
@@ -8,12 +8,10 @@
8
8
  export declare const TEXT_BASELINE_SHIFT = 4;
9
9
  /** Tick label to the plot edge, on either axis. */
10
10
  export declare const AXIS_LABEL_GAP = 8;
11
+ /** Highcharts' `labels.distance`: label to the plot perimeter, on either axis. */
12
+ export declare const AXIS_LABEL_DISTANCE = 15;
11
13
  /** Baseline of an x axis label below the axis line, tick length aside. */
12
14
  export declare const X_AXIS_LABEL_DROP = 14;
13
- /** Air added to the tick label column when measuring the y axis gutter. */
14
- export declare const AXIS_GUTTER_PADDING = 10;
15
- /** `labels.padding` when the axis does not set it — the Highcharts default. */
16
- export declare const AXIS_LABEL_PADDING = 5;
17
15
  /** A rotated axis title to the column of tick labels it stands beside. */
18
16
  export declare const AXIS_TITLE_GAP = 6;
19
17
  /** An x axis title below its labels. */
@@ -8,12 +8,10 @@
8
8
  export const TEXT_BASELINE_SHIFT = 4;
9
9
  /** Tick label to the plot edge, on either axis. */
10
10
  export const AXIS_LABEL_GAP = 8;
11
+ /** Highcharts' `labels.distance`: label to the plot perimeter, on either axis. */
12
+ export const AXIS_LABEL_DISTANCE = 15;
11
13
  /** Baseline of an x axis label below the axis line, tick length aside. */
12
14
  export const X_AXIS_LABEL_DROP = 14;
13
- /** Air added to the tick label column when measuring the y axis gutter. */
14
- export const AXIS_GUTTER_PADDING = 10;
15
- /** `labels.padding` when the axis does not set it — the Highcharts default. */
16
- export const AXIS_LABEL_PADDING = 5;
17
15
  /** A rotated axis title to the column of tick labels it stands beside. */
18
16
  export const AXIS_TITLE_GAP = 6;
19
17
  /** An x axis title below its labels. */
@@ -28,6 +28,8 @@ export interface AxisTick {
28
28
  /** What the axis draws where a formatter returned something other than text. */
29
29
  node?: ReactNode;
30
30
  anchor: TickAnchor;
31
+ /** Slide along the axis that keeps an end label's box inside the canvas. */
32
+ shift: number;
31
33
  /** Sentence this tick explains itself with, opened from the label on hover. */
32
34
  hint?: string;
33
35
  }
@@ -60,6 +62,8 @@ declare const isBand: (scale: AnyScale) => scale is BandScale;
60
62
  declare const isContinuous: (scale: AnyScale) => scale is ContinuousScale;
61
63
  /** Axis line to the label baseline; `labels.y` replaces the drop, as in Highcharts. */
62
64
  export declare function xLabelOffset(axis: AxisOptions, onTop: boolean): number;
65
+ /** Plot edge to the y label column: `labels.x` when set, `labels.distance` otherwise. */
66
+ export declare const yLabelGap: (axis: AxisOptions) => number;
63
67
  export interface BuildModelInput {
64
68
  options: ChartOptions;
65
69
  size: Size;
@@ -3,7 +3,7 @@ import { normalizePoints } from '../data/points';
3
3
  import { applyValueFormat, formatTime } from '../format/time';
4
4
  import { measureLegend } from '../layout/legend-layout';
5
5
  import { measureText, textHeight } from '../layout/measure-text';
6
- import { AXIS_GUTTER_PADDING, AXIS_LABEL_GAP, AXIS_LABEL_PADDING, AXIS_TITLE_GAP, MARKER_REACH, VALUE_LABEL_HEADROOM, X_AXIS_LABEL_DROP } from '../layout/metrics';
6
+ import { AXIS_LABEL_GAP, AXIS_LABEL_DISTANCE, AXIS_TITLE_GAP, MARKER_REACH, VALUE_LABEL_HEADROOM } from '../layout/metrics';
7
7
  import { nodeText } from '../layout/node-text';
8
8
  import { computeSlots } from '../layout/point-width';
9
9
  import { layoutTitles } from '../layout/title-layout';
@@ -94,8 +94,10 @@ export function xLabelOffset(axis, onTop) {
94
94
  if (explicit !== undefined) {
95
95
  return onTop ? -explicit : explicit;
96
96
  }
97
- return (axis.tickLength ?? 0) + (onTop ? AXIS_LABEL_GAP : X_AXIS_LABEL_DROP);
97
+ return onTop ? -(axis.labels?.distance ?? AXIS_LABEL_DISTANCE) : axis.labels?.distance ?? AXIS_LABEL_DISTANCE;
98
98
  }
99
+ /** Plot edge to the y label column: `labels.x` when set, `labels.distance` otherwise. */
100
+ export const yLabelGap = axis => axis.labels?.x !== undefined ? Math.abs(axis.labels.x) : axis.labels?.distance ?? AXIS_LABEL_DISTANCE;
99
101
  /** Highcharts' air around a series: a share of the range beyond each open end. */
100
102
  function axisPadding(axis, domain) {
101
103
  const span = domain.max - domain.min;
@@ -185,22 +187,22 @@ const EXPLICIT_ANCHOR = {
185
187
  center: 'middle',
186
188
  right: 'end'
187
189
  };
188
- function tickAnchor(label, position, axis, bounds) {
189
- const explicit = axis.labels?.align && EXPLICIT_ANCHOR[axis.labels.align];
190
- if (explicit) {
191
- return explicit;
192
- }
193
- if (!bounds || !label) {
194
- return 'middle';
190
+ function tickAnchor(axis) {
191
+ return axis.labels?.align && EXPLICIT_ANCHOR[axis.labels.align] || 'middle';
192
+ }
193
+ /** Highcharts slides an end label until its box sits inside the canvas, keeping it centred. */
194
+ function tickShift(label, position, axis, bounds) {
195
+ if (!bounds || !label || axis.labels?.align) {
196
+ return 0;
195
197
  }
196
198
  const half = measureText(label, axis.labels?.style) / 2;
197
199
  if (position - half < bounds[0]) {
198
- return 'start';
200
+ return bounds[0] - (position - half);
199
201
  }
200
202
  if (position + half > bounds[1]) {
201
- return 'end';
203
+ return bounds[1] - (position + half);
202
204
  }
203
- return 'middle';
205
+ return 0;
204
206
  }
205
207
  /**
206
208
  * Blanks the label of any tick whose text would run into the last one kept.
@@ -263,7 +265,8 @@ function axisTicks(axis, scale, hint, timePattern, along, numberFormat, bounds)
263
265
  position,
264
266
  label,
265
267
  node,
266
- anchor: tickAnchor(label, position, axis, bounds),
268
+ anchor: tickAnchor(axis),
269
+ shift: along === 'x' ? tickShift(label, position, axis, bounds) : 0,
267
270
  hint: axis.labels?.hint?.({
268
271
  value,
269
272
  index,
@@ -513,7 +516,9 @@ export function buildChartModel({
513
516
  }), axis.tickAmount ?? 5, undefined, 'y', numberFormat).map(tick => tick.label);
514
517
  const widest = labels.reduce((widest_, label) => Math.max(widest_, measureText(label, axis.labels?.style)), 0);
515
518
  const title = axis.title?.text && (axis.title.rotation ?? -90) !== 0 ? textHeight(axis.title.style) + AXIS_TITLE_GAP : 0;
516
- return Math.ceil(widest) + AXIS_GUTTER_PADDING + (axis.labels?.padding ?? AXIS_LABEL_PADDING) + title;
519
+ // Highcharts reserves the widest label plus the distance it is drawn at; the two
520
+ // must be one number or the plot edge and the label column drift apart.
521
+ return Math.ceil(widest) + yLabelGap(axis) + title;
517
522
  };
518
523
  const gutter = axisless || yAxes[0]?.visible === false ? 0 : measureGutter(32);
519
524
  const legendAlign = options.legend?.align ?? 'left';
@@ -566,7 +571,10 @@ export function buildChartModel({
566
571
  id: 'x0',
567
572
  options: xAxisOptions,
568
573
  scale: xScale,
569
- ticks: axisTicks(xAxisOptions, xScale, xHint, timePattern, 'x', numberFormat, [plot.x, plot.x + plot.width])
574
+ ticks: axisTicks(xAxisOptions, xScale, xHint, timePattern, 'x', numberFormat,
575
+ // Highcharts justifies against the chart spacing box, not the plot: the
576
+ // gutter is room to hang over, the spacing is not.
577
+ [Math.min(plot.x, spaceLeft), Math.max(plot.x + plot.width, size.width - spaceRight)])
570
578
  },
571
579
  yAxes: yAxisModels,
572
580
  titles: titles.entries,
@@ -88,8 +88,8 @@ export interface AxisLabelsOptions {
88
88
  align?: 'left' | 'center' | 'right';
89
89
  x?: number;
90
90
  y?: number;
91
- /** Air between the label column and the plot, as Highcharts reserves it. */
92
- padding?: number;
91
+ /** Label to the plot perimeter, Highcharts' `labels.distance`; `x`/`y` override it. */
92
+ distance?: number;
93
93
  style?: TextStyle;
94
94
  }
95
95
  /** `zIndex` at or above this draws over the series instead of under it. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushwoosh/kit-charts",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "Small React chart library on SVG, with a Highcharts-shaped configuration model",
5
5
  "license": "MIT",
6
6
  "repository": {