-
Notifications
You must be signed in to change notification settings - Fork 3
/
vega.nu
145 lines (140 loc) · 3.7 KB
/
vega.nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
export-env {
$env.vega_view_bin = ($env.FILE_PWD | path join "target" "release" "vega-view")
}
# Display a Vega visualization of the input data in a new window.
export def view [
spec: record # a vega-lite specification
--title: string = "Vega View" # title for the window
--width: number = 1000 # width of the window
--height: number = 800 # height of the window
] {
to json | ^$env.vega_view_bin --title $title --width $width --height $height ($spec | upsert data { url: "/data"} | to json)
}
# vega-lite specification for a bar graph
export def bar [
value: string # field name for the bar height
--category: string # field to discriminate different bars
--subcategory: string # field to discriminate stacked bar sections
--aggregate: string = 'sum' # how to combine values for a bar or bar section
] {
{
'$schema': 'https://vega.github.io/schema/vega-lite/v5.json',
mark: { 'type': 'bar', tooltip: true } ,
"width": "container",
encoding: {
...(
if $category != null {
{
x: { field: $category, type: 'nominal' }
}
} else {
{}
}
),
y: {
aggregate: $aggregate,
field: $value,
type: 'quantitative',
axis: {
title: $"($aggregate) of ($value)"
}
}
...(
if $subcategory != null {
{
"color": {
"field": $subcategory,
"type": "nominal",
}
}
} else {
{}
}
)
},
}
}
# vega-lite specification for a time series plot
export def series [
value: string # field name for the series values
time: string # field for time values
--category: string # field to discriminate different series
--area # render as a stacked area plot
] {
let mark = if $area { "area" } else { "line" }
{
'$schema': 'https://vega.github.io/schema/vega-lite/v5.json',
mark: {
type: $mark,
tooltip: true
},
"width": "container",
encoding: {
x: {
field: $time,
type: 'temporal'
},
y: {
field: $value,
type: 'quantitative',
},
...(
if $category != null {
{
"color": {
"field": $category,
"type": "nominal",
}
}
} else {
{}
}
)
}
}
}
# vega-lite specification for a scatter plot
export def scatter [
value: string # field name for the y coodinate of a point in the plot
domain: string # field for the x coodinate of a point in the plot
--category: string # field for the category of the point in the plot
] {
{
'$schema': 'https://vega.github.io/schema/vega-lite/v5.json',
mark: { 'type': 'point', tooltip: true } ,
"width": "container",
encoding: {
x: {
field: $domain,
type: 'quantitative',
},
y: {
field: $value,
type: 'quantitative',
}
...(
if $category != null {
{
"color": {
"field": $category,
"type": "nominal",
},
"shape": {
"field": $category,
"type": "nominal",
}
}
} else {
{}
}
)
},
}
}
# swap the x and y axis of a vega-lite specification
export def flip [] {
mut spec = $in
let x = $spec.encoding.x?
let y = $spec.encoding.y?
$spec | upsert encoding.y $x | upsert encoding.x $y
}