Skip to content

Commit 20f1c57

Browse files
committed
Include only required fields in data
1 parent cd3ca34 commit 20f1c57

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/rendering/render.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,15 @@ end
202202

203203
function Base.display(d::REPL.REPLDisplay, plt::VLSpec{:plot})
204204
# checkplot(plt)
205-
tmppath = writehtml_full(JSON.json(getparams(plt)))
205+
tmppath = writehtml_full(render_json(plt))
206206
launch_browser(tmppath) # Open the browser
207207
end
208208

209209
function Base.display(d::REPL.REPLDisplay, plt::VGSpec)
210210
tmppath = write_vg_html_full(JSON.json(getparams(plt)))
211211
launch_browser(tmppath) # Open the browser
212212
end
213+
214+
render_json(x) = sprint(render_json, x)
215+
render_json(io::IO, plt::VLSpec) =
216+
JSON.print(io, getparams(with_stripped_data(plt)))

src/rendering/show.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ end
2121

2222
function convert_vl_to_vg(v::VLSpec{:plot})
2323
vl2vg_script_path = joinpath(@__DIR__, "vl2vg.js")
24-
data = JSON.json(getparams(v))
24+
data = render_json(v)
2525
p = open(`$(nodejs_cmd()) $vl2vg_script_path`, "r+")
2626
writer = @async begin
2727
write(p, data)
@@ -39,7 +39,7 @@ end
3939
function convert_vl_to_x(v::VLSpec{:plot}, second_script)
4040
vl2vg_script_path = joinpath(@__DIR__, "vl2vg.js")
4141
full_second_script_path = joinpath(@__DIR__, "..", "..", "deps", "node_modules", "vega-cli", "bin", second_script)
42-
data = JSON.json(getparams(v))
42+
data = render_json(v)
4343
p = open(pipeline(`$(nodejs_cmd()) $vl2vg_script_path`, `$(nodejs_cmd()) $full_second_script_path`), "r+")
4444
writer = @async begin
4545
write(p, data)
@@ -75,7 +75,7 @@ Base.Multimedia.istextmime(::MIME{Symbol("application/vnd.vegalite.v3+json")}) =
7575
Base.Multimedia.istextmime(::MIME{Symbol("application/vnd.vega.v5+json")}) = true
7676

7777
function Base.show(io::IO, m::MIME"application/vnd.vegalite.v3+json", v::VLSpec{:plot})
78-
print(io, JSON.json(getparams(v)))
78+
render_json(io, v)
7979
end
8080

8181
function Base.show(io::IO, m::MIME"application/vnd.vega.v5+json", v::VGSpec)

src/vlspec.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,31 @@ end
9090
Create a copy of `spec` without data. See also [`deletedata!`](@ref).
9191
"""
9292
deletedata(spec::VLSpec) = deletedata!(copy(spec))
93+
94+
push_field!(fields, _) = nothing
95+
push_field!(fields, xs::AbstractVector) = foldl(push_field!, xs; init=fields)
96+
function push_field!(fields, dict::AbstractDict)
97+
f = get(dict, "field", nothing)
98+
f !== nothing && push!(fields, string(f))
99+
for v in values(dict)
100+
push_field!(fields, v)
101+
end
102+
end
103+
104+
encoding_fields(spec::VLSpec) = encoding_fields(getparams(spec))
105+
function encoding_fields(specdict)
106+
fields = Set{String}()
107+
for (k, v) in specdict
108+
k == "data" && continue
109+
push_field!(fields, v)
110+
end
111+
return sort!(collect(fields))
112+
end
113+
114+
function with_stripped_data(spec::VLSpec)
115+
fields = encoding_fields(spec)
116+
vals = get(get(getparams(spec), "data", Dict()), "values", nothing)
117+
vals isa AbstractVector || return spec
118+
vals = map(row -> Dict(f => get(row, f, nothing) for f in fields), vals)
119+
return @set spec.data.values = vals
120+
end

0 commit comments

Comments
 (0)