MCPcopy Create free account
hub / github.com/tinyplex/tinybase / getTimeTicks

Function getTimeTicks

src/ui-react-dom-charts/common/wilkinson.ts:225–275  ·  view source on GitHub ↗
(
  minTimestamp: number,
  maxTimestamp: number,
  targetTickCount: number,
  labelSize: number,
  axisSize: number,
)

Source from the content-addressed store, hash-verified

223};
224
225export const getTimeTicks = (
226 minTimestamp: number,
227 maxTimestamp: number,
228 targetTickCount: number,
229 labelSize: number,
230 axisSize: number,
231): number[] => {
232 const min = mathMin(minTimestamp, maxTimestamp);
233 const max = mathMax(minTimestamp, maxTimestamp);
234 let bestTicks: number[] = [min, max];
235 let bestScore = -infinity;
236
237 arrayForEach(TIME_INTERVALS, (interval, index) => {
238 const ticks: number[] = [];
239 for (
240 let tick = floorTime(min, interval), count = 0;
241 tick <= max && count < 1000;
242 tick = addTime(tick, interval), count++
243 ) {
244 arrayPush(ticks, tick);
245 }
246 const lastTick = ticks[size(ticks) - 1];
247 if (!isFiniteNumber(lastTick) || lastTick < max) {
248 arrayPush(ticks, addTime(lastTick ?? floorTime(min, interval), interval));
249 }
250 const tickCount = size(ticks);
251 const tickMin = ticks[0];
252 const tickMax = ticks[tickCount - 1];
253 const range = max - min;
254 const target = mathMax(targetTickCount, 2);
255 const spacing = axisSize / (tickCount - 1);
256 const score =
257 tickCount < 2
258 ? -infinity
259 : WEIGHTS[0] * (1 - index / mathMax(size(TIME_INTERVALS) - 1, 1)) +
260 WEIGHTS[1] *
261 (1 - (mathAbs(min - tickMin) + mathAbs(tickMax - max)) / range) +
262 WEIGHTS[2] * (2 - mathMax(tickCount / target, target / tickCount)) +
263 WEIGHTS[3] *
264 (spacing < labelSize * 1.2
265 ? -infinity
266 : mathMin(spacing / labelSize, 1));
267
268 if (score > bestScore) {
269 bestTicks = ticks;
270 bestScore = score;
271 }
272 });
273
274 return bestTicks;
275};
276
277export const getTimeTickLabel = (
278 timestamp: number,

Callers 1

CartesianChartFunction · 0.90

Calls 5

arrayForEachFunction · 0.90
arrayPushFunction · 0.90
sizeFunction · 0.90
floorTimeFunction · 0.70
addTimeFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…