@tylix/charts 0.5.1 → 0.7.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/LICENSE +21 -0
- package/components/AreaChart.tyx +40 -0
- package/components/BarChart.tyx +159 -0
- package/components/Donut.tyx +37 -0
- package/components/LineChart.tyx +297 -0
- package/components/PieChart.tyx +118 -0
- package/components/Sparkline.tyx +57 -0
- package/package.json +32 -7
- package/src/area.js +119 -0
- package/src/band.js +37 -0
- package/src/bar.js +218 -0
- package/src/crosshair.js +162 -0
- package/src/index.d.ts +292 -6
- package/src/index.js +14 -2
- package/src/line.js +1 -1
- package/src/path.js +92 -16
- package/src/pie.js +187 -0
- package/src/svg.js +380 -25
- package/tylix-components.json +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mbiyrukzz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
component AreaChart
|
|
2
|
+
|
|
3
|
+
import LineChart from "./LineChart"
|
|
4
|
+
|
|
5
|
+
props
|
|
6
|
+
series: Array
|
|
7
|
+
id?: string
|
|
8
|
+
label?: string
|
|
9
|
+
width?: number
|
|
10
|
+
height?: number
|
|
11
|
+
curve?: string
|
|
12
|
+
stacked?: boolean
|
|
13
|
+
legend?: boolean
|
|
14
|
+
crosshair?: boolean
|
|
15
|
+
yDomain?: Array
|
|
16
|
+
xTicks?: number
|
|
17
|
+
yTicks?: number
|
|
18
|
+
formatX?: Function
|
|
19
|
+
formatY?: Function
|
|
20
|
+
class?: string
|
|
21
|
+
|
|
22
|
+
template
|
|
23
|
+
<LineChart
|
|
24
|
+
series="{{ series }}"
|
|
25
|
+
id="{{ id }}"
|
|
26
|
+
label="{{ label or 'Area chart' }}"
|
|
27
|
+
width="{{ width }}"
|
|
28
|
+
height="{{ height }}"
|
|
29
|
+
curve="{{ curve }}"
|
|
30
|
+
area="{{ true }}"
|
|
31
|
+
stacked="{{ stacked }}"
|
|
32
|
+
legend="{{ legend }}"
|
|
33
|
+
crosshair="{{ crosshair }}"
|
|
34
|
+
yDomain="{{ yDomain }}"
|
|
35
|
+
xTicks="{{ xTicks }}"
|
|
36
|
+
yTicks="{{ yTicks }}"
|
|
37
|
+
formatX="{{ formatX }}"
|
|
38
|
+
formatY="{{ formatY }}"
|
|
39
|
+
class="{{ class }}"
|
|
40
|
+
/>
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
component BarChart
|
|
2
|
+
|
|
3
|
+
props
|
|
4
|
+
series: Array
|
|
5
|
+
categories?: Array
|
|
6
|
+
label?: string
|
|
7
|
+
width?: number
|
|
8
|
+
height?: number
|
|
9
|
+
orientation?: string
|
|
10
|
+
stacked?: boolean
|
|
11
|
+
legend?: boolean
|
|
12
|
+
radius?: number
|
|
13
|
+
padding?: number
|
|
14
|
+
groupPadding?: number
|
|
15
|
+
yDomain?: Array
|
|
16
|
+
yTicks?: number
|
|
17
|
+
formatValue?: Function
|
|
18
|
+
formatCategory?: Function
|
|
19
|
+
class?: string
|
|
20
|
+
|
|
21
|
+
state
|
|
22
|
+
ready: false
|
|
23
|
+
|
|
24
|
+
computed
|
|
25
|
+
rootClass() {
|
|
26
|
+
return `tylix-chart text-zinc-500 dark:text-zinc-400 ${this.class ?? ""}`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
ratio() {
|
|
30
|
+
return `aspect-ratio:${this.width ?? 480} / ${this.height ?? 240}`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
horizontal() {
|
|
34
|
+
return this.orientation is "horizontal"
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
chart() {
|
|
38
|
+
if not this.ready {
|
|
39
|
+
return null
|
|
40
|
+
}
|
|
41
|
+
return window.__tylixCharts.barChart(this.series ?? [], {
|
|
42
|
+
categories: this.categories,
|
|
43
|
+
orientation: this.orientation ?? "vertical",
|
|
44
|
+
stacked: this.stacked ?? false,
|
|
45
|
+
width: this.width ?? 480,
|
|
46
|
+
height: this.height ?? 240,
|
|
47
|
+
yDomain: this.yDomain,
|
|
48
|
+
yTicks: this.yTicks ?? 5,
|
|
49
|
+
radius: this.radius ?? 4,
|
|
50
|
+
padding: this.padding ?? 0.3,
|
|
51
|
+
groupPadding: this.groupPadding ?? 0.08,
|
|
52
|
+
formatValue: this.formatValue,
|
|
53
|
+
formatCategory: this.formatCategory,
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
showLegend() {
|
|
58
|
+
if not this.ready {
|
|
59
|
+
return false
|
|
60
|
+
}
|
|
61
|
+
if this.legend is false {
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
return this.chart.series.length > 1
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Vertical charts drop labels that would collide.
|
|
68
|
+
catTicks() {
|
|
69
|
+
if not this.ready {
|
|
70
|
+
return []
|
|
71
|
+
}
|
|
72
|
+
const ticks = this.chart.categoryTicks
|
|
73
|
+
if this.horizontal {
|
|
74
|
+
return ticks
|
|
75
|
+
}
|
|
76
|
+
const step = this.chart.categoryScale.step()
|
|
77
|
+
let longest = 1
|
|
78
|
+
ticks.forEach(t => {
|
|
79
|
+
longest = Math.max(longest, t.label.length)
|
|
80
|
+
})
|
|
81
|
+
const skip = Math.max(1, Math.ceil((longest * 6.2 + 6) / (step > 0 ? step : 1)))
|
|
82
|
+
return ticks.filter(t => (t.index % skip) is 0)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
bars() {
|
|
86
|
+
if not this.ready {
|
|
87
|
+
return []
|
|
88
|
+
}
|
|
89
|
+
const single = this.chart.series.length is 1
|
|
90
|
+
const out = []
|
|
91
|
+
this.chart.series.forEach(s => {
|
|
92
|
+
s.bars.forEach(b => {
|
|
93
|
+
if b.path {
|
|
94
|
+
out.push({
|
|
95
|
+
d: b.path,
|
|
96
|
+
color: s.color,
|
|
97
|
+
tip: single ? `${b.categoryLabel}: ${b.label}` : `${b.categoryLabel} · ${s.name}: ${b.label}`,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
return out
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
onMount
|
|
106
|
+
async {
|
|
107
|
+
const load = window.__tylixChartsLoad ?? (window.__tylixChartsLoad = import("/_tylix/charts/index.js").then(m => { window.__tylixCharts = m }))
|
|
108
|
+
try {
|
|
109
|
+
await load
|
|
110
|
+
this.ready = true
|
|
111
|
+
} catch (e) {
|
|
112
|
+
console.error("[tylix/charts] could not load geometry", e)
|
|
113
|
+
window.__tylixChartsLoad = null
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
template
|
|
118
|
+
<div class="{{ rootClass }}">
|
|
119
|
+
{{#if ready}}
|
|
120
|
+
{{#if showLegend}}
|
|
121
|
+
<div class="flex flex-wrap gap-x-4 gap-y-1 mb-2 text-xs text-zinc-600 dark:text-zinc-300">
|
|
122
|
+
{{#each s in chart.series}}
|
|
123
|
+
<span class="inline-flex items-center gap-1.5">
|
|
124
|
+
<span class="w-2.5 h-2.5 rounded-[3px]" style="{{ `background:${s.color}` }}"></span>{{ s.name }}
|
|
125
|
+
</span>
|
|
126
|
+
{{/each}}
|
|
127
|
+
</div>
|
|
128
|
+
{{/if}}
|
|
129
|
+
<svg viewBox="{{ `0 0 ${chart.width} ${chart.height}` }}" class="w-full h-auto" font-size="11" role="img" aria-label="{{ label or 'Bar chart' }}">
|
|
130
|
+
{{#if horizontal}}
|
|
131
|
+
{{#each t in chart.valueTicks}}
|
|
132
|
+
<line x1="{{ t.pos }}" x2="{{ t.pos }}" y1="{{ chart.plot.y }}" y2="{{ chart.plot.y + chart.plot.height }}" stroke="currentColor" stroke-opacity="0.1" stroke-dasharray="3 4" />
|
|
133
|
+
<text x="{{ t.pos }}" y="{{ chart.plot.y + chart.plot.height + 16 }}" text-anchor="middle" fill="currentColor" fill-opacity="0.65">{{ t.label }}</text>
|
|
134
|
+
{{/each}}
|
|
135
|
+
<line x1="{{ chart.baseline }}" x2="{{ chart.baseline }}" y1="{{ chart.plot.y }}" y2="{{ chart.plot.y + chart.plot.height }}" stroke="currentColor" stroke-opacity="0.3" />
|
|
136
|
+
{{#each t in catTicks}}
|
|
137
|
+
<text x="{{ chart.plot.x - 8 }}" y="{{ t.pos }}" text-anchor="end" dominant-baseline="middle" fill="currentColor" fill-opacity="0.65">{{ t.label }}</text>
|
|
138
|
+
{{/each}}
|
|
139
|
+
{{#else}}
|
|
140
|
+
{{#each t in chart.valueTicks}}
|
|
141
|
+
<line x1="{{ chart.plot.x }}" x2="{{ chart.plot.x + chart.plot.width }}" y1="{{ t.pos }}" y2="{{ t.pos }}" stroke="currentColor" stroke-opacity="0.1" stroke-dasharray="3 4" />
|
|
142
|
+
<text x="{{ chart.plot.x - 8 }}" y="{{ t.pos }}" text-anchor="end" dominant-baseline="middle" fill="currentColor" fill-opacity="0.65">{{ t.label }}</text>
|
|
143
|
+
{{/each}}
|
|
144
|
+
<line x1="{{ chart.plot.x }}" x2="{{ chart.plot.x + chart.plot.width }}" y1="{{ chart.baseline }}" y2="{{ chart.baseline }}" stroke="currentColor" stroke-opacity="0.3" />
|
|
145
|
+
{{#each t in catTicks}}
|
|
146
|
+
<text x="{{ t.pos }}" y="{{ chart.plot.y + chart.plot.height + 16 }}" text-anchor="middle" fill="currentColor" fill-opacity="0.65">{{ t.label }}</text>
|
|
147
|
+
{{/each}}
|
|
148
|
+
{{/if}}
|
|
149
|
+
|
|
150
|
+
{{#each b in bars}}
|
|
151
|
+
<path d="{{ b.d }}" fill="{{ b.color }}" class="opacity-90 hover:opacity-100 transition-opacity">
|
|
152
|
+
<title>{{ b.tip }}</title>
|
|
153
|
+
</path>
|
|
154
|
+
{{/each}}
|
|
155
|
+
</svg>
|
|
156
|
+
{{#else}}
|
|
157
|
+
<div class="w-full" style="{{ ratio }}"></div>
|
|
158
|
+
{{/if}}
|
|
159
|
+
</div>
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
component Donut
|
|
2
|
+
|
|
3
|
+
import PieChart from "./PieChart"
|
|
4
|
+
|
|
5
|
+
props
|
|
6
|
+
data: Array
|
|
7
|
+
label?: string
|
|
8
|
+
size?: number
|
|
9
|
+
inner?: number
|
|
10
|
+
cornerRadius?: number
|
|
11
|
+
gap?: number
|
|
12
|
+
startAngle?: number
|
|
13
|
+
sort?: string
|
|
14
|
+
legend?: string
|
|
15
|
+
centerLabel?: string
|
|
16
|
+
centerSubLabel?: string
|
|
17
|
+
showCenter?: boolean
|
|
18
|
+
formatValue?: Function
|
|
19
|
+
class?: string
|
|
20
|
+
|
|
21
|
+
template
|
|
22
|
+
<PieChart
|
|
23
|
+
data="{{ data }}"
|
|
24
|
+
label="{{ label or 'Donut chart' }}"
|
|
25
|
+
size="{{ size }}"
|
|
26
|
+
inner="{{ inner or 0.62 }}"
|
|
27
|
+
cornerRadius="{{ cornerRadius }}"
|
|
28
|
+
gap="{{ gap }}"
|
|
29
|
+
startAngle="{{ startAngle }}"
|
|
30
|
+
sort="{{ sort }}"
|
|
31
|
+
legend="{{ legend }}"
|
|
32
|
+
centerLabel="{{ centerLabel }}"
|
|
33
|
+
centerSubLabel="{{ centerSubLabel }}"
|
|
34
|
+
showCenter="{{ showCenter }}"
|
|
35
|
+
formatValue="{{ formatValue }}"
|
|
36
|
+
class="{{ class }}"
|
|
37
|
+
/>
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
component LineChart
|
|
2
|
+
|
|
3
|
+
props
|
|
4
|
+
series: Array
|
|
5
|
+
id?: string
|
|
6
|
+
label?: string
|
|
7
|
+
width?: number
|
|
8
|
+
height?: number
|
|
9
|
+
curve?: string
|
|
10
|
+
area?: boolean
|
|
11
|
+
stacked?: boolean
|
|
12
|
+
legend?: boolean
|
|
13
|
+
crosshair?: boolean
|
|
14
|
+
yDomain?: Array
|
|
15
|
+
xTicks?: number
|
|
16
|
+
yTicks?: number
|
|
17
|
+
zeroBaseline?: boolean
|
|
18
|
+
formatX?: Function
|
|
19
|
+
formatY?: Function
|
|
20
|
+
class?: string
|
|
21
|
+
|
|
22
|
+
state
|
|
23
|
+
hover: null
|
|
24
|
+
ready: false
|
|
25
|
+
|
|
26
|
+
computed
|
|
27
|
+
uid() {
|
|
28
|
+
return this.id ?? "tylix-chart"
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
rootClass() {
|
|
32
|
+
return `tylix-chart text-zinc-500 dark:text-zinc-400 [--tylix-chart-bg:#fff] dark:[--tylix-chart-bg:#09090b] ${this.class ?? ""}`
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
ratio() {
|
|
36
|
+
return `aspect-ratio:${this.width ?? 480} / ${this.height ?? 240}`
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
chart() {
|
|
40
|
+
if not this.ready {
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
const { lineChart, areaChart } = window.__tylixCharts
|
|
44
|
+
const options = {
|
|
45
|
+
width: this.width ?? 480,
|
|
46
|
+
height: this.height ?? 240,
|
|
47
|
+
curve: this.curve ?? "smooth",
|
|
48
|
+
stacked: this.stacked ?? false,
|
|
49
|
+
yDomain: this.yDomain,
|
|
50
|
+
xTicks: this.xTicks ?? 6,
|
|
51
|
+
yTicks: this.yTicks ?? 5,
|
|
52
|
+
zeroBaseline: this.zeroBaseline,
|
|
53
|
+
formatX: this.formatX,
|
|
54
|
+
formatY: this.formatY,
|
|
55
|
+
}
|
|
56
|
+
if this.stacked {
|
|
57
|
+
return areaChart(this.series ?? [], options)
|
|
58
|
+
}
|
|
59
|
+
if this.area {
|
|
60
|
+
return areaChart(this.series ?? [], options)
|
|
61
|
+
}
|
|
62
|
+
return lineChart(this.series ?? [], options)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
layers() {
|
|
66
|
+
if not this.ready {
|
|
67
|
+
return []
|
|
68
|
+
}
|
|
69
|
+
const stacked = this.stacked ?? false
|
|
70
|
+
return this.chart.series.map((s, i) => {
|
|
71
|
+
let last = null
|
|
72
|
+
s.points.forEach(p => {
|
|
73
|
+
if p.py is not null {
|
|
74
|
+
last = p
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
const gid = `${this.uid}-g${i}`
|
|
78
|
+
return {
|
|
79
|
+
name: s.name,
|
|
80
|
+
color: s.color,
|
|
81
|
+
path: s.path,
|
|
82
|
+
areaPath: s.areaPath,
|
|
83
|
+
gid: gid,
|
|
84
|
+
fill: stacked ? s.color : `url(#${gid})`,
|
|
85
|
+
fillOpacity: stacked ? 0.8 : 1,
|
|
86
|
+
last: stacked ? null : last,
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
showLegend() {
|
|
92
|
+
if not this.ready {
|
|
93
|
+
return false
|
|
94
|
+
}
|
|
95
|
+
if this.legend is false {
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
return this.chart.series.length > 1
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
tip() {
|
|
102
|
+
if not this.ready {
|
|
103
|
+
return null
|
|
104
|
+
}
|
|
105
|
+
const h = this.hover
|
|
106
|
+
if not h {
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
const chart = this.chart
|
|
110
|
+
const head = this.fx(h.x)
|
|
111
|
+
const rows = h.rows.map((r, i) => ({
|
|
112
|
+
y: 32 + i * 16,
|
|
113
|
+
color: r.color,
|
|
114
|
+
name: r.name,
|
|
115
|
+
value: r.value is null ? "–" : this.fy(r.value),
|
|
116
|
+
}))
|
|
117
|
+
let chars = String(head).length
|
|
118
|
+
rows.forEach(r => {
|
|
119
|
+
chars = Math.max(chars, r.name.length + String(r.value).length + 3)
|
|
120
|
+
})
|
|
121
|
+
const w = Math.ceil(chars * 6.2 + 28)
|
|
122
|
+
let x = h.px + 12
|
|
123
|
+
if x + w > chart.width - 4 {
|
|
124
|
+
x = h.px - 12 - w
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
x: Math.max(4, x),
|
|
128
|
+
y: chart.plot.y + 4,
|
|
129
|
+
w: w,
|
|
130
|
+
h: 26 + rows.length * 16,
|
|
131
|
+
head: head,
|
|
132
|
+
rows: rows,
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
action
|
|
137
|
+
fx(v) {
|
|
138
|
+
if this.formatX {
|
|
139
|
+
return this.formatX(v)
|
|
140
|
+
}
|
|
141
|
+
return String(v)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
fy(v) {
|
|
145
|
+
if this.formatY {
|
|
146
|
+
return this.formatY(v)
|
|
147
|
+
}
|
|
148
|
+
return String(v)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
move(e) {
|
|
152
|
+
if not this.ready {
|
|
153
|
+
return null
|
|
154
|
+
}
|
|
155
|
+
if this.crosshair is false {
|
|
156
|
+
return null
|
|
157
|
+
}
|
|
158
|
+
const r = e.currentTarget.getBoundingClientRect()
|
|
159
|
+
if r.width is 0 {
|
|
160
|
+
return null
|
|
161
|
+
}
|
|
162
|
+
const x = (e.clientX - r.left) * this.chart.width / r.width
|
|
163
|
+
const plot = this.chart.plot
|
|
164
|
+
if x < plot.x {
|
|
165
|
+
this.hover = null
|
|
166
|
+
return null
|
|
167
|
+
}
|
|
168
|
+
if x > plot.x + plot.width {
|
|
169
|
+
this.hover = null
|
|
170
|
+
return null
|
|
171
|
+
}
|
|
172
|
+
this.hover = window.__tylixCharts.nearestPoints(this.chart, x)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
leave() {
|
|
176
|
+
this.hover = null
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
key(e) {
|
|
180
|
+
if not this.ready {
|
|
181
|
+
return null
|
|
182
|
+
}
|
|
183
|
+
if this.crosshair is false {
|
|
184
|
+
return null
|
|
185
|
+
}
|
|
186
|
+
if e.key is "Escape" {
|
|
187
|
+
this.hover = null
|
|
188
|
+
return null
|
|
189
|
+
}
|
|
190
|
+
const right = e.key is "ArrowRight"
|
|
191
|
+
const left = e.key is "ArrowLeft"
|
|
192
|
+
if not right {
|
|
193
|
+
if not left {
|
|
194
|
+
return null
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const { nearestPoints, snapPositions } = window.__tylixCharts
|
|
198
|
+
const xs = snapPositions(this.chart)
|
|
199
|
+
if xs.length is 0 {
|
|
200
|
+
return null
|
|
201
|
+
}
|
|
202
|
+
e.preventDefault()
|
|
203
|
+
let i = right ? 0 : xs.length - 1
|
|
204
|
+
if this.hover {
|
|
205
|
+
i = this.hover.index + (right ? 1 : -1)
|
|
206
|
+
}
|
|
207
|
+
i = Math.min(xs.length - 1, Math.max(0, i))
|
|
208
|
+
this.hover = nearestPoints(this.chart, xs[i])
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
onMount
|
|
212
|
+
async {
|
|
213
|
+
const load = window.__tylixChartsLoad ?? (window.__tylixChartsLoad = import("/_tylix/charts/index.js").then(m => { window.__tylixCharts = m }))
|
|
214
|
+
try {
|
|
215
|
+
await load
|
|
216
|
+
this.ready = true
|
|
217
|
+
} catch (e) {
|
|
218
|
+
console.error("[tylix/charts] could not load geometry", e)
|
|
219
|
+
window.__tylixChartsLoad = null
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
template
|
|
224
|
+
<div class="{{ rootClass }}">
|
|
225
|
+
{{#if ready}}
|
|
226
|
+
{{#if showLegend}}
|
|
227
|
+
<div class="flex flex-wrap gap-x-4 gap-y-1 mb-2 text-xs text-zinc-600 dark:text-zinc-300">
|
|
228
|
+
{{#each s in layers}}
|
|
229
|
+
<span class="inline-flex items-center gap-1.5">
|
|
230
|
+
<span class="w-2.5 h-2.5 rounded-[3px]" style="{{ `background:${s.color}` }}"></span>{{ s.name }}
|
|
231
|
+
</span>
|
|
232
|
+
{{/each}}
|
|
233
|
+
</div>
|
|
234
|
+
{{/if}}
|
|
235
|
+
<svg
|
|
236
|
+
viewBox="{{ `0 0 ${chart.width} ${chart.height}` }}"
|
|
237
|
+
class="w-full h-auto touch-pan-y outline-none"
|
|
238
|
+
font-size="11"
|
|
239
|
+
role="img"
|
|
240
|
+
aria-label="{{ label or 'Chart' }}"
|
|
241
|
+
tabindex="0"
|
|
242
|
+
onpointermove="{{ move }}"
|
|
243
|
+
onpointerdown="{{ move }}"
|
|
244
|
+
onpointerleave="{{ leave }}"
|
|
245
|
+
onblur="{{ leave }}"
|
|
246
|
+
onkeydown="{{ key }}"
|
|
247
|
+
>
|
|
248
|
+
<defs>
|
|
249
|
+
{{#each s in layers}}
|
|
250
|
+
<linearGradient id="{{ s.gid }}" x1="0" y1="0" x2="0" y2="1">
|
|
251
|
+
<stop offset="0" style="{{ `stop-color:${s.color};stop-opacity:0.32` }}" />
|
|
252
|
+
<stop offset="1" style="{{ `stop-color:${s.color};stop-opacity:0.02` }}" />
|
|
253
|
+
</linearGradient>
|
|
254
|
+
{{/each}}
|
|
255
|
+
</defs>
|
|
256
|
+
|
|
257
|
+
{{#each t in chart.yTicks}}
|
|
258
|
+
<line x1="{{ chart.plot.x }}" x2="{{ chart.plot.x + chart.plot.width }}" y1="{{ t.y }}" y2="{{ t.y }}" stroke="currentColor" stroke-opacity='{{ t.value is 0 ? 0.3 : 0.1 }}' stroke-dasharray='{{ t.value is 0 ? "none" : "3 4" }}' />
|
|
259
|
+
<text x="{{ chart.plot.x - 8 }}" y="{{ t.y }}" text-anchor="end" dominant-baseline="middle" fill="currentColor" fill-opacity="0.65">{{ t.label }}</text>
|
|
260
|
+
{{/each}}
|
|
261
|
+
|
|
262
|
+
{{#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>
|
|
264
|
+
{{/each}}
|
|
265
|
+
|
|
266
|
+
{{#each s in layers}}
|
|
267
|
+
{{#if s.areaPath}}
|
|
268
|
+
<path d="{{ s.areaPath }}" fill="{{ s.fill }}" fill-opacity="{{ s.fillOpacity }}" stroke="none" />
|
|
269
|
+
{{/if}}
|
|
270
|
+
<path d="{{ s.path }}" fill="none" stroke="{{ s.color }}" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
|
|
271
|
+
{{#if s.last}}
|
|
272
|
+
<circle cx="{{ s.last.px }}" cy="{{ s.last.py }}" r="4" fill="{{ s.color }}" stroke="var(--tylix-chart-bg, #fff)" stroke-width="2" />
|
|
273
|
+
{{/if}}
|
|
274
|
+
{{/each}}
|
|
275
|
+
|
|
276
|
+
{{#if tip}}
|
|
277
|
+
<g pointer-events="none">
|
|
278
|
+
<line x1="{{ hover.px }}" x2="{{ hover.px }}" y1="{{ chart.plot.y }}" y2="{{ chart.plot.y + chart.plot.height }}" stroke="currentColor" stroke-opacity="0.3" stroke-dasharray="3 3" />
|
|
279
|
+
{{#each r in hover.rows}}
|
|
280
|
+
<circle cx="{{ r.px }}" cy="{{ r.py }}" r="4.5" fill="{{ r.color }}" stroke="var(--tylix-chart-bg, #fff)" stroke-width="2" />
|
|
281
|
+
{{/each}}
|
|
282
|
+
<g transform="{{ `translate(${tip.x},${tip.y})` }}">
|
|
283
|
+
<rect width="{{ tip.w }}" height="{{ tip.h }}" rx="6" class="fill-white dark:fill-zinc-900 stroke-zinc-200 dark:stroke-zinc-700" />
|
|
284
|
+
<text x="10" y="16" font-weight="600" class="fill-zinc-900 dark:fill-zinc-100">{{ tip.head }}</text>
|
|
285
|
+
{{#each r in tip.rows}}
|
|
286
|
+
<circle cx="14" cy="{{ r.y - 4 }}" r="3.5" fill="{{ r.color }}" />
|
|
287
|
+
<text x="24" y="{{ r.y }}" class="fill-zinc-500 dark:fill-zinc-400">{{ r.name }}</text>
|
|
288
|
+
<text x="{{ tip.w - 10 }}" y="{{ r.y }}" text-anchor="end" font-weight="600" class="fill-zinc-900 dark:fill-zinc-100">{{ r.value }}</text>
|
|
289
|
+
{{/each}}
|
|
290
|
+
</g>
|
|
291
|
+
</g>
|
|
292
|
+
{{/if}}
|
|
293
|
+
</svg>
|
|
294
|
+
{{#else}}
|
|
295
|
+
<div class="w-full" style="{{ ratio }}"></div>
|
|
296
|
+
{{/if}}
|
|
297
|
+
</div>
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
component PieChart
|
|
2
|
+
|
|
3
|
+
props
|
|
4
|
+
data: Array
|
|
5
|
+
label?: string
|
|
6
|
+
size?: number
|
|
7
|
+
inner?: number
|
|
8
|
+
cornerRadius?: number
|
|
9
|
+
gap?: number
|
|
10
|
+
startAngle?: number
|
|
11
|
+
sort?: string
|
|
12
|
+
legend?: string
|
|
13
|
+
centerLabel?: string
|
|
14
|
+
centerSubLabel?: string
|
|
15
|
+
showCenter?: boolean
|
|
16
|
+
formatValue?: Function
|
|
17
|
+
class?: string
|
|
18
|
+
|
|
19
|
+
state
|
|
20
|
+
ready: false
|
|
21
|
+
|
|
22
|
+
computed
|
|
23
|
+
box() {
|
|
24
|
+
return this.size ?? 220
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
legendMode() {
|
|
28
|
+
return this.legend ?? "right"
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
rootClass() {
|
|
32
|
+
const dir = this.legendMode is "bottom" ? "flex-col" : "flex-row"
|
|
33
|
+
return `tylix-chart text-zinc-500 dark:text-zinc-400 inline-flex ${dir} items-center gap-6 ${this.class ?? ""}`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
chart() {
|
|
37
|
+
if not this.ready {
|
|
38
|
+
return null
|
|
39
|
+
}
|
|
40
|
+
return window.__tylixCharts.pieChart(this.data ?? [], {
|
|
41
|
+
width: this.box,
|
|
42
|
+
height: this.box,
|
|
43
|
+
innerRadius: this.inner ?? 0,
|
|
44
|
+
cornerRadius: this.cornerRadius ?? 4,
|
|
45
|
+
gap: this.gap ?? 2,
|
|
46
|
+
startAngle: this.startAngle ?? 0,
|
|
47
|
+
sort: this.sort ?? "none",
|
|
48
|
+
formatValue: this.formatValue,
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// The center text only fits when the hole is big enough.
|
|
53
|
+
center() {
|
|
54
|
+
if not this.ready {
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
const hole = this.chart.innerRadius
|
|
58
|
+
if hole < 34 {
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
if this.showCenter is false {
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
const total = this.chart.total
|
|
65
|
+
const big = this.centerLabel ?? (this.formatValue ? this.formatValue(total) : String(total))
|
|
66
|
+
return {
|
|
67
|
+
big: big,
|
|
68
|
+
sub: this.centerSubLabel ?? "Total",
|
|
69
|
+
size: Math.max(14, Math.min(28, hole * 0.5)),
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
onMount
|
|
74
|
+
async {
|
|
75
|
+
const load = window.__tylixChartsLoad ?? (window.__tylixChartsLoad = import("/_tylix/charts/index.js").then(m => { window.__tylixCharts = m }))
|
|
76
|
+
try {
|
|
77
|
+
await load
|
|
78
|
+
this.ready = true
|
|
79
|
+
} catch (e) {
|
|
80
|
+
console.error("[tylix/charts] could not load geometry", e)
|
|
81
|
+
window.__tylixChartsLoad = null
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
template
|
|
86
|
+
<div class="{{ rootClass }}">
|
|
87
|
+
{{#if ready}}
|
|
88
|
+
<svg viewBox="{{ `0 0 ${box} ${box}` }}" style="{{ `width:${box}px;height:${box}px` }}" class="group shrink-0" role="img" aria-label="{{ label or 'Pie chart' }}" font-size="11">
|
|
89
|
+
{{#each s in chart.slices}}
|
|
90
|
+
{{#if s.path}}
|
|
91
|
+
<path d="{{ s.path }}" fill="{{ s.color }}" fill-rule="evenodd" class="transition-opacity group-hover:opacity-45 hover:!opacity-100">
|
|
92
|
+
<title>{{ `${s.label}: ${s.valueLabel} (${s.percentLabel})` }}</title>
|
|
93
|
+
</path>
|
|
94
|
+
{{/if}}
|
|
95
|
+
{{/each}}
|
|
96
|
+
{{#if center}}
|
|
97
|
+
<text x="{{ chart.cx }}" y="{{ chart.cy - 2 }}" text-anchor="middle" font-weight="600" font-size="{{ center.size }}" class="fill-zinc-900 dark:fill-zinc-100">{{ center.big }}</text>
|
|
98
|
+
{{#if center.sub}}
|
|
99
|
+
<text x="{{ chart.cx }}" y="{{ chart.cy + 14 }}" text-anchor="middle" fill="currentColor" fill-opacity="0.65">{{ center.sub }}</text>
|
|
100
|
+
{{/if}}
|
|
101
|
+
{{/if}}
|
|
102
|
+
</svg>
|
|
103
|
+
|
|
104
|
+
{{#if legendMode is not "none"}}
|
|
105
|
+
<ul class="space-y-1.5 text-sm min-w-[10rem]">
|
|
106
|
+
{{#each s in chart.slices}}
|
|
107
|
+
<li class="flex items-center gap-2">
|
|
108
|
+
<span class="w-2.5 h-2.5 rounded-[3px] shrink-0" style="{{ `background:${s.color}` }}"></span>
|
|
109
|
+
<span class="flex-1 text-zinc-700 dark:text-zinc-200 truncate">{{ s.label }}</span>
|
|
110
|
+
<span class="text-xs tabular-nums text-zinc-500">{{ s.percentLabel }}</span>
|
|
111
|
+
</li>
|
|
112
|
+
{{/each}}
|
|
113
|
+
</ul>
|
|
114
|
+
{{/if}}
|
|
115
|
+
{{#else}}
|
|
116
|
+
<div style="{{ `width:${box}px;height:${box}px` }}"></div>
|
|
117
|
+
{{/if}}
|
|
118
|
+
</div>
|