Skip to content

Commit

Permalink
Merge pull request #131 from leethargo/rs/fix129_data_kwarg
Browse files Browse the repository at this point in the history
fix #129: calling @vlplot with data keyword
  • Loading branch information
davidanthoff committed Nov 22, 2018
2 parents 0c305db + f1f8ba3 commit 1d178f7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
6 changes: 2 additions & 4 deletions src/dsl_vlplot_macro/dsl_vlplot_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,8 @@ function fix_shortcuts(spec::Dict{String,Any}, positional_key::String)
new_spec["data"] = Dict{String,Any}("url" => Sys.iswindows() && new_spec["data"].scheme=="file" ? as_uri[1:5] * as_uri[7:end] : as_uri)
elseif TableTraits.isiterabletable(new_spec["data"])
it = IteratorInterfaceExtensions.getiterator(new_spec["data"])

recs = [Dict{String,Any}(string(c[1])=>isa(c[2], DataValues.DataValue) ? (isna(c[2]) ? nothing : get(c[2])) : c[2] for c in zip(keys(r), values(r))) for r in it]

new_spec["data"] = Dict{String,Any}("values" => recs)
set_spec_data!(new_spec, it)
detect_encoding_type!(new_spec, it)
end
end

Expand Down
34 changes: 20 additions & 14 deletions src/vlspec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@ struct VLSpec{T}
end
vltype(::VLSpec{T}) where T = T

function (p::VLSpec{:plot})(data)
TableTraits.isiterabletable(data) || throw(ArgumentError("'data' is not a table."))

it = IteratorInterfaceExtensions.getiterator(data)
function set_spec_data!(specdict, datait)
recs = [Dict{String,Any}(string(c[1])=>isa(c[2], DataValues.DataValue) ? (isna(c[2]) ? nothing : get(c[2])) : c[2] for c in zip(keys(r), values(r))) for r in datait]
specdict["data"] = Dict{String,Any}("values" => recs)
end

col_names = fieldnames(eltype(it))
col_types = [fieldtype(eltype(it),i) for i in col_names]
function detect_encoding_type!(specdict, datait)
col_names = fieldnames(eltype(datait))
col_types = [fieldtype(eltype(datait),i) for i in col_names]
col_type_mapping = Dict{Symbol,Type}(i[1]=>i[2] for i in zip(col_names,col_types))

recs = [Dict{String,Any}(string(c[1])=>isa(c[2], DataValues.DataValue) ? (isna(c[2]) ? nothing : get(c[2])) : c[2] for c in zip(keys(r), values(r))) for r in it]

new_dict = copy(p.params)
new_dict["data"] = Dict{String,Any}("values" => recs)

if haskey(new_dict, "encoding")
for (k,v) in new_dict["encoding"]
if haskey(specdict, "encoding")
for (k,v) in specdict["encoding"]
if !haskey(v, "type")
if !haskey(v, "aggregate") && haskey(v, "field") && haskey(col_type_mapping,Symbol(v["field"]))
jl_type = col_type_mapping[Symbol(v["field"])]
Expand All @@ -42,6 +38,16 @@ function (p::VLSpec{:plot})(data)
end
end
end
end

function (p::VLSpec{:plot})(data)
TableTraits.isiterabletable(data) || throw(ArgumentError("'data' is not a table."))

new_dict = copy(p.params)

it = IteratorInterfaceExtensions.getiterator(data)
set_spec_data!(new_dict, it)
detect_encoding_type!(new_dict, it)

return VLSpec{:plot}(new_dict)
end
Expand All @@ -63,4 +69,4 @@ function (p::VLSpec{:plot})(path::AbstractPath)
new_dict["data"] = Dict{String,Any}("url" => Sys.iswindows() ? as_uri[1:5] * as_uri[7:end] : as_uri)

return VLSpec{:plot}(new_dict)
end
end
2 changes: 2 additions & 0 deletions test/test_vlplot_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@ using Test

@test (@vlplot(description="foo") + [@vlplot(:point); @vlplot(:circle)]).params == @vlplot(description="foo", vconcat=[{mark=:point},{mark=:circle}]).params

@test (@vlplot(:point, x=:a)(DataFrame(a=[1])).params == @vlplot(:point, data=DataFrame(a=[1]), x=:a).params)

end

0 comments on commit 1d178f7

Please sign in to comment.