Mix.install([
{:kino, "~> 0.4.1"},
{:vega_lite, "~> 0.1.1"}
])
alias VegaLite, as: Vl
Here's the static scene
position = %{x: 0, y: 0}
{area_x1, area_x2, area_y1, area_y2} = {150, 180, -150, -180}
span =
[area_x1, area_x2, area_y1, area_y2]
|> Enum.map(&abs/1)
|> Enum.max()
|> Kernel.*(1.5)
graph =
Vl.new(width: 600, height: 600)
|> Vl.data_from_values([position])
|> Vl.layers([
Vl.new()
|> Vl.mark(:circle, size: 200, fill: "black")
|> Vl.encode_field(:x, "x",
type: :quantitative,
scale: [domain: [-span, span]],
axis: [orient: "top", title: "x"]
)
|> Vl.encode_field(:y, "y",
type: :quantitative,
scale: [domain: [-span, span]],
axis: [title: "y"]
),
Vl.new()
|> Vl.data_from_values([%{x1: area_x1, x2: area_x2, y1: area_y1, y2: area_y2}])
|> Vl.mark(:rect, fill: nil, stroke: "tomato", stroke_width: 2)
|> Vl.encode_field(:x, "x1", type: :quantitative)
|> Vl.encode_field(:x2, "x2")
|> Vl.encode_field(:y, "y1", type: :quantitative)
|> Vl.encode_field(:y2, "y2")
])
|> Vl.resolve(:scale, x: :shared, y: :shared)
widget = Kino.VegaLite.new(graph)
Kino.VegaLite.clear(widget)
for i <- 1..10 do
Kino.VegaLite.push(widget, %{x: 20 * i, y: -20 * i})
Process.sleep(250)
end