Skip to content

Commit

Permalink
Include only required fields in data
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Oct 11, 2019
1 parent cd3ca34 commit b3428f2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/rendering/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,15 @@ end

function Base.display(d::REPL.REPLDisplay, plt::VLSpec{:plot})
# checkplot(plt)
tmppath = writehtml_full(JSON.json(getparams(plt)))
tmppath = writehtml_full(render_json(plt))
launch_browser(tmppath) # Open the browser
end

function Base.display(d::REPL.REPLDisplay, plt::VGSpec)
tmppath = write_vg_html_full(JSON.json(getparams(plt)))
launch_browser(tmppath) # Open the browser
end

render_json(x) = sprint(render_json, x)
render_json(io::IO, plt::VLSpec) =
JSON.print(io, getparams(with_stripped_data(plt)))
8 changes: 4 additions & 4 deletions src/rendering/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ end

function convert_vl_to_vg(v::VLSpec{:plot})
vl2vg_script_path = joinpath(@__DIR__, "vl2vg.js")
data = JSON.json(getparams(v))
data = render_json(v)
p = open(`$(nodejs_cmd()) $vl2vg_script_path`, "r+")
writer = @async begin
write(p, data)
Expand All @@ -39,7 +39,7 @@ end
function convert_vl_to_x(v::VLSpec{:plot}, second_script)
vl2vg_script_path = joinpath(@__DIR__, "vl2vg.js")
full_second_script_path = joinpath(@__DIR__, "..", "..", "deps", "node_modules", "vega-cli", "bin", second_script)
data = JSON.json(getparams(v))
data = render_json(v)
p = open(pipeline(`$(nodejs_cmd()) $vl2vg_script_path`, `$(nodejs_cmd()) $full_second_script_path`), "r+")
writer = @async begin
write(p, data)
Expand Down Expand Up @@ -75,11 +75,11 @@ Base.Multimedia.istextmime(::MIME{Symbol("application/vnd.vegalite.v3+json")}) =
Base.Multimedia.istextmime(::MIME{Symbol("application/vnd.vega.v5+json")}) = true

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

function Base.show(io::IO, m::MIME"application/vnd.vega.v5+json", v::VGSpec)
print(io, JSON.json(getparams(v)))
render_json(io, v)
end

function Base.show(io::IO, m::MIME"application/vnd.vega.v5+json", v::VLSpec{:plot})
Expand Down
29 changes: 29 additions & 0 deletions src/vlspec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,32 @@ end
Create a copy of `spec` without data. See also [`deletedata!`](@ref).
"""
deletedata(spec::VLSpec) = deletedata!(copy(spec))

push_field!(fields, _) = nothing
push_field!(fields, xs::AbstractVector) = foldl(push_field!, xs; init=fields)
function push_field!(fields, dict::AbstractDict)
f = get(dict, "field", nothing)
f !== nothing && push!(fields, string(f))
for v in values(dict)
push_field!(fields, v)
end
end

encoding_fields(spec::VLSpec) = encoding_fields(getparams(spec))
function encoding_fields(specdict)
fields = Set{String}()
for (k, v) in specdict
k == "data" && continue
push_field!(fields, v)
end
return sort!(collect(fields))
end

function with_stripped_data(spec::VLSpec)
fields = encoding_fields(spec)
data = getparams(spec.data)
vals = get(data, "values", nothing)
vals isa AbstractVector || return spec
vals = map(row -> Dict(f => get(row, f, nothing) for f in fields), vals)
return @set spec.data.values = vals
end

0 comments on commit b3428f2

Please sign in to comment.