Skip to content

Commit

Permalink
Add SimpleVLSpec to make it usable w/o VegaLite for simple plots
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Sep 26, 2019
1 parent 61035b0 commit 172d96b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
4 changes: 1 addition & 3 deletions Project.toml
Expand Up @@ -4,7 +4,6 @@ authors = ["Takafumi Arakaki <aka.tkf@gmail.com>"]
version = "0.1.0-DEV"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Electron = "a1bb12fb-d4d1-54b4-b10a-ee7951ef7ad3"
ElectronDisplay = "d872a56f-244b-5cc9-b574-2017b5b909a8"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Expand All @@ -14,7 +13,6 @@ julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
VegaLite = "112f6efa-9a02-5b7d-90c0-432ed331239a"

[targets]
test = ["Test", "VegaLite"]
test = ["Test"]
34 changes: 29 additions & 5 deletions src/VegaStreams.jl
Expand Up @@ -2,7 +2,6 @@ module VegaStreams

export vegastream

using Base64
using Electron
using ElectronDisplay
using ElectronDisplay: asset, displayhtml, newdisplay
Expand All @@ -20,7 +19,8 @@ function vegalite_html(vlspec)
error(vlspec, " does not support vegalite v2 or v3.")
end

payload = stringmime(
payload = sprint(
show,
MIME("application/vnd.vegalite.v$major_version_vegalite+json"),
vlspec,
)
Expand Down Expand Up @@ -79,9 +79,10 @@ end

struct VegaStreamWindow
window::Electron.Window
processrow
end

function vegastream(vlspec; kwargs...)
function vegastream(vlspec; processrow=identity, kwargs...)
html_page = vegalite_html(vlspec)

# Using ElectronDisplay internals to support `single_window = false`.
Expand All @@ -98,15 +99,38 @@ function vegastream(vlspec; kwargs...)
=#

# TODO: wait for VIEW to be set?
return VegaStreamWindow(window)
return VegaStreamWindow(window, processrow)
end

Base.push!(stream::VegaStreamWindow, row) = append!(stream, [row])

function Base.append!(stream::VegaStreamWindow, rows)
json = JSON.json(collect(rows))
json = JSON.json(collect(map(stream.processrow, rows)))
run(stream.window, "appendRows($json)")
return stream
end

vegastream(vlspec::Dict; kwargs...) = vegastream(SimpleVLSpec(vlspec); kwargs...)

vegastream(mark::Symbol = :line; kwargs...) =
vegastream(SimpleVLSpec(mark); processrow = NamedTuple{(:x, :y)}, kwargs...)

struct SimpleVLSpec
params::Dict{String, Any}
end

SimpleVLSpec(mark::Symbol) =
SimpleVLSpec(Dict(
"mark" => mark,
"encoding" => Dict(
"x" => Dict("field" => "x"),
"y" => Dict("field" => "y"),
)
))

function Base.show(io::IO, ::MIME"application/vnd.vegalite.v3+json", vlspec::SimpleVLSpec)
JSON.print(io, vlspec.params)
return
end

end # module
8 changes: 6 additions & 2 deletions test/runtests.jl
@@ -1,10 +1,14 @@
using VegaLite
using VegaStreams
using Test

@testset "VegaStreams.jl" begin
vls = vegastream(@vlplot(:line, x=:x, y=:y))
vls = vegastream()
for row in enumerate(randn(100))
sleep(0.01)
push!(vls, row)
end

vls = vegastream(:point)
for (x, y) in enumerate(randn(100))
sleep(0.01)
push!(vls, (x=x, y=y))
Expand Down

0 comments on commit 172d96b

Please sign in to comment.