From b3428f207ab581c0261023ffc25523cb801ea32f Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Fri, 11 Oct 2019 14:30:35 -0700 Subject: [PATCH] Include only required fields in data --- src/rendering/render.jl | 6 +++++- src/rendering/show.jl | 8 ++++---- src/vlspec.jl | 29 +++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/rendering/render.jl b/src/rendering/render.jl index 067f4e73..b0758684 100644 --- a/src/rendering/render.jl +++ b/src/rendering/render.jl @@ -202,7 +202,7 @@ 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 @@ -210,3 +210,7 @@ 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))) diff --git a/src/rendering/show.jl b/src/rendering/show.jl index 80fe6ec4..4ba7c61b 100644 --- a/src/rendering/show.jl +++ b/src/rendering/show.jl @@ -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) @@ -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) @@ -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}) diff --git a/src/vlspec.jl b/src/vlspec.jl index 785098db..56f7ae13 100644 --- a/src/vlspec.jl +++ b/src/vlspec.jl @@ -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