From b42de82f61978ba15e3bd7dd637852f0b8064bd0 Mon Sep 17 00:00:00 2001 From: fredo Date: Mon, 14 Mar 2016 00:24:31 +0100 Subject: [PATCH] started IJulia integration, corrected Escher replotting issue --- LICENSE | 2 +- REQUIRE | 1 + assets/vega-lite.html | 15 +-- src/VegaLite.jl | 2 +- src/escher_integration.jl | 29 +++++- src/render.jl | 208 ++++++++++++++++++++++++++++++++------ test/REQUIRE | 1 + 7 files changed, 213 insertions(+), 45 deletions(-) diff --git a/LICENSE b/LICENSE index 7adb0470..bf243295 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 Frédéric Testard +Copyright (c) 2016 John Myles White, Randy Zwitch & Frédéric Testard Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/REQUIRE b/REQUIRE index 2bbaa542..db4f7e43 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,3 @@ julia 0.4 +Compat JSON diff --git a/assets/vega-lite.html b/assets/vega-lite.html index 21d1f6c5..2c169d3b 100644 --- a/assets/vega-lite.html +++ b/assets/vega-lite.html @@ -6,7 +6,7 @@ @@ -18,23 +18,24 @@ type: Object, notify: true, observer: 'render' - } + }, + svg: Boolean, + actions: Boolean }, render: function() { var embedSpec = { mode: "vega-lite", + renderer: this.svg ? "svg" : "canvas", + actions: false, //this.actions, spec: this.json } - vg.embed("#plot", embedSpec, function(error, result) { - result.view.renderer("svg") - // Callback receiving the View instance and parsed Vega spec - // result.view is the View, which resides under the '#vis' element + vg.embed(this, embedSpec, function(error, result) { }); }, - ready: function (){ + attached: function (){ this.render() }, diff --git a/src/VegaLite.jl b/src/VegaLite.jl index 7fb66178..50081ab1 100644 --- a/src/VegaLite.jl +++ b/src/VegaLite.jl @@ -2,7 +2,7 @@ module VegaLite - using JSON + using JSON, Compat import Base: +, *, scale, show diff --git a/src/escher_integration.jl b/src/escher_integration.jl index 5e102a6e..0e38ea5c 100644 --- a/src/escher_integration.jl +++ b/src/escher_integration.jl @@ -1,12 +1,33 @@ import Escher: Elem, Tile, render import Base: convert +ESCHER_SVG = true +ESCHER_BUTTONS = true + +export svg, buttons + +svg() = ESCHER_SVG +svg(b::Bool) = (global ESCHER_SVG ; ESCHER_SVG = b) +buttons() = ESCHER_BUTTONS +buttons(b::Bool) = (global ESCHER_BUTTONS ; ESCHER_BUTTONS = b) + + type VegaLiteTile <: Tile - json::AbstractString + json::AbstractString + svg::Bool + actions::Bool +end + +function VegaLiteTile(vis::AbstractString) + VegaLiteTile(vis, ESCHER_SVG, ESCHER_BUTTONS) end -# convert(::Type{Tile}, v::VegaLiteVis) = VegaLiteTile(JSON.json(tojs(v))) convert(::Type{Tile}, v::VegaLiteVis) = VegaLiteTile(JSON.json(v.vis)) -render(plot::VegaLiteTile, state) = - Elem(:"vega-lite-plot", attributes = Dict(:json=>plot.json)) +function render(plot::VegaLiteTile, state) + attr = Dict(:json=>plot.json) + plot.svg && ( attr[:svg] = "" ) + plot.actions && ( attr[:actions] = "" ) + + Elem(:"vega-lite-plot", attributes = attr) +end diff --git a/src/render.jl b/src/render.jl index 8b89c3ae..0fabcae0 100644 --- a/src/render.jl +++ b/src/render.jl @@ -1,47 +1,46 @@ -asset(url...) = readall(Pkg.dir("VegaLite", "assets", "bower_components", url...)) +asset(url...) = @compat readstring(Pkg.dir("VegaLite", "assets", "bower_components", url...)) #Vega Scaffold: https://github.com/vega/vega/wiki/Runtime function writehtml(io::IO, v::VegaLiteVis; title="Vega.jl Visualization") + d3 = asset("d3","d3.min.js") + vega = asset("vega", "vega.js") + vegalite = asset("vega-lite", "vega-lite.js") + vegaembed = asset("vega-embed", "vega-embed.js") - d3 = asset("d3","d3.min.js") - vega = asset("vega", "vega.js") - vegalite = asset("vega-lite", "vega-lite.js") - vegaembed = asset("vega-embed", "vega-embed.js") + divid = "vg" * randstring(3) - divid = "vg" * randstring(3) + println(io, + """ + + + $title + + + + - println(io, - """ - - - $title - - - - + + +
+ - - -
- + + - - """) + + """) end function show(io::IO, v::VegaLiteVis) @@ -59,8 +58,153 @@ function show(io::IO, v::VegaLiteVis) @osx_only run(`open $tmppath`) @windows_only run(`cmd /c start $tmppath`) @linux_only run(`xdg-open $tmppath`) - end return end + +################################################### + +import Base.writemime + +# asset(url...) = readall(Pkg.dir("Vega", "assets", "bower_components", url...)) + +# function writemime(io::IO, ::MIME"text/html", v::VegaVisualization) +# divid = "vg" * randstring(3) +# script_contents = scriptstr(v, divid) +# display("text/html", """ +# +#
+# +# +# """) +# end +# +import Patchwork: Elem + +# function patchwork_repr(v::VegaLiteVis) +# divid = "vg" * randstring(3) +# script_contents = scriptstr(v, divid) +# Elem(:div, [ +# Elem(:div, "") & Dict(:id=>divid, :style=>Dict("min-height"=>"$(v.height + 110)px")), +# Elem(:script, script_contents) & Dict(:type=>"text/javascript") +# ]) +# end + +# function writemime(io::IO, m::MIME"text/html", v::VegaLiteVis) +# +# +# fh = """ +#
+# +# +# +# +# +# +# +# +# +# +# +# +#
+# """ +# +# # writemime(io, m, fh) +# write(io, fh) +# end + +jspath(url...) = Pkg.dir("VegaLite", "assets", "bower_components", url...) + +function writemime(io::IO, m::MIME"text/html", v::VegaLiteVis) + # fh = """ + #
+ # + # + # + # + + fh = """ +
+ +
+ +
+ + + + + +
+ """ + + println(fh) + # writemime(io, m, fh) + write(io, fh) +end + + +export writemime diff --git a/test/REQUIRE b/test/REQUIRE index 543f40fa..12c9ca9a 100644 --- a/test/REQUIRE +++ b/test/REQUIRE @@ -1,3 +1,4 @@ julia 0.4 JSON RDatasets +Compat