Skip to content

Commit

Permalink
Merge pull request #85 from davidanthoff/path
Browse files Browse the repository at this point in the history
Add support for FilePaths
  • Loading branch information
davidanthoff committed Jun 3, 2018
2 parents 3eba772 + b1556c8 commit 49ccbae
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ DataValues
MacroTools
NamedTuples
URIParser
FilePaths
1 change: 1 addition & 0 deletions src/VegaLite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using FileIO # 17s !!!
import DataValues # 1s
import MacroTools
using URIParser
using FilePaths

# This import can eventually be removed, it currently just makes sure
# that the iterable tables integration for DataFrames and friends
Expand Down
5 changes: 5 additions & 0 deletions src/dsl_vlplot_macro/dsl_vlplot_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ function fix_shortcuts(spec::Dict{String,Any}, positional_key::String)
return parse_shortcut(string(p[2]))
elseif p[1]=="typ"
return "type"=>p[2]
elseif p[1]=="url" && p[2] isa AbstractPath && parent=="data"
# TODO This is a hack that might only work on Windows
# Vega seems to not understand properly formed file URIs
as_uri = string(URI(p[2]))
return p[1] => as_uri[1:5] * as_uri[7:end]
else
return p
end
Expand Down
64 changes: 38 additions & 26 deletions src/vlspec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,52 @@ end
vltype(::VLSpec{T}) where T = T

function (p::VLSpec{:plot})(data)
TableTraits.isiterabletable(data) || throw(ArgumentError("'data' is not a table."))
if data isa AbstractPath
new_dict = copy(p.params)

it = IteratorInterfaceExtensions.getiterator(data)
as_uri = string(URI(data))

col_names = TableTraits.column_names(it)
col_types = TableTraits.column_types(it)
col_type_mapping = Dict{Symbol,Type}(i[1]=>i[2] for i in zip(col_names,col_types))

recs = [Dict(c[1]=>isa(c[2], DataValues.DataValue) ? (isnull(c[2]) ? nothing : get(c[2])) : c[2] for c in zip(keys(r), values(r))) for r in it]
# TODO This is a hack that might only work on Windows
# Vega seems to not understand properly formed file URIs
new_dict["data"] = Dict{String,Any}("url" => as_uri[1:5] * as_uri[7:end])

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(v, "type")
if !haskey(v, "aggregate") && haskey(v, "field") && haskey(col_type_mapping,Symbol(v["field"]))
jl_type = col_type_mapping[Symbol(v["field"])]
if jl_type <: DataValues.DataValue
jl_type = eltype(jl_type)
end
if jl_type <: Number
v["type"] = "quantitative"
elseif jl_type <: AbstractString
v["type"] = "nominal"
elseif jl_type <: Base.Dates.AbstractTime
v["type"] = "temporal"
return VLSpec{:plot}(new_dict)
elseif TableTraits.isiterabletable(data)
it = IteratorInterfaceExtensions.getiterator(data)

col_names = TableTraits.column_names(it)
col_types = TableTraits.column_types(it)
col_type_mapping = Dict{Symbol,Type}(i[1]=>i[2] for i in zip(col_names,col_types))

recs = [Dict(c[1]=>isa(c[2], DataValues.DataValue) ? (isnull(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(v, "type")
if !haskey(v, "aggregate") && haskey(v, "field") && haskey(col_type_mapping,Symbol(v["field"]))
jl_type = col_type_mapping[Symbol(v["field"])]
if jl_type <: DataValues.DataValue
jl_type = eltype(jl_type)
end
if jl_type <: Number
v["type"] = "quantitative"
elseif jl_type <: AbstractString
v["type"] = "nominal"
elseif jl_type <: Base.Dates.AbstractTime
v["type"] = "temporal"
end
end
end
end
end
end

return VLSpec{:plot}(new_dict)
return VLSpec{:plot}(new_dict)
else
throw(ArgumentError("'data' is not a table."))
end
end

function (p::VLSpec{:plot})(uri::URI)
Expand Down

0 comments on commit 49ccbae

Please sign in to comment.