Skip to content

Instantly share code, notes, and snippets.

@zachallaun
Created April 16, 2023 18:41
Show Gist options
  • Save zachallaun/44445a4e99ef99f1c1c7dc0445424852 to your computer and use it in GitHub Desktop.
Save zachallaun/44445a4e99ef99f1c1c7dc0445424852 to your computer and use it in GitHub Desktop.
Graphing example using Livebook and Kino.VegaLite

Graphing Example

Mix.install([
  {:kino_vega_lite, "~> 0.1.8"}
])

alias VegaLite, as: Vl

Temperature over time

temp = fn time, degrees_c ->
  %{"time" => time, "degrees_c" => degrees_c}
end

data_stream =
  Stream.iterate(temp.(DateTime.utc_now(), 20.0), fn last ->
    temp.(
      DateTime.add(last["time"], 30, :second),
      last["degrees_c"] + :rand.uniform()
    )
  end)
data_stream
|> Stream.take(10)
|> Enum.to_list()
# Source: https://vega.github.io/vega-lite/examples/line_color.html

vl =
  Vl.new(width: 600, height: 400)
  |> Vl.mark(:line)
  |> Vl.encode_field(:x, "time", type: :temporal)
  |> Vl.encode_field(:y, "degrees_c", type: :quantitative)
  |> Kino.VegaLite.new()
data_stream
|> Stream.each(fn datum ->
  Kino.VegaLite.push(vl, datum)
  :timer.sleep(500)
end)
|> Stream.run()
@zachallaun
Copy link
Author

kino_vegalite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment