@tylix/charts 1.2.0 → 1.4.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 CHANGED
@@ -1,36 +1,137 @@
1
1
  # @tylix/charts
2
2
 
3
- Hand-rolled SVG charts for Tylix. No third-party dependencies.
3
+ SVG charts with no dependencies. Use them as `.tyx` components in Tylix
4
+ pages, as React, Vue or Svelte components, or as plain functions that
5
+ return SVG strings or geometry.
4
6
 
5
- ## What's included
7
+ | Chart | Function | `.tyx` component |
8
+ | --- | --- | --- |
9
+ | Line (optionally filled) | `lineChart` / `lineChartSvg` | `<LineChart>` |
10
+ | Area, stacked area | `areaChart` / `areaChartSvg` | `<AreaChart>` |
11
+ | Bar: grouped, stacked, horizontal | `barChart` / `barChartSvg` | `<BarChart>` |
12
+ | Pie, donut | `pieChart`, `donutChart` / `…Svg` | `<PieChart>`, `<Donut>` |
13
+ | Scatter, bubble | `scatterChart` / `scatterChartSvg` | `<ScatterChart>` |
14
+ | Gauge | `gauge` / `gaugeSvg` | `<Gauge>` |
15
+ | Calendar heatmap | `calendarHeatmap` / `calendarHeatmapSvg` | `<Heatmap>` |
16
+ | Sparkline | `sparkline` / `sparklineSvg` | `<Sparkline>` |
6
17
 
7
- - `sparkline()` and `lineChart()` return plain geometry (paths, ticks,
8
- plot area) so a `.tyx` template can render them with `{{#each}}`
9
- - `sparklineSvg()` and `lineChartSvg()` return finished SVG strings
10
- - Building blocks: `linearScale`, `niceTicks`, `extent`, `linePath`, `areaPath`
11
- - Gap support (`null` values break the line), `linear` and `step` curves,
12
- optional area fill, multiple series
13
- - TypeScript declarations (`index.d.ts`)
14
- - Themeable colors via `--tylix-chart-1` through `--tylix-chart-5`;
15
- sparklines inherit `currentColor`
18
+ All of them come with:
16
19
 
17
- ## Usage
20
+ - **Time axes**: give points a `Date` as `x` and the axis ticks on whole hours, days, weeks, months or years. Labels and tooltips are formatted to match (UTC by default; `timeZone`, `locale`).
21
+ - **Tooltips**: an interactive crosshair for line and area charts (pointer and arrow keys), hover tooltips on scatter points and heatmap days, and `<title>` tooltips in the plain SVG output.
22
+ - **Legends**: shown automatically for two or more series. `legend: false` hides them.
23
+ - **Accessibility**: every SVG has a `<title>` and a plain-language `<desc>` ("1 series over 12 points. CPU: 10 to 80, last 64."). The components also include a visually hidden data table, and the gauge uses `role="meter"`.
24
+ - **Theming**: series colors come from `--tylix-chart-1` … `--tylix-chart-5`. Text and gridlines use `currentColor`, and `--tylix-chart-bg` sets the surface color.
18
25
 
19
- ```js
20
- import { sparkline, lineChart } from '@tylix/charts'
26
+ ```sh
27
+ npm install @tylix/charts
28
+ ```
29
+
30
+ ## In Tylix pages
31
+
32
+ ```html
33
+ page Dashboard
34
+
35
+ import { LineChart, BarChart, Gauge, Heatmap, ScatterChart } from "@tylix/charts"
36
+
37
+ state
38
+ visits: [{ name: "Visits", data: [] }]
39
+ cpu: 62
40
+
41
+ template
42
+ <LineChart label="Visits" series="{{ visits }}" height="{{ 260 }}" />
43
+ <Gauge value="{{ cpu }}" caption="CPU" needle="{{ true }}"
44
+ thresholds="{{ [{ value: 0, color: '#16a34a' }, { value: 80, color: '#dc2626' }] }}" />
45
+ <Heatmap label="Commits" data="{{ commits }}" />
46
+ ```
47
+
48
+ The components load the chart geometry from `/_tylix/charts/` (served by
49
+ `tylix dev` / `tylix start`) and render with Tailwind-aware dark mode.
50
+
51
+ ## React
52
+
53
+ ```jsx
54
+ import { LineChart, Gauge, Chart } from '@tylix/charts/react'
55
+
56
+ <LineChart series={series} height={260} formatY={(v) => `${v}%`} />
57
+ <Gauge value={72} caption="CPU" needle />
58
+ <Chart type="heatmap" data={days} />
59
+ ```
60
+
61
+ ## Vue
62
+
63
+ ```vue
64
+ <script setup>
65
+ import { LineChart, BarChart } from '@tylix/charts/vue'
66
+ </script>
67
+
68
+ <template>
69
+ <LineChart :series="series" :height="260" />
70
+ <BarChart :series="sales" :categories="['Q1', 'Q2', 'Q3']" stacked />
71
+ </template>
72
+ ```
21
73
 
22
- const s = sparkline([12, 18, 15, 30, 22], { width: 120, height: 32 })
23
- // s.path, s.areaPath, s.last
74
+ ## Svelte 5
24
75
 
25
- const c = lineChart(
26
- [{ name: 'CPU', data: [10, 40, 30, 80] }],
27
- { width: 480, height: 240, formatY: (v) => `${v}%` }
76
+ ```svelte
77
+ <script>
78
+ import { Chart } from '@tylix/charts/svelte'
79
+ </script>
80
+
81
+ <Chart type="scatter" series={points} height={300} />
82
+ ```
83
+
84
+ All three wrappers render the same SVG on the server and in the browser.
85
+ SVG ids come from the framework's own id helper, so hydration matches.
86
+ Line and area charts get the interactive crosshair once mounted. Chart
87
+ options can be passed as props or all together as `options={...}`.
88
+
89
+ ## Angular and anything else
90
+
91
+ `renderChart(type, data, options)` returns an SVG string:
92
+
93
+ ```ts
94
+ import { renderChart } from '@tylix/charts'
95
+
96
+ this.svg = this.sanitizer.bypassSecurityTrustHtml(
97
+ renderChart('bar', this.series, { categories: this.quarters }),
28
98
  )
29
- // c.plot, c.xTicks, c.yTicks, c.series[0].path
99
+ // <div [innerHTML]="svg"></div>
30
100
  ```
31
101
 
32
- Run the tests with `node --test`.
102
+ For the line/area crosshair, call `attachCrosshair(svgElement, chartGeometry(type, data, options))`.
103
+
104
+ ## Data shapes
105
+
106
+ ```js
107
+ // line, area, bar
108
+ [{ name: 'CPU', color?: '#2563eb', data: [10, 40, null, 25] }] // x = index, null = gap
109
+ [{ name: 'Visits', data: [{ x: new Date('2024-05-01'), y: 120 }] }] // time axis
110
+
111
+ // scatter / bubble (size makes bubbles; radius grows with the square root)
112
+ [{ name: 'Plans', data: [{ x: 12, y: 40, size: 300, label: 'Pro' }] }]
113
+
114
+ // pie, donut
115
+ [{ label: 'Direct', value: 40 }, { label: 'Search', value: 25 }]
116
+
117
+ // gauge
118
+ gaugeSvg(72, { min: 0, max: 100, thresholds: [{ value: 0, color: 'green' }, { value: 80, color: 'red' }], bands: true, needle: true })
119
+
120
+ // calendar heatmap: an array or an object keyed by date
121
+ [{ date: '2024-05-01', value: 3 }] or { '2024-05-01': 3 }
122
+ calendarHeatmapSvg(days, { to: '2024-12-31', weekStart: 1, levels: 5 })
123
+ ```
124
+
125
+ ## Geometry only
126
+
127
+ Every `…Chart()` function returns plain geometry: paths, ticks, the plot
128
+ area and the points with their pixel positions. Render it however you
129
+ like, for example with `{{#each}}` in a template, in a canvas, or in a
130
+ PDF. The building blocks are exported too: `linearScale`, `bandScale`,
131
+ `niceTicks`, `extent`, `linePath`, `areaPath`, `arcPath`, `timeAxis`,
132
+ `timeTicks`, `nearestPoints`, `nearestScatterPoint`, `describeSeries`
133
+ and `dataTable`.
33
134
 
34
135
  ## License
35
136
 
36
- MIT
137
+ MIT
@@ -0,0 +1,102 @@
1
+ component Gauge
2
+
3
+ props
4
+ value: number
5
+ min?: number
6
+ max?: number
7
+ label?: string
8
+ caption?: string
9
+ size?: number
10
+ thickness?: number
11
+ startAngle?: number
12
+ endAngle?: number
13
+ thresholds?: Array
14
+ bands?: boolean
15
+ needle?: boolean
16
+ color?: string
17
+ format?: Function
18
+ class?: string
19
+
20
+ state
21
+ ready: false
22
+
23
+ computed
24
+ g() {
25
+ if not this.ready {
26
+ return null
27
+ }
28
+ return window.__tylixCharts.gauge(this.value, {
29
+ min: this.min ?? 0,
30
+ max: this.max ?? 100,
31
+ size: this.size ?? 200,
32
+ thickness: this.thickness ?? 0.16,
33
+ startAngle: this.startAngle ?? -120,
34
+ endAngle: this.endAngle ?? 120,
35
+ thresholds: this.thresholds ?? [],
36
+ bands: this.bands ?? false,
37
+ color: this.color ?? "var(--tylix-chart-1, #2563eb)",
38
+ format: this.format ?? (v => String(Math.round(v * 10) / 10)),
39
+ })
40
+ }
41
+
42
+ valueY() {
43
+ return this.needle ? this.g.cy + this.g.size * 0.22 : this.g.cy + 8
44
+ }
45
+
46
+ height() {
47
+ const g = this.g
48
+ return Math.ceil(Math.max(g.bottom + 18, this.valueY + (this.caption ? g.size * 0.1 : 0) + 8))
49
+ }
50
+
51
+ ends() {
52
+ const g = this.g
53
+ const r = g.size / 2 - 2
54
+ const a0 = (this.startAngle ?? -120) * Math.PI / 180
55
+ const a1 = (this.endAngle ?? 120) * Math.PI / 180
56
+ const at = a => ({ x: g.cx + r * 0.82 * Math.sin(a), y: Math.min(this.height - 4, g.cy - r * Math.cos(a) + 14) })
57
+ return { lo: at(a0), hi: at(a1) }
58
+ }
59
+
60
+ description() {
61
+ const g = this.g
62
+ return `${this.label ?? "Gauge"}: ${g.label} of ${g.min} to ${g.max}${this.caption ? `, ${this.caption}` : ""}`
63
+ }
64
+
65
+ onMount
66
+ async {
67
+ const load = window.__tylixChartsLoad ?? (window.__tylixChartsLoad = import("/_tylix/charts/index.js").then(m => { window.__tylixCharts = m }))
68
+ try {
69
+ await load
70
+ this.ready = true
71
+ } catch (e) {
72
+ console.error("[tylix/charts] could not load geometry", e)
73
+ window.__tylixChartsLoad = null
74
+ }
75
+ }
76
+
77
+ template
78
+ <div class="{{ `tylix-chart text-zinc-500 dark:text-zinc-400 ${this.class ?? ''}` }}">
79
+ {{#if ready}}
80
+ <svg viewBox="{{ `0 0 ${g.size} ${height}` }}" class="w-full h-auto" font-size="11" role="meter" aria-valuenow="{{ g.value }}" aria-valuemin="{{ g.min }}" aria-valuemax="{{ g.max }}" aria-label="{{ description }}">
81
+ {{#each b in g.bands}}
82
+ <path d="{{ b.path }}" fill="{{ b.color }}" fill-opacity="0.5" />
83
+ {{/each}}
84
+ <path d="{{ g.track }}" fill="currentColor" fill-opacity="0.1" />
85
+ {{#if g.valuePath}}
86
+ <path d="{{ g.valuePath }}" fill="{{ g.color }}" style="transition: d .4s ease" />
87
+ {{/if}}
88
+ {{#if needle}}
89
+ <line x1="{{ g.cx }}" y1="{{ g.cy }}" x2="{{ g.needle.x }}" y2="{{ g.needle.y }}" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" class="text-zinc-800 dark:text-zinc-200" />
90
+ <circle cx="{{ g.cx }}" cy="{{ g.cy }}" r="5" fill="currentColor" class="text-zinc-800 dark:text-zinc-200" />
91
+ {{/if}}
92
+ <text x="{{ g.cx }}" y="{{ valueY }}" text-anchor="middle" font-weight="600" font-size="{{ g.size * 0.16 }}" fill="currentColor" class="text-zinc-900 dark:text-zinc-100">{{ g.label }}</text>
93
+ {{#if caption}}
94
+ <text x="{{ g.cx }}" y="{{ valueY + g.size * 0.1 }}" text-anchor="middle" fill="currentColor">{{ caption }}</text>
95
+ {{/if}}
96
+ <text x="{{ ends.lo.x }}" y="{{ ends.lo.y }}" text-anchor="middle" fill="currentColor" fill-opacity="0.65">{{ g.min }}</text>
97
+ <text x="{{ ends.hi.x }}" y="{{ ends.hi.y }}" text-anchor="middle" fill="currentColor" fill-opacity="0.65">{{ g.max }}</text>
98
+ </svg>
99
+ {{#else}}
100
+ <div class="w-full aspect-[4/3]"></div>
101
+ {{/if}}
102
+ </div>
@@ -0,0 +1,148 @@
1
+ component Heatmap
2
+
3
+ props
4
+ data: Array
5
+ label?: string
6
+ from?: string
7
+ to?: string
8
+ weekStart?: number
9
+ cell?: number
10
+ levels?: number
11
+ max?: number
12
+ color?: string
13
+ legend?: boolean
14
+ formatValue?: Function
15
+ class?: string
16
+
17
+ state
18
+ hover: null
19
+ ready: false
20
+
21
+ computed
22
+ h() {
23
+ if not this.ready {
24
+ return null
25
+ }
26
+ return window.__tylixCharts.calendarHeatmap(this.data ?? [], {
27
+ from: this.from,
28
+ to: this.to,
29
+ weekStart: this.weekStart ?? 0,
30
+ cell: this.cell ?? 11,
31
+ levels: this.levels ?? 5,
32
+ max: this.max,
33
+ })
34
+ }
35
+
36
+ height() {
37
+ return this.h.height + (this.legend is false ? 0 : 24)
38
+ }
39
+
40
+ fill() {
41
+ return this.color ?? "var(--tylix-chart-1, #2563eb)"
42
+ }
43
+
44
+ legendCells() {
45
+ const h = this.h
46
+ const step = h.cell + 3
47
+ const x0 = h.width - h.levels * step - 34
48
+ return [...Array(h.levels).keys()].map(level => ({ level: level, x: x0 + level * step }))
49
+ }
50
+
51
+ tip() {
52
+ const c = this.hover
53
+ if not c {
54
+ return null
55
+ }
56
+ const text = `${this.fv(c.value)} on ${c.date}`
57
+ const w = Math.ceil(text.length * 6.2 + 20)
58
+ let x = c.x + this.h.cell / 2 - w / 2
59
+ x = Math.min(this.h.width - w - 2, Math.max(2, x))
60
+ return { x: x, y: Math.max(2, c.y - 30), w: w, text: text }
61
+ }
62
+
63
+ description() {
64
+ const h = this.h
65
+ return `${this.label ?? "Activity"}: ${h.cells.length} days, total ${this.fv(h.total)}`
66
+ }
67
+
68
+ action
69
+ fv(v) {
70
+ return this.formatValue ? this.formatValue(v) : String(v)
71
+ }
72
+
73
+ opacity(level) {
74
+ return level is 0 ? 0.08 : 0.2 + 0.8 * level / (this.h.levels - 1)
75
+ }
76
+
77
+ paint(level) {
78
+ return level is 0 ? "currentColor" : this.fill
79
+ }
80
+
81
+ focusCell(c) {
82
+ this.hover = c
83
+ }
84
+
85
+ leave() {
86
+ this.hover = null
87
+ }
88
+
89
+ key(e) {
90
+ if not this.hover {
91
+ return null
92
+ }
93
+ const cells = this.h.cells
94
+ const i = cells.findIndex(c => c.date is this.hover.date)
95
+ const moves = { ArrowRight: 7, ArrowLeft: -7, ArrowDown: 1, ArrowUp: -1 }
96
+ if not (e.key in moves) {
97
+ return null
98
+ }
99
+ e.preventDefault()
100
+ const next = cells[i + moves[e.key]]
101
+ if next {
102
+ this.hover = next
103
+ }
104
+ }
105
+
106
+ onMount
107
+ async {
108
+ const load = window.__tylixChartsLoad ?? (window.__tylixChartsLoad = import("/_tylix/charts/index.js").then(m => { window.__tylixCharts = m }))
109
+ try {
110
+ await load
111
+ this.ready = true
112
+ } catch (e) {
113
+ console.error("[tylix/charts] could not load geometry", e)
114
+ window.__tylixChartsLoad = null
115
+ }
116
+ }
117
+
118
+ template
119
+ <div class="{{ `tylix-chart text-zinc-500 dark:text-zinc-400 ${this.class ?? ''}` }}">
120
+ {{#if ready}}
121
+ <svg viewBox="{{ `0 0 ${h.width} ${height}` }}" class="w-full h-auto outline-none" font-size="10" role="img" aria-label="{{ description }}" tabindex="0" onpointerleave="{{ leave }}" onblur="{{ leave }}" onkeydown="{{ key }}" onfocus="{{ hover = hover ?? h.cells.at(-1) }}">
122
+ {{#each m in h.months}}
123
+ <text x="{{ m.x }}" y="10" fill="currentColor" fill-opacity="0.65">{{ m.label }}</text>
124
+ {{/each}}
125
+ {{#each d in h.days}}
126
+ <text x="0" y="{{ d.y }}" dominant-baseline="middle" fill="currentColor" fill-opacity="0.65">{{ d.label }}</text>
127
+ {{/each}}
128
+ {{#each c in h.cells}}
129
+ <rect x="{{ c.x }}" y="{{ c.y }}" width="{{ h.cell }}" height="{{ h.cell }}" rx="2" fill="{{ paint(c.level) }}" fill-opacity="{{ opacity(c.level) }}" stroke="currentColor" stroke-opacity="{{ hover and hover.date is c.date ? 0.9 : 0 }}" onpointerenter="{{ focusCell(c) }}" />
130
+ {{/each}}
131
+ {{#if legend is not false}}
132
+ <text x="{{ legendCells[0].x - 6 }}" y="{{ h.height + 10 + h.cell - 2 }}" text-anchor="end" fill="currentColor" fill-opacity="0.65">Less</text>
133
+ {{#each l in legendCells}}
134
+ <rect x="{{ l.x }}" y="{{ h.height + 10 }}" width="{{ h.cell }}" height="{{ h.cell }}" rx="2" fill="{{ paint(l.level) }}" fill-opacity="{{ opacity(l.level) }}" />
135
+ {{/each}}
136
+ <text x="{{ legendCells.at(-1).x + h.cell + 6 }}" y="{{ h.height + 10 + h.cell - 2 }}" fill="currentColor" fill-opacity="0.65">More</text>
137
+ {{/if}}
138
+ {{#if tip}}
139
+ <g pointer-events="none" transform="{{ `translate(${tip.x},${tip.y})` }}">
140
+ <rect width="{{ tip.w }}" height="22" rx="5" fill="#18181b" />
141
+ <text x="{{ tip.w / 2 }}" y="15" text-anchor="middle" fill="#fff">{{ tip.text }}</text>
142
+ </g>
143
+ {{/if}}
144
+ </svg>
145
+ {{#else}}
146
+ <div class="w-full aspect-[5/1]"></div>
147
+ {{/if}}
148
+ </div>
@@ -98,6 +98,21 @@ computed
98
98
  return this.chart.series.length > 1
99
99
  }
100
100
 
101
+ table() {
102
+ if not this.ready {
103
+ return ""
104
+ }
105
+ return window.__tylixCharts.dataTable(this.series ?? [], {
106
+ caption: this.label ?? "Chart data",
107
+ formatX: v => this.fx(v instanceof Date ? v.getTime() : v),
108
+ formatY: v => this.fy(v),
109
+ })
110
+ }
111
+
112
+ srOnly() {
113
+ return window.__tylixCharts ? window.__tylixCharts.SR_ONLY_STYLE : "position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0)"
114
+ }
115
+
101
116
  tip() {
102
117
  if not this.ready {
103
118
  return null
@@ -136,7 +151,10 @@ computed
136
151
  action
137
152
  fx(v) {
138
153
  if this.formatX {
139
- return this.formatX(v)
154
+ return this.formatX(this.chart and this.chart.time ? new Date(v) : v)
155
+ }
156
+ if this.chart {
157
+ return this.chart.formatX(v)
140
158
  }
141
159
  return String(v)
142
160
  }
@@ -260,7 +278,7 @@ template
260
278
  {{/each}}
261
279
 
262
280
  {{#each t in chart.xTicks}}
263
- <text x="{{ t.x }}" y="{{ chart.plot.y + chart.plot.height + 16 }}" text-anchor="middle" fill="currentColor" fill-opacity="0.65">{{ t.label }}</text>
281
+ <text x="{{ t.x }}" y="{{ chart.plot.y + chart.plot.height + 16 }}" text-anchor="{{ t.anchor ?? 'middle' }}" fill="currentColor" fill-opacity="0.65">{{ t.label }}</text>
264
282
  {{/each}}
265
283
 
266
284
  {{#each s in layers}}
@@ -291,6 +309,7 @@ template
291
309
  </g>
292
310
  {{/if}}
293
311
  </svg>
312
+ <div style="{{ srOnly }}">{{@html table}}</div>
294
313
  {{#else}}
295
314
  <div class="w-full" style="{{ ratio }}"></div>
296
315
  {{/if}}
@@ -0,0 +1,162 @@
1
+ component ScatterChart
2
+
3
+ props
4
+ series: Array
5
+ id?: string
6
+ label?: string
7
+ width?: number
8
+ height?: number
9
+ radius?: Array
10
+ legend?: boolean
11
+ xDomain?: Array
12
+ yDomain?: Array
13
+ xTicks?: number
14
+ yTicks?: number
15
+ formatX?: Function
16
+ formatY?: Function
17
+ class?: string
18
+
19
+ state
20
+ hover: null
21
+ ready: false
22
+
23
+ computed
24
+ rootClass() {
25
+ return `tylix-chart relative text-zinc-500 dark:text-zinc-400 [--tylix-chart-bg:#fff] dark:[--tylix-chart-bg:#09090b] ${this.class ?? ""}`
26
+ }
27
+
28
+ ratio() {
29
+ return `aspect-ratio:${this.width ?? 480} / ${this.height ?? 280}`
30
+ }
31
+
32
+ chart() {
33
+ if not this.ready {
34
+ return null
35
+ }
36
+ return window.__tylixCharts.scatterChart(this.series ?? [], {
37
+ width: this.width ?? 480,
38
+ height: this.height ?? 280,
39
+ radius: this.radius ?? [4, 22],
40
+ xDomain: this.xDomain,
41
+ yDomain: this.yDomain,
42
+ xTicks: this.xTicks ?? 6,
43
+ yTicks: this.yTicks ?? 5,
44
+ formatX: this.formatX,
45
+ formatY: this.formatY ?? (v => String(v)),
46
+ })
47
+ }
48
+
49
+ showLegend() {
50
+ if not this.ready {
51
+ return false
52
+ }
53
+ if this.legend is false {
54
+ return false
55
+ }
56
+ return this.chart.series.length > 1
57
+ }
58
+
59
+ tip() {
60
+ const h = this.hover
61
+ if not h {
62
+ return null
63
+ }
64
+ const head = h.label ?? h.name
65
+ const line = `${this.chart.formatX(h.x)}, ${this.chart.formatY(h.y)}${h.size is null ? "" : ` · ${h.size}`}`
66
+ const w = Math.ceil(Math.max(String(head).length, line.length) * 6.2 + 24)
67
+ let x = h.px + h.r + 8
68
+ if x + w > this.chart.width - 4 {
69
+ x = h.px - h.r - 8 - w
70
+ }
71
+ return { x: Math.max(4, x), y: Math.max(4, h.py - 22), w: w, head: head, line: line }
72
+ }
73
+
74
+ table() {
75
+ if not this.ready {
76
+ return ""
77
+ }
78
+ const rows = this.chart.series.flatMap(s => s.points.map(p => ({ name: s.name, data: [{ x: p.label ?? this.chart.formatX(p.x), y: p.y }] })))
79
+ return window.__tylixCharts.dataTable(rows, { caption: this.label ?? "Scatter chart data" })
80
+ }
81
+
82
+ srOnly() {
83
+ return window.__tylixCharts ? window.__tylixCharts.SR_ONLY_STYLE : ""
84
+ }
85
+
86
+ action
87
+ move(e) {
88
+ if not this.ready {
89
+ return null
90
+ }
91
+ const r = e.currentTarget.getBoundingClientRect()
92
+ if r.width is 0 {
93
+ return null
94
+ }
95
+ const k = this.chart.width / r.width
96
+ this.hover = window.__tylixCharts.nearestScatterPoint(this.chart, (e.clientX - r.left) * k, (e.clientY - r.top) * k)
97
+ }
98
+
99
+ leave() {
100
+ this.hover = null
101
+ }
102
+
103
+ onMount
104
+ async {
105
+ const load = window.__tylixChartsLoad ?? (window.__tylixChartsLoad = import("/_tylix/charts/index.js").then(m => { window.__tylixCharts = m }))
106
+ try {
107
+ await load
108
+ this.ready = true
109
+ } catch (e) {
110
+ console.error("[tylix/charts] could not load geometry", e)
111
+ window.__tylixChartsLoad = null
112
+ }
113
+ }
114
+
115
+ template
116
+ <div class="{{ rootClass }}">
117
+ {{#if ready}}
118
+ {{#if showLegend}}
119
+ <div class="flex flex-wrap gap-x-4 gap-y-1 mb-2 text-xs text-zinc-600 dark:text-zinc-300">
120
+ {{#each s in chart.series}}
121
+ <span class="inline-flex items-center gap-1.5">
122
+ <span class="w-2.5 h-2.5 rounded-full" style:background="{{ s.color }}"></span>{{ s.name }}
123
+ </span>
124
+ {{/each}}
125
+ </div>
126
+ {{/if}}
127
+ <svg
128
+ viewBox="{{ `0 0 ${chart.width} ${chart.height}` }}"
129
+ class="w-full h-auto"
130
+ font-size="11"
131
+ role="img"
132
+ aria-label="{{ label or 'Scatter chart' }}"
133
+ onpointermove="{{ move }}"
134
+ onpointerdown="{{ move }}"
135
+ onpointerleave="{{ leave }}"
136
+ >
137
+ {{#each t in chart.yTicks}}
138
+ <line x1="{{ chart.plot.x }}" x2="{{ chart.plot.x + chart.plot.width }}" y1="{{ t.y }}" y2="{{ t.y }}" stroke="currentColor" stroke-opacity="0.1" stroke-dasharray="3 4" />
139
+ <text x="{{ chart.plot.x - 8 }}" y="{{ t.y }}" text-anchor="end" dominant-baseline="middle" fill="currentColor" fill-opacity="0.65">{{ t.label }}</text>
140
+ {{/each}}
141
+ {{#each t in chart.xTicks}}
142
+ <text x="{{ t.x }}" y="{{ chart.plot.y + chart.plot.height + 16 }}" text-anchor="{{ t.anchor ?? 'middle' }}" fill="currentColor" fill-opacity="0.65">{{ t.label }}</text>
143
+ {{/each}}
144
+ {{#each s in chart.series}}
145
+ {{#each p in s.points}}
146
+ <circle cx="{{ p.px }}" cy="{{ p.py }}" r="{{ p.r }}" fill="{{ s.color }}" fill-opacity="{{ p.size is null ? 0.85 : 0.5 }}" stroke="{{ s.color }}" stroke-width="{{ hover and hover.px is p.px and hover.py is p.py ? 2.5 : 1 }}" />
147
+ {{/each}}
148
+ {{/each}}
149
+ {{#if tip}}
150
+ <g pointer-events="none" transform="{{ `translate(${tip.x},${tip.y})` }}">
151
+ <rect width="{{ tip.w }}" height="40" rx="6" fill="var(--tylix-chart-bg, #fff)" stroke="currentColor" stroke-opacity="0.25" />
152
+ <circle cx="12" cy="13" r="3.5" fill="{{ hover.color }}" />
153
+ <text x="22" y="17" font-weight="600" fill="currentColor" class="text-zinc-900 dark:text-zinc-100">{{ tip.head }}</text>
154
+ <text x="10" y="32" fill="currentColor" fill-opacity="0.7">{{ tip.line }}</text>
155
+ </g>
156
+ {{/if}}
157
+ </svg>
158
+ <div style="{{ srOnly }}">{{@html table}}</div>
159
+ {{#else}}
160
+ <div class="w-full" style="{{ ratio }}"></div>
161
+ {{/if}}
162
+ </div>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tylix/charts",
3
- "version": "1.2.0",
4
- "description": "Hand-rolled SVG charts for Tylix: sparkline, line, area, bar, pie and donut",
3
+ "version": "1.4.0",
4
+ "description": "SVG charts for Tylix .tyx pages, React, Vue, Svelte or plain JS: line, area, bar, pie, donut, scatter/bubble, gauge, calendar heatmap and sparklines, with time axes, tooltips and accessible output. No dependencies.",
5
5
  "license": "MIT",
6
6
  "author": "Mbiyrukzz",
7
7
  "type": "module",
@@ -29,6 +29,19 @@
29
29
  ".": {
30
30
  "types": "./src/index.d.ts",
31
31
  "default": "./src/index.js"
32
+ },
33
+ "./react": {
34
+ "types": "./src/frameworks/react.d.ts",
35
+ "default": "./src/frameworks/react.js"
36
+ },
37
+ "./vue": {
38
+ "types": "./src/frameworks/vue.d.ts",
39
+ "default": "./src/frameworks/vue.js"
40
+ },
41
+ "./svelte": {
42
+ "types": "./src/frameworks/svelte/index.d.ts",
43
+ "svelte": "./src/frameworks/svelte/index.js",
44
+ "default": "./src/frameworks/svelte/index.js"
32
45
  }
33
46
  },
34
47
  "files": [
@@ -44,6 +57,26 @@
44
57
  "access": "public"
45
58
  },
46
59
  "scripts": {
47
- "test": "node --test"
60
+ "test": "node --test test/*.test.js"
61
+ },
62
+ "devDependencies": {
63
+ "@tylix/compiler": "^1.4.0",
64
+ "jsdom": "^29.1.1"
65
+ },
66
+ "peerDependencies": {
67
+ "react": ">=18",
68
+ "vue": ">=3.3",
69
+ "svelte": ">=5.20"
70
+ },
71
+ "peerDependenciesMeta": {
72
+ "react": {
73
+ "optional": true
74
+ },
75
+ "vue": {
76
+ "optional": true
77
+ },
78
+ "svelte": {
79
+ "optional": true
80
+ }
48
81
  }
49
82
  }