Skip to content
12 changes: 8 additions & 4 deletions src/explorer/explore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ function explore(sim_inst::SimulationInstance; title="Electron", model_index::In

end

# Helper function returns true if VegaLite is verison 3 or above, and false otherwise
function _is_VegaLite_v3()
return isdefined(VegaLite, :vlplot) ? true : false
end

"""
plot(m::Model, comp_name::Symbol, datum_name::Symbol; interactive::Bool = false)

Expand All @@ -128,10 +133,8 @@ function plot(m::Model, comp_name::Symbol, datum_name::Symbol; interactive::Bool
spec = Mimi._spec_for_item(m, comp_name, datum_name, interactive=interactive)
spec === nothing ? error("Spec cannot be built.") : VLspec = spec["VLspec"]

return VegaLite.VLSpec{:plot}(VLspec)

return _is_VegaLite_v3() ? VegaLite.VLSpec(VLspec) : VegaLite.VLSpec{:plot}(VLspec)
end

"""
plot(sim_inst::SimulationInstance, comp_name::Symbol, datum_name::Symbol; interactive::Bool = false, model_index::Int = 1, scen_name::Union{Nothing, String} = nothing, results_output_dir::Union{Nothing, String} = nothing)

Expand All @@ -158,5 +161,6 @@ function plot(sim_inst::SimulationInstance, comp_name::Symbol, datum_name::Symbo
spec = Mimi._spec_for_sim_item(sim_inst, comp_name, datum_name, results; interactive = interactive, model_index = model_index)
spec === nothing ? error("Spec cannot be built.") : VLspec = spec["VLspec"]

return VegaLite.VLSpec{:plot}(VLspec)
return _is_VegaLite_v3() ? VegaLite.VLSpec(VLspec) : VegaLite.VLSpec{:plot}(VLspec)

end
8 changes: 7 additions & 1 deletion test/test_explorer_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ using Electron
import Mimi:
dataframe_or_scalar, _spec_for_item, menu_item_list, getdataframe, dim_names

# Helper function returns true if VegaLite is verison 3 or above, and false otherwise
function _is_VegaLite_v3()
return isdefined(VegaLite, :vlplot) ? true : false
end

@defcomp MyComp begin
a = Parameter(index=[time, regions])
b = Parameter(index=[time])
Expand Down Expand Up @@ -70,7 +75,8 @@ close(w)
items = [:a, :b, :c, :d, :e, :f, :x]
for item in items
p = Mimi.plot(m, :MyComp, item)
@test typeof(p) == VegaLite.VLSpec{:plot}
p_type = _is_VegaLite_v3() ? VegaLite.VLSpec : VegaLite.VLSpec{:plot}
@test typeof(p) == p_type
end

#6. errors and warnings
Expand Down
40 changes: 24 additions & 16 deletions test/test_explorer_sim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,48 +73,56 @@ close(w)

## 3. Plots

function plot_type_test(p)
if _is_VegaLite_v3()
@test typeof(p) == VegaLite.VLSpec
else
@test typeof(p) == VegaLite.VLSpec{:plot}
end
end

# trumpet plot
p = Mimi.plot(si, :emissions, :E_Global)
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)
p = Mimi.plot(si, :emissions, :E_Global; interactive = true)
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)

p = Mimi.plot(si_disk, :emissions, :E_Global, results_output_dir = results_output_dir)
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)
p = Mimi.plot(si_disk, :emissions, :E_Global; interactive = true, results_output_dir = results_output_dir)
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)

@test_throws ErrorException p = Mimi.plot(si_disk, :emissions, :E_Global) #should error, no in-memory results

# mulitrumpet plot
p = Mimi.plot(si, :emissions, :E)
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)
p = Mimi.plot(si, :emissions, :E; interactive = true);
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)

p = Mimi.plot(si_disk, :emissions, :E, results_output_dir = results_output_dir)
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)
p = Mimi.plot(si_disk, :emissions, :E; interactive = true, results_output_dir = results_output_dir);
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)

# histogram plot
p = Mimi.plot(si, :grosseconomy, :share_var)
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)
p = Mimi.plot(si, :grosseconomy, :share_var; interactive = true); # currently just calls static version
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)

p = Mimi.plot(si_disk, :grosseconomy, :share_var; results_output_dir = results_output_dir)
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)
p = Mimi.plot(si_disk, :grosseconomy, :share_var; interactive = true, results_output_dir = results_output_dir); # currently just calls static version
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)

# multihistogram plot
p = Mimi.plot(si, :grosseconomy, :depk_var)
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)
p = Mimi.plot(si, :grosseconomy, :depk_var; interactive = true);
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)

p = Mimi.plot(si_disk, :grosseconomy, :depk_var; results_output_dir = results_output_dir)
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)
p = Mimi.plot(si_disk, :grosseconomy, :depk_var; interactive = true, results_output_dir = results_output_dir);
@test typeof(p) == VegaLite.VLSpec{:plot}
plot_type_test(p)