1
1

wslでelixir その137

Last updated at Posted at 2024-02-16

概要

wsl(wsl2じゃない)で、elixirやってみた。
動きがある、vegalite、やってみた。

写真

image.png

セットアップ

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

サンプルコード

import :math
alias VegaLite, as: Vl

f = fn x -> 
	:math.sin(x * 2) * :math.cos(x) 
end
f.(12)

values = for x <- 1..10000, do: (x / 100)
				|> (fn x -> 
          %{x: x, y: f.(x)} 
        end).()

        
Vl.new(width: 720, height: 100)
|> Vl.data_from_values(values)
|> Vl.mark(:line)
|> Vl.encode_field(:x, "x", type: :quantitative, title: "x")
|> Vl.encode_field(:y, "y", type: :quantitative, title: "sin(2x)*cos(x)")

interval = 100

kino = Vl.new(width: 400, height: 400)
	|> Vl.mark(:line)
	|> Vl.encode_field(:x, "x", type: :quantitative)
	|> Vl.encode_field(:y, "y", type: :quantitative)
	|> Kino.VegaLite.new()
	|> Kino.render()

fun = fn _, {kino, x} ->
	Kino.VegaLite.push(kino, %{x: x, y: f.(x)}, window: 100)
	{:cont, {kino, x + 0.1}}
end

Kino.listen(interval, {kino, 0}, fun)

以上。

1
1
0

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
1
1