@pushwoosh/kit-charts 0.0.7 → 0.0.8

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.
Files changed (2) hide show
  1. package/core/scale/time.js +31 -12
  2. package/package.json +1 -1
@@ -63,6 +63,10 @@ const LADDER = [{
63
63
  unit: 'week',
64
64
  step: 1,
65
65
  approx: WEEK
66
+ }, {
67
+ unit: 'week',
68
+ step: 2,
69
+ approx: 2 * WEEK
66
70
  }, {
67
71
  unit: 'month',
68
72
  step: 1,
@@ -84,9 +88,10 @@ const LADDER = [{
84
88
  approx: 365 * DAY,
85
89
  calendar: true
86
90
  }];
87
- const pickRung = (span, count) => {
91
+ const pickRungIndex = (span, count) => {
88
92
  const target = span / Math.max(1, count);
89
- return LADDER.find(rung => rung.approx >= target) ?? LADDER[LADDER.length - 1];
93
+ const found = LADDER.findIndex(rung => rung.approx >= target);
94
+ return found === -1 ? LADDER.length - 1 : found;
90
95
  };
91
96
  const alignUp = (from, rung) => {
92
97
  const date = new Date(from);
@@ -119,6 +124,20 @@ const advance = (timestamp, rung) => {
119
124
  return Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + months, date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
120
125
  };
121
126
  const rungFor = unit => LADDER.find(rung => rung.unit === unit && rung.step === 1) ?? LADDER[LADDER.length - 1];
127
+ const ticksFor = (min, max, rung) => {
128
+ const ticks = [];
129
+ // Calendar alignment lands on the start of the current unit, which can sit
130
+ // before the domain — a tick there would be drawn outside the plot.
131
+ let cursor = alignUp(min, rung);
132
+ if (cursor < min) {
133
+ cursor = advance(cursor, rung);
134
+ }
135
+ for (let i = 0; cursor <= max && i < 1000; i += 1) {
136
+ ticks.push(cursor);
137
+ cursor = advance(cursor, rung);
138
+ }
139
+ return ticks;
140
+ };
122
141
  export function timeTicks(min, max, options = {}) {
123
142
  const {
124
143
  count = 6,
@@ -130,17 +149,17 @@ export function timeTicks(min, max, options = {}) {
130
149
  if (min === max) {
131
150
  return [min];
132
151
  }
133
- const rung = unit ? rungFor(unit) : pickRung(max - min, count);
134
- const ticks = [];
135
- // Calendar alignment lands on the start of the current unit, which can sit
136
- // before the domain — a tick there would be drawn outside the plot.
137
- let cursor = alignUp(min, rung);
138
- if (cursor < min) {
139
- cursor = advance(cursor, rung);
152
+ if (unit) {
153
+ const fixed = ticksFor(min, max, rungFor(unit));
154
+ return fixed.length ? fixed : [min, max];
140
155
  }
141
- for (let i = 0; cursor <= max && i < 1000; i += 1) {
142
- ticks.push(cursor);
143
- cursor = advance(cursor, rung);
156
+ // A rung as coarse as the span lands a single tick inside it — a month step on
157
+ // a month of data — so step down until the axis has something to read.
158
+ let index = pickRungIndex(max - min, count);
159
+ let ticks = ticksFor(min, max, LADDER[index]);
160
+ while (ticks.length < 2 && index > 0) {
161
+ index -= 1;
162
+ ticks = ticksFor(min, max, LADDER[index]);
144
163
  }
145
164
  return ticks.length ? ticks : [min, max];
146
165
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushwoosh/kit-charts",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Small React chart library on SVG, with a Highcharts-shaped configuration model",
5
5
  "license": "MIT",
6
6
  "repository": {