MCPcopy Create free account
hub / github.com/Athenodoros/TopHat / getChartDomainFunctions

Function getChartDomainFunctions

src/shared/data.ts:45–69  ·  view source on GitHub ↗
(values: number[], padding: number = 0)

Source from the content-addressed store, hash-verified

43 getOffsetAndSizeForRange: (x: number, y?: number) => { offset: string; size: string };
44}
45export const getChartDomainFunctions = (values: number[], padding: number = 0): ChartDomainFunctions => {
46 const flip = values.every((x) => x <= 0) ? -1 : 1;
47 values = values.map((x) => x * flip);
48
49 let min = Math.min(0, ...values);
50 let max = Math.max(0, ...values);
51
52 if (min === 0 && max === 0) {
53 max = 0.1;
54 }
55
56 const valueRange = max - min;
57 min -= min ? valueRange * padding : 0;
58 max += valueRange * padding;
59
60 const scale = (raw: number) => (raw / (max - min)) * 100 + "%";
61
62 const getPoint = (raw: number) => scale(raw * flip - min);
63 const getOffsetAndSizeForRange = (x: number, y: number = 0) => ({
64 offset: scale(Math.min(x * flip, y * flip) - min),
65 size: scale(Math.abs(x - y)),
66 });
67
68 return { getPoint, getOffsetAndSizeForRange };
69};
70
71export const mapValuesWithKeys = <K extends string | number, V, O>(object: Record<K, V>, fn: (k: K, v: V) => O) =>
72 fromPairs(toPairs(object).map(([k, v]) => [k, fn(k as K, v as V)])) as Record<K, O>;

Callers 4

BasicBarChartFunction · 0.90
useCategoriesTableDataFunction · 0.90
CategoriesBarSummaryFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected