queryverse / VegaLite.jl

Julia bindings to Vega-Lite
Other
267 stars 39 forks source link

Error: CanvasRenderer is missing a valid canvas or context #383

Closed John-Boik closed 3 years ago

John-Boik commented 3 years ago

I'm new to VegaLite and trying to use it to plot a histogram of datetimes. I have a dataframe, df2, that has a column "time" containing datetimes. I want to save the figure as a png. I'm using REPL in a command window on linux, and calling my Julia script using include(scriptname.jl). In the script, a module, I import VegaLite. When I try to save as a png file I get the error: CanvasRenderer is missing a valid canvas or context. I have installed Canvas.jl, and am using Julia Version 1.5.3 with a new install of VegaLite. Any ideas why I get this error, and how I can produce a png file? I call: df2 |> VegaLite.@vlplot(:bar, x="yearmonthdate(time):o", y="count()") |> VegaLite.save("hist_start_day.png") Then I receive the error:

Error: CanvasRenderer is missing a valid canvas or context
    at Object.error (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-scenegraph/node_modules/vega-util/build/vega-util.js:39:11)
    at CanvasRenderer.resize (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-scenegraph/build/vega-scenegraph.js:3633:28)
    at CanvasRenderer.initialize (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-scenegraph/build/vega-scenegraph.js:3190:19)
    at CanvasRenderer.initialize (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-scenegraph/build/vega-scenegraph.js:3621:30)
    at initializeRenderer (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-view/build/vega-view.js:639:14)
    at renderHeadless (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-view/build/vega-view.js:758:12)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async View.renderToCanvas [as toCanvas] (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-view/build/vega-view.js:794:15)

If I instead try to save as an html file, then it works properly. Also, if I have "using VegaLite" in my script, it does not correct the error. I tried to install vega-cli, but there does not seem to be any julia package similar to that name.

jeremiahpslewis commented 3 years ago

Hey! Nice to have new users to VegaLite, sorry about the frustration with canvas (quite likely a javascript dependency issue).

Before someone is able to look into this in more detail, there are three things you can do to help the community give feedback on your problem:

1) Can you try using the latest stable Julia release, 1.6.0? https://julialang.org

2) Can you post a minimal working example, e.g. enough code so that someone else could run your snippet completely? (In this case, you would need to include a 'mock' df2 data frame which reproduces the problem as well as the necessary import statements, something like using DataFrames, VegaLite).

3) Can you use the following command using Pkg; Pkg.status() and paste the output into a code block in this issue?

John-Boik commented 3 years ago

OK, I have now updated to Julia 1.6.0, and rebuilt my package from scratch. Here is a code snipit that causes saving with png to fail. The line that saves as html works just fine.

module test_vega

import DataFrames
import Dates
import VegaLite
using Infiltrator
using Revise

some_dates = []
for ii in -3:3
    push!(some_dates, Dates.now() + Dates.Day(ii))
end
df = DataFrames.DataFrame(time=some_dates)

df |> VegaLite.@vlplot(:bar, x={"timeUnit" = "yearmonthdate", "field" = "time","type" = "ordinal", 
    "axis"={"labelAngle"=-45}}, y="count()", title="End day") |> VegaLite.save("test_vegalite.html")

df |> VegaLite.@vlplot(:bar, x={"timeUnit" = "yearmonthdate", "field" = "time","type" = "ordinal", 
    "axis"={"labelAngle"=-45}}, y="count()", title="End day") |> VegaLite.save("test_vegalite.png")

#@infiltrate; @assert false

end # ---------------------- module --------------------------------

Here is the pkg status:

(Ideai) pkg> status
     Project Ideai v0.1.0
      Status `~/Devel/Ideai_Ju/Ideai/Project.toml`
  [fbb218c0] BSON v0.3.3
  [336ed68f] CSV v0.8.4
  [a93c6f00] DataFrames v0.22.7
  [aae7a2af] DiffEqFlux v1.36.1
  [0c46a032] DifferentialEquations v6.16.0
  [31c24e10] Distributions v0.24.15
  [587475ba] Flux v0.12.1
  [59287772] Formatting v0.4.2
  [c27321d9] Glob v1.3.0
  [5903a43b] Infiltrator v0.3.0
  [194296ae] LibPQ v1.6.2
  [429524aa] Optim v1.3.0
  [1dea7af3] OrdinaryDiffEq v5.52.3
  [91a5bcdd] Plots v1.11.2
  [438e738f] PyCall v1.92.3
  [d330b81b] PyPlot v2.9.0
  [295af30f] Revise v3.1.14
  [2913bbd2] StatsBase v0.33.5
  [112f6efa] VegaLite v2.4.0
  [fdbf4ff8] XLSX v0.7.6
  [e88e6eb3] Zygote v0.6.9
  [10745b16] Statistics

Here is the error:

julia> include("./test_vegalite.jl")
WARNING: replacing module test_vega.
Error: CanvasRenderer is missing a valid canvas or context
    at Object.error (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-scenegraph/node_modules/vega-util/build/vega-util.js:39:11)
    at CanvasRenderer.resize (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-scenegraph/build/vega-scenegraph.js:3633:28)
    at CanvasRenderer.initialize (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-scenegraph/build/vega-scenegraph.js:3190:19)
    at CanvasRenderer.initialize (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-scenegraph/build/vega-scenegraph.js:3621:30)
    at initializeRenderer (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-view/build/vega-view.js:639:14)
    at renderHeadless (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-view/build/vega-view.js:758:12)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async View.renderToCanvas [as toCanvas] (/home/jboik/.julia/artifacts/ead9a31a77872dcae4f9a0e110ddb2281812b6b0/node_modules/vega-view/build/vega-view.js:794:15)
jeremiahpslewis commented 3 years ago

Sorry, it should have occurred to me way sooner. This is a known issue which will be resolved in a future version; at the moment, there is a test which captures the fact that this behavior is broken...

https://github.com/queryverse/VegaLite.jl/issues/361

https://github.com/queryverse/VegaLite.jl/blob/3344842b690e430568399ec63c086dc949b52b7e/test/test_io.jl#L94

jeremiahpslewis commented 3 years ago

There are dependencies which need to be updated / deconflicted before this will work. In the meantime, you might be able to use VegaLite to generate a Vega spec and then pipe it to vg2png in the Vega-cli package. Admittedly, a poor solution, but one of few available until this is resolved.

davidanthoff commented 3 years ago

This should now be fixed on master.

xeruf commented 2 years ago

Is the fix released yet? I still get this error and had to resort to the workaround with vega (vl2vg and then vg2png).

xeruf commented 1 year ago

Now I even encounter this issue in vg2png...