@tylix/charts 1.1.0 → 1.3.0
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.
- package/README.md +123 -22
- package/components/Gauge.tyx +102 -0
- package/components/Heatmap.tyx +148 -0
- package/components/LineChart.tyx +21 -2
- package/components/ScatterChart.tyx +162 -0
- package/package.json +36 -3
- package/src/a11y.js +50 -0
- package/src/area.js +8 -7
- package/src/crosshair.js +1 -1
- package/src/frameworks/react.d.ts +23 -0
- package/src/frameworks/react.js +38 -0
- package/src/frameworks/shared.js +37 -0
- package/src/frameworks/svelte/Chart.svelte +24 -0
- package/src/frameworks/svelte/index.d.ts +15 -0
- package/src/frameworks/svelte/index.js +1 -0
- package/src/frameworks/vue.d.ts +21 -0
- package/src/frameworks/vue.js +64 -0
- package/src/gauge.js +81 -0
- package/src/heatmap.js +83 -0
- package/src/index.d.ts +152 -2
- package/src/index.js +11 -0
- package/src/line.js +40 -8
- package/src/scatter.js +109 -0
- package/src/svg.js +178 -11
- package/src/time.js +164 -0
- package/tylix-components.json +4 -1
package/src/time.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// Time axes. A series whose x values are Dates (or ISO date strings
|
|
2
|
+
// with `time: true`) is plotted on milliseconds, with ticks on calendar
|
|
3
|
+
// boundaries (whole minutes, hours, days, weeks, month starts, years)
|
|
4
|
+
// and labels formatted for the tick interval.
|
|
5
|
+
//
|
|
6
|
+
// Dates are shown in UTC by default so the server and the browser draw
|
|
7
|
+
// the same chart; pass `timeZone: 'local'` or an IANA zone to change it.
|
|
8
|
+
|
|
9
|
+
const SECOND = 1000
|
|
10
|
+
const MINUTE = 60 * SECOND
|
|
11
|
+
const HOUR = 60 * MINUTE
|
|
12
|
+
const DAY = 24 * HOUR
|
|
13
|
+
const WEEK = 7 * DAY
|
|
14
|
+
|
|
15
|
+
// [unit, step, approximate ms]
|
|
16
|
+
const INTERVALS = [
|
|
17
|
+
['second', 1, SECOND], ['second', 5, 5 * SECOND], ['second', 15, 15 * SECOND], ['second', 30, 30 * SECOND],
|
|
18
|
+
['minute', 1, MINUTE], ['minute', 5, 5 * MINUTE], ['minute', 15, 15 * MINUTE], ['minute', 30, 30 * MINUTE],
|
|
19
|
+
['hour', 1, HOUR], ['hour', 3, 3 * HOUR], ['hour', 6, 6 * HOUR], ['hour', 12, 12 * HOUR],
|
|
20
|
+
['day', 1, DAY], ['day', 2, 2 * DAY], ['week', 1, WEEK], ['week', 2, 2 * WEEK],
|
|
21
|
+
['month', 1, 30 * DAY], ['month', 3, 91 * DAY], ['month', 6, 182 * DAY],
|
|
22
|
+
['year', 1, 365 * DAY],
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
// Works across realms (iframes) and through reactive proxies, where
|
|
26
|
+
// `instanceof Date` can be false.
|
|
27
|
+
export function isDate(value) {
|
|
28
|
+
return (
|
|
29
|
+
value instanceof Date ||
|
|
30
|
+
(value != null && typeof value === 'object' && typeof value.getTime === 'function' && typeof value.toISOString === 'function')
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function toTime(value) {
|
|
35
|
+
if (isDate(value)) return value.getTime()
|
|
36
|
+
if (typeof value === 'string') {
|
|
37
|
+
const t = Date.parse(value)
|
|
38
|
+
return Number.isNaN(t) ? null : t
|
|
39
|
+
}
|
|
40
|
+
return typeof value === 'number' ? value : null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** True when any point's x is a Date. */
|
|
44
|
+
export function isTimeData(series = []) {
|
|
45
|
+
return series.some((s) =>
|
|
46
|
+
(s.data ?? []).some((d) => d && typeof d === 'object' && isDate(d.x)),
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function zoneOf(timeZone) {
|
|
51
|
+
return timeZone === 'local' ? undefined : timeZone ?? 'UTC'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Calendar parts in the chosen zone.
|
|
55
|
+
function parts(ms, timeZone) {
|
|
56
|
+
const d = new Date(ms)
|
|
57
|
+
if (zoneOf(timeZone) === 'UTC') {
|
|
58
|
+
return { y: d.getUTCFullYear(), m: d.getUTCMonth(), d: d.getUTCDate(), dow: d.getUTCDay() }
|
|
59
|
+
}
|
|
60
|
+
return { y: d.getFullYear(), m: d.getMonth(), d: d.getDate(), dow: d.getDay() }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function makeDate(y, m, day, timeZone) {
|
|
64
|
+
return zoneOf(timeZone) === 'UTC' ? Date.UTC(y, m, day) : new Date(y, m, day).getTime()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Picks the interval whose tick count is closest to `count` (allowing
|
|
68
|
+
// up to 1.5x as many), so 6.4 monthly ticks beat 2 quarterly ones.
|
|
69
|
+
export function timeInterval(start, end, count = 6) {
|
|
70
|
+
const span = Math.max(1, end - start)
|
|
71
|
+
let best = null
|
|
72
|
+
let bestScore = Infinity
|
|
73
|
+
for (const iv of INTERVALS) {
|
|
74
|
+
const n = span / iv[2]
|
|
75
|
+
if (n > count * 1.5) continue
|
|
76
|
+
const score = Math.abs(Math.log(Math.max(n, 1e-9) / count))
|
|
77
|
+
if (score < bestScore) {
|
|
78
|
+
bestScore = score
|
|
79
|
+
best = iv
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (best && span / (365 * 86400000) <= count * 1.5) return { unit: best[0], step: best[1] }
|
|
83
|
+
const years = Math.ceil(span / (365 * DAY) / count)
|
|
84
|
+
const nice = [1, 2, 5, 10, 20, 25, 50, 100].find((n) => n >= years) ?? years
|
|
85
|
+
return { unit: 'year', step: nice }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Tick timestamps inside [start, end] on calendar boundaries. */
|
|
89
|
+
export function timeTicks(start, end, count = 6, { timeZone } = {}) {
|
|
90
|
+
if (!Number.isFinite(start) || !Number.isFinite(end)) return { ticks: [], unit: 'day', step: 1 }
|
|
91
|
+
if (start > end) [start, end] = [end, start]
|
|
92
|
+
const { unit, step } = timeInterval(start, end, count)
|
|
93
|
+
const ticks = []
|
|
94
|
+
if (unit === 'second' || unit === 'minute' || unit === 'hour') {
|
|
95
|
+
const size = { second: SECOND, minute: MINUTE, hour: HOUR }[unit] * step
|
|
96
|
+
// Whole units (UTC offsets are whole minutes for nearly every zone).
|
|
97
|
+
for (let t = Math.ceil(start / size) * size; t <= end; t += size) ticks.push(t)
|
|
98
|
+
} else if (unit === 'day' || unit === 'week') {
|
|
99
|
+
const p = parts(start, timeZone)
|
|
100
|
+
let t = makeDate(p.y, p.m, p.d, timeZone)
|
|
101
|
+
if (unit === 'week') t = makeDate(p.y, p.m, p.d - ((p.dow + 6) % 7), timeZone) // Mondays
|
|
102
|
+
const stepDays = unit === 'week' ? 7 * step : step
|
|
103
|
+
for (let i = 0; i < 1000; i++) {
|
|
104
|
+
const q = parts(t, timeZone)
|
|
105
|
+
if (t >= start && t <= end) ticks.push(t)
|
|
106
|
+
if (t > end) break
|
|
107
|
+
t = makeDate(q.y, q.m, q.d + stepDays, timeZone)
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
const p = parts(start, timeZone)
|
|
111
|
+
const months = unit === 'year' ? 12 * step : step
|
|
112
|
+
let y = p.y
|
|
113
|
+
let m = unit === 'year' ? 0 : Math.floor(p.m / step) * step
|
|
114
|
+
if (unit === 'year') y = Math.floor(p.y / step) * step
|
|
115
|
+
for (let i = 0; i < 1000; i++) {
|
|
116
|
+
const t = makeDate(y, m, 1, timeZone)
|
|
117
|
+
if (t > end) break
|
|
118
|
+
if (t >= start) ticks.push(t)
|
|
119
|
+
m += months
|
|
120
|
+
y += Math.floor(m / 12)
|
|
121
|
+
m %= 12
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return { ticks, unit, step }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const FORMATS = {
|
|
128
|
+
second: { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false },
|
|
129
|
+
minute: { hour: '2-digit', minute: '2-digit', hour12: false },
|
|
130
|
+
hour: { hour: '2-digit', minute: '2-digit', hour12: false },
|
|
131
|
+
day: { month: 'short', day: 'numeric' },
|
|
132
|
+
week: { month: 'short', day: 'numeric' },
|
|
133
|
+
month: { month: 'short', year: 'numeric' },
|
|
134
|
+
year: { year: 'numeric' },
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** A label formatter for ticks of `unit`. */
|
|
138
|
+
export function timeFormatter(unit, { timeZone, locale = 'en-US' } = {}) {
|
|
139
|
+
const f = new Intl.DateTimeFormat(locale, { ...FORMATS[unit], timeZone: zoneOf(timeZone) })
|
|
140
|
+
return (v) => f.format(new Date(v))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** A fuller formatter for tooltips (date, plus time for sub-day data). */
|
|
144
|
+
export function timeTooltipFormatter(unit, { timeZone, locale = 'en-US' } = {}) {
|
|
145
|
+
const subDay = unit === 'second' || unit === 'minute' || unit === 'hour'
|
|
146
|
+
const f = new Intl.DateTimeFormat(locale, {
|
|
147
|
+
year: 'numeric', month: 'short', day: 'numeric',
|
|
148
|
+
...(subDay ? { hour: '2-digit', minute: '2-digit', hour12: false } : {}),
|
|
149
|
+
timeZone: zoneOf(timeZone),
|
|
150
|
+
})
|
|
151
|
+
return (v) => f.format(new Date(v))
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Time axis for [start, end] (ms or Dates):
|
|
156
|
+
* { ticks: [{ value, label }], unit, step, format, tooltip }
|
|
157
|
+
*/
|
|
158
|
+
export function timeAxis(start, end, count = 6, options = {}) {
|
|
159
|
+
const s = toTime(start)
|
|
160
|
+
const e = toTime(end)
|
|
161
|
+
const { ticks, unit, step } = timeTicks(s, e, count, options)
|
|
162
|
+
const format = timeFormatter(unit, options)
|
|
163
|
+
return { ticks: ticks.map((value) => ({ value, label: format(value) })), unit, step, format, tooltip: timeTooltipFormatter(unit, options) }
|
|
164
|
+
}
|
package/tylix-components.json
CHANGED
|
@@ -4,5 +4,8 @@
|
|
|
4
4
|
"AreaChart": "components/AreaChart.tyx",
|
|
5
5
|
"BarChart": "components/BarChart.tyx",
|
|
6
6
|
"PieChart": "components/PieChart.tyx",
|
|
7
|
-
"Donut": "components/Donut.tyx"
|
|
7
|
+
"Donut": "components/Donut.tyx",
|
|
8
|
+
"ScatterChart": "components/ScatterChart.tyx",
|
|
9
|
+
"Gauge": "components/Gauge.tyx",
|
|
10
|
+
"Heatmap": "components/Heatmap.tyx"
|
|
8
11
|
}
|