@pushwoosh/kit-charts 0.0.11 → 0.0.12

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.
@@ -92,6 +92,12 @@ own `formatter` or `format` wins over it.
92
92
  the step is chosen from the candidates that stay integral at its magnitude, and
93
93
  never falls below 1.
94
94
 
95
+ `tickAmount` is Highcharts' demand and not a hint: the y axis stretches the end
96
+ the caller left open until exactly that many ticks fit, so a count of 2 on a
97
+ `tickAmount: 5` axis still draws 0..4 and reads against a stable grid rather than
98
+ filling the plot. An end pinned with `max` cannot be stretched, so a tick amount
99
+ alongside one is ignored.
100
+
95
101
  `labels.allowOverlap` defaults to false on both axes and on data labels. A label
96
102
  whose box would run into one already placed loses its text — Highcharts hides
97
103
  the label and keeps the tick, and so do we.
@@ -139,6 +139,7 @@ function buildYScale(axis, extent, range, hint, linked) {
139
139
  max: axis.max === undefined
140
140
  },
141
141
  tickHint: hint,
142
+ tickCount: axis.tickAmount,
142
143
  allowDecimals: axis.allowDecimals
143
144
  });
144
145
  }
@@ -9,6 +9,8 @@ export interface LinearScaleOptions {
9
9
  range: readonly [number, number];
10
10
  nice?: boolean | NiceEnds;
11
11
  tickHint?: number;
12
+ /** Highcharts' `tickAmount`: stretch the open end so exactly this many ticks fit. */
13
+ tickCount?: number;
12
14
  /** False keeps every tick a whole number, as a count axis needs. */
13
15
  allowDecimals?: boolean;
14
16
  }
@@ -31,18 +31,32 @@ export function createLinearScale(options) {
31
31
  range,
32
32
  nice = false,
33
33
  tickHint = 5,
34
+ tickCount,
34
35
  allowDecimals = true
35
36
  } = options;
36
37
  let [min, max] = options.domain;
38
+ const niceMin = nice === true || typeof nice === 'object' && nice.min === true;
39
+ const niceMax = nice === true || typeof nice === 'object' && nice.max === true;
37
40
  if (!Number.isFinite(min) || !Number.isFinite(max)) {
38
41
  [min, max] = [0, 1];
39
42
  } else if (min === max) {
40
43
  const pad = Math.abs(min) || 1;
41
- [min, max] = [min - pad, max + pad];
44
+ // An end the caller pinned stays put: padding both would take a `min: 0`
45
+ // count axis below zero on a day where every value is zero.
46
+ [min, max] = [min - (niceMin || !niceMax ? pad : 0), max + (niceMax || !niceMin ? pad : 0)];
42
47
  }
43
- const niceMin = nice === true || typeof nice === 'object' && nice.min === true;
44
- const niceMax = nice === true || typeof nice === 'object' && nice.max === true;
45
- if (niceMin || niceMax) {
48
+ if (tickCount !== undefined && tickCount > 1 && niceMax) {
49
+ let step = niceStep((max - min) / (tickCount - 1), allowDecimals);
50
+ if (niceMin) {
51
+ min = Math.floor(min / step) * step;
52
+ }
53
+ // Rounding the low end down lengthens the run, which can leave the last tick
54
+ // short of the data; widen the step until the fixed number of them covers it.
55
+ while (min + (tickCount - 1) * step < max) {
56
+ step = niceStep(step * 1.5, allowDecimals);
57
+ }
58
+ max = min + (tickCount - 1) * step;
59
+ } else if (niceMin || niceMax) {
46
60
  const step = niceStep((max - min) / tickHint, allowDecimals);
47
61
  if (niceMin) {
48
62
  min = Math.floor(min / step) * step;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushwoosh/kit-charts",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "Small React chart library on SVG, with a Highcharts-shaped configuration model",
5
5
  "license": "MIT",
6
6
  "repository": {