@pushwoosh/kit-charts 0.0.14 → 0.0.16

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,17 +99,33 @@ 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.
112
115
 
116
+ A y axis with `labels.enabled: false` reserves no gutter at all — only a rotated
117
+ title still takes room — so the plot starts at `chart.spacing`, where Highcharts
118
+ starts it, rather than a label column in from there.
119
+
120
+ An end the axis leaves unpadded puts its extreme point on the plot edge, where
121
+ half of the point's marker would fall outside the SVG and be cut. Such an end
122
+ keeps `MARKER_REACH` of room: the plot edge moves in to 5px from the canvas
123
+ unless the gutter, the spacing or `chart.marginLeft`/`marginRight` already leaves
124
+ at least that much, so a chart with `spacing: 0` and no gutter is 5px in rather
125
+ than flush. Only an end that can draw a marker pays it: `marker.enabled: false`
126
+ does not release the room — the hover dot is drawn regardless, as in Highcharts —
127
+ but `marker.states.hover.enabled: false` does, and then no dot is drawn either.
128
+
113
129
  `labels.allowOverlap` defaults to false on both axes and on data labels. A label
114
130
  whose box would run into one already placed loses its text — Highcharts hides
115
131
  the label and keeps the tick, and so do we.
@@ -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,11 +3,11 @@ 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';
10
- import { resolveAxes, resolveSeries, isSeriesVisible } from '../options/normalize';
10
+ import { resolveAxes, resolveSeries, isSeriesVisible, showsHoverMarker, showsMarkers } from '../options/normalize';
11
11
  import { createBandScale } from '../scale/band';
12
12
  import { createColorScale } from '../scale/color';
13
13
  import { createLinearScale } from '../scale/linear';
@@ -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,
@@ -472,7 +475,7 @@ export function buildChartModel({
472
475
  const yLabelsShown = !axisless && yAxes[0]?.visible !== false && yAxes[0]?.labels?.enabled !== false;
473
476
  const markerReach = resolved.some(({
474
477
  options: series
475
- }) => series.marker?.enabled !== false) ? MARKER_REACH : 0;
478
+ }) => showsMarkers(series.marker)) ? MARKER_REACH : 0;
476
479
  // The topmost tick label is centred on the plot's top edge, so half of it hangs
477
480
  // above the plot: without this the highest grid line and its label sit on the
478
481
  // chart border.
@@ -496,10 +499,13 @@ export function buildChartModel({
496
499
  })();
497
500
  const marginTop = spaceTop + titleHeight + (legendAtTop ? legendHeight : 0) + (labelsOnTop ? xLabelHeight : 0) + Math.max(labelHeadroom, topOverflow);
498
501
  const marginBottom = spaceBottom + (labelsOnTop ? 0 : xLabelHeight) + (legendAtTop ? 0 : legendHeight);
499
- const measureGutter = estimate => {
502
+ const measureGutter = () => {
500
503
  const axis = yAxes[0] ?? {};
504
+ const title = axis.title?.text && (axis.title.rotation ?? -90) !== 0 ? textHeight(axis.title.style) + AXIS_TITLE_GAP : 0;
505
+ // Highcharts measures the label column and nothing else, so an axis without
506
+ // labels leaves the plot flush with the chart edge rather than indented.
501
507
  if (axis.labels?.enabled === false) {
502
- return estimate;
508
+ return title;
503
509
  }
504
510
  const labels = axis.categories?.length ? [...axis.categories] : axisTicks(axis, createLinearScale({
505
511
  domain: (() => {
@@ -512,15 +518,26 @@ export function buildChartModel({
512
518
  allowDecimals: axis.allowDecimals
513
519
  }), axis.tickAmount ?? 5, undefined, 'y', numberFormat).map(tick => tick.label);
514
520
  const widest = labels.reduce((widest_, label) => Math.max(widest_, measureText(label, axis.labels?.style)), 0);
515
- 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;
521
+ // Highcharts reserves the widest label plus the distance it is drawn at; the two
522
+ // must be one number or the plot edge and the label column drift apart.
523
+ return Math.ceil(widest) + yLabelGap(axis) + title;
517
524
  };
518
- const gutter = axisless || yAxes[0]?.visible === false ? 0 : measureGutter(32);
525
+ const gutter = axisless || yAxes[0]?.visible === false ? 0 : measureGutter();
519
526
  const legendAlign = options.legend?.align ?? 'left';
520
527
  const legendBesideLeft = legendMetrics.beside && legendAlign === 'left';
528
+ // A point on the plot edge draws half its marker outside the canvas, where the
529
+ // svg cuts it; an end the axis pads keeps that room already.
530
+ const edgeMarkers = !axisless && xKind !== 'band' && resolved.some(({
531
+ renderer,
532
+ options: series
533
+ }) => renderer.scales.x !== 'band' && (showsMarkers(series.marker) || showsHoverMarker(series.marker)));
534
+ const hoverReach = edgeMarkers ? MARKER_REACH : 0;
535
+ const domainMin = xAxisOptions.min ?? xDomain.min;
536
+ const domainMax = xAxisOptions.max ?? xDomain.max;
537
+ const edgeRoom = (margin, flush) => flush ? Math.max(margin, hoverReach) : margin;
521
538
  // Highcharts margins replace the computed edge distance rather than adding to it.
522
- const left = options.chart?.marginLeft ?? spaceLeft + gutter + (legendBesideLeft ? legendMetrics.width : 0);
523
- const right = options.chart?.marginRight ?? spaceRight + (legendMetrics.beside && !legendBesideLeft ? legendMetrics.width : 0);
539
+ const left = options.chart?.marginLeft ?? edgeRoom(spaceLeft + gutter + (legendBesideLeft ? legendMetrics.width : 0), domainMin === acc.x.min);
540
+ const right = options.chart?.marginRight ?? edgeRoom(spaceRight + (legendMetrics.beside && !legendBesideLeft ? legendMetrics.width : 0), domainMax === acc.x.max);
524
541
  const top = options.chart?.marginTop ?? marginTop;
525
542
  const bottom = options.chart?.marginBottom ?? marginBottom;
526
543
  const plot = {
@@ -567,9 +584,9 @@ export function buildChartModel({
567
584
  options: xAxisOptions,
568
585
  scale: xScale,
569
586
  ticks: axisTicks(xAxisOptions, xScale, xHint, timePattern, 'x', numberFormat,
570
- // Highcharts turns an end label inwards only where it would leave the
571
- // canvas, not where it leaves the plot: the gutter is room to hang over.
572
- [0, size.width])
587
+ // Highcharts justifies against the chart spacing box, not the plot: the
588
+ // gutter is room to hang over, the spacing is not.
589
+ [Math.min(plot.x, spaceLeft), Math.max(plot.x + plot.width, size.width - spaceRight)])
573
590
  },
574
591
  yAxes: yAxisModels,
575
592
  titles: titles.entries,
@@ -1,4 +1,4 @@
1
- import { type AxisOptions, type ChartOptions, type SeriesOptionsBase } from '../types/options';
1
+ import { type AxisOptions, type ChartOptions, type MarkerOptions, type SeriesOptionsBase } from '../types/options';
2
2
  export declare const toAxisArray: (axis: AxisOptions | readonly AxisOptions[] | undefined) => AxisOptions[];
3
3
  export declare function resolveSeries(options: ChartOptions): SeriesOptionsBase[];
4
4
  export declare function resolveAxes(options: ChartOptions): {
@@ -6,3 +6,7 @@ export declare function resolveAxes(options: ChartOptions): {
6
6
  yAxes: AxisOptions[];
7
7
  };
8
8
  export declare const isSeriesVisible: (series: SeriesOptionsBase) => boolean;
9
+ /** A marker at every point of the series. */
10
+ export declare const showsMarkers: (marker: MarkerOptions | undefined) => boolean;
11
+ /** The dot under the pointer, which Highcharts draws whatever `marker.enabled` says. */
12
+ export declare const showsHoverMarker: (marker: MarkerOptions | undefined) => boolean;
@@ -40,4 +40,8 @@ export function resolveAxes(options) {
40
40
  yAxes: toAxisArray(options.yAxis)
41
41
  };
42
42
  }
43
- export const isSeriesVisible = series => series.visible !== false;
43
+ export const isSeriesVisible = series => series.visible !== false;
44
+ /** A marker at every point of the series. */
45
+ export const showsMarkers = marker => marker?.enabled !== false && marker?.symbol !== 'none';
46
+ /** The dot under the pointer, which Highcharts draws whatever `marker.enabled` says. */
47
+ export const showsHoverMarker = marker => marker?.states?.hover?.enabled !== false;
@@ -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.14",
3
+ "version": "0.0.16",
4
4
  "description": "Small React chart library on SVG, with a Highcharts-shaped configuration model",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { drawable, projectSeries, toAreaPath, toLinePath } from '../../core/geometry/path';
3
+ import { showsHoverMarker } from '../../core/options/normalize';
3
4
  import { includeZero } from '../shared/domain';
4
5
  import { nearestByX } from '../shared/hit';
5
6
  import { seriesLegend } from '../shared/legend';
@@ -41,7 +42,7 @@ export const area = {
41
42
  marker: ctx.series.marker,
42
43
  projected: projected,
43
44
  color: color
44
- }), hovered ? _jsx("circle", {
45
+ }), hovered && showsHoverMarker(ctx.series.marker) ? _jsx("circle", {
45
46
  cx: hovered.px,
46
47
  cy: hovered.py,
47
48
  r: ctx.series.marker?.states?.hover?.radius ?? 4,
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { drawable, projectSeries, toLinePath } from '../../core/geometry/path';
3
+ import { showsHoverMarker } from '../../core/options/normalize';
3
4
  import { nearestByX } from '../shared/hit';
4
5
  import { seriesLegend } from '../shared/legend';
5
6
  import { Markers } from '../shared/Markers';
@@ -33,7 +34,7 @@ export const line = {
33
34
  marker: ctx.series.marker,
34
35
  projected: projected,
35
36
  color: color
36
- }), hovered ? _jsx("circle", {
37
+ }), hovered && showsHoverMarker(ctx.series.marker) ? _jsx("circle", {
37
38
  cx: hovered.px,
38
39
  cy: hovered.py,
39
40
  r: ctx.series.marker?.states?.hover?.radius ?? 4,
@@ -1,11 +1,12 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { showsMarkers } from '../../core/options/normalize';
2
3
  const markerRadius = marker => marker?.radius ?? (marker?.width ? marker.width / 2 : 3);
3
4
  export function Markers({
4
5
  marker,
5
6
  projected,
6
7
  color
7
8
  }) {
8
- if (marker?.enabled === false || marker?.symbol === 'none') {
9
+ if (!showsMarkers(marker)) {
9
10
  return null;
10
11
  }
11
12
  const radius = markerRadius(marker);