Powered by AppSignal & Oban Pro

vl

stage2/vl.livemd

vl

Mix.install([
  {:kino_vega_lite, "~> 0.1.7"},
  {:kino, "~> 0.10.0"}
])

Section

x = 1..11

y =
  for xi <- x do
    3 * xi - 24
  end

my_data = %{x: x, y: y}
VegaLite.new()
|> VegaLite.data_from_values(my_data, only: ["x", "y"])
|> VegaLite.mark(:line)
|> VegaLite.encode_field(:x, "x", type: :quantitative)
|> VegaLite.encode_field(:y, "y", type: :quantitative)
my_data = %{
  x: [1, 2, 3, 4, 5, 6, 7],
  y: [64.3, 63.8, 63.6, 64.0, 63.5, 63.2, 63.1]
}
VegaLite.new()
|> VegaLite.data_from_values(my_data, only: ["x", "y"])
|> VegaLite.mark(:point)
|> VegaLite.encode_field(:x, "x", type: :quantitative)
|> VegaLite.encode_field(:y, "y", type: :quantitative)
y =
  1..100
  |> Enum.map(fn x ->
    :math.sin(x / 5)
  end)

x =
  1..100
  |> Enum.map(fn x ->
    x
  end)

my_data = %{x: x, y: y}
VegaLite.new()
|> VegaLite.data_from_values(my_data, only: ["x", "y"])
|> VegaLite.mark(:line)
|> VegaLite.encode_field(:x, "x", type: :quantitative)
|> VegaLite.encode_field(:y, "y", type: :quantitative)
memory = [
  total: :red,
  processes: :yellow,
  atom: :green,
  binary: :pink,
  code: :orange,
  ets: :blue
]

layers =
  for {layer, color} <- memory do
    VegaLite.new()
    |> VegaLite.mark(:line)
    |> VegaLite.encode_field(:x, "iteration", type: :quantitative)
    |> VegaLite.encode_field(:y, Atom.to_string(layer),
      type: :quantitative,
      title: "Memory usage (MB)"
    )
    |> VegaLite.encode(:color, value: color, datum: Atom.to_string(layer))
  end

widget =
  VegaLite.new(width: 500, height: 200)
  |> VegaLite.layers(layers)
  |> Kino.VegaLite.new()
Kino.VegaLite.periodically(widget, 200, 0, fn i ->
  point =
    :erlang.memory()
    |> Enum.map(fn {type, bytes} ->
      {type, bytes / 1_000_000}
    end)
    |> Map.new()
    |> Map.put(:iteration, i)

  Kino.VegaLite.push(widget, point, window: 1000)
  {:cont, i + 1}
end)
for i <- 1..1_000_000 do
  :"atom#{i}"
end