8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Kino.VegaLiteのグラフアニメーション表示

Posted at

グラフアニメーション表示をやってみました。

表示領域を確保

最初に、Kino.render()で、グラフを描画する領域を確保しておきます。
Kino.render()が返した値を保存(wl_wiget)しておいきます。
image.png

グラフの描画

Kino.VegaLite.push_manyを使ってグラフのデータを更新してアニメーション表示します。

ポイントは、この更新方法です。
push_manyは、データを追加するためのもので、グラフ全体を書き直す機能はもっていません。
どうやらグラフ全体を書き換える手段がありません。

Kino.VegaLite.clear()でクリアして再描画するという力業を使ってます。
push_manyにclearオプションがあってもいいかも。

描画と描画の間が短かすぎるとちらついたりします。適当に時間をあけます。
Process.sleep(20)で20msec停止させてみました。

Kino.VegaLite.clear(vl_widget)
Kino.VegaLite.push_many(vl_widget, points)

sin波が移動しているデモを作ってみました。
動画は、https://github.com/masahiro-999/kino-animation
を見てください

image.png

livemd全体

以下の通りです。

kino-animation

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

Section

alias VegaLite, as: Vl

vl_widget =
  Vl.new(width: 800, height: 400)
  |> VegaLite.mark(:point, tooltip: true)
  |> VegaLite.encode_field(:x, "x", type: :quantitative)
  |> VegaLite.encode_field(:y, "y", type: :quantitative)
  |> Kino.VegaLite.new()
  |> Kino.render()

plot = fn x, y ->
  points =
    [x, y]
    |> Enum.zip()
    |> Enum.map(fn {x, y} -> %{x: x, y: y} end)

  Kino.VegaLite.clear(vl_widget)
  Kino.VegaLite.push_many(vl_widget, points)
end
target_func = fn x, t -> :math.sin((x - t) * :math.pi() * 2) end
x = for i <- 0..100, do: i / 100

for i <- 0..1000 do
  t = i / 100
  y = for x1 <- x, do: target_func.(x1, t)
  plot.(x, y)
  Process.sleep(20)
end

nil

ソースファイル
https://github.com/masahiro-999/kino-animation

8
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?