Skip to content

Commit 354ee42

Browse files
committed
Add math stuff graph to demo page
1 parent ae972bf commit 354ee42

1 file changed

Lines changed: 71 additions & 1 deletion

File tree

demo/lib/plox_demo_web/live/graphs_live.ex

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule PloxDemoWeb.GraphsLive do
44
import Plox
55

66
def mount(_params, _session, socket) do
7-
{:ok, socket |> mount_simple_line() |> mount_logo_graph()}
7+
{:ok, socket |> mount_simple_line() |> mount_logo_graph() |> mount_math_stuff()}
88
end
99

1010
def render(assigns) do
@@ -13,6 +13,8 @@ defmodule PloxDemoWeb.GraphsLive do
1313
<.simple_line simple_line={@simple_line} />
1414
1515
<.logo_graph logo_graph={@logo_graph} />
16+
17+
<.math_stuff math_stuff={@math_stuff} />
1618
</div>
1719
"""
1820
end
@@ -185,6 +187,74 @@ defmodule PloxDemoWeb.GraphsLive do
185187
"""
186188
end
187189

190+
defp mount_math_stuff(socket) do
191+
sine_data =
192+
Enum.map(-360..360//30, fn deg ->
193+
%{degrees: deg, sin: :math.sin(deg * :math.pi() / 180)}
194+
end)
195+
196+
cosine_data =
197+
Enum.map(-360..360//20, fn deg ->
198+
%{degrees: deg, cos: :math.cos(deg * :math.pi() / 180)}
199+
end)
200+
201+
arctangent_data =
202+
Enum.map(-180..180//10, fn deg ->
203+
%{degrees: deg, atan: :math.atan(deg * :math.pi() / 180)}
204+
end)
205+
206+
x_scale = number_scale(-360, 360)
207+
y_scale = number_scale(-1.5, 1.5)
208+
209+
sine_dataset = dataset(sine_data, x: {x_scale, & &1.degrees}, y: {y_scale, & &1.sin})
210+
cosine_dataset = dataset(cosine_data, x: {x_scale, & &1.degrees}, y: {y_scale, & &1.cos})
211+
212+
arctangent_dataset =
213+
dataset(arctangent_data, x: {x_scale, & &1.degrees}, y: {y_scale, & &1.atan})
214+
215+
assign(socket,
216+
math_stuff:
217+
to_graph(
218+
scales: [x_scale: x_scale, y_scale: y_scale],
219+
datasets: [sine: sine_dataset, cosine: cosine_dataset, arctangent: arctangent_dataset]
220+
)
221+
)
222+
end
223+
224+
defp math_stuff(assigns) do
225+
~H"""
226+
<div class="space-y-4">
227+
<.heading>3. Sine/Cosine/ArcTangent Graph</.heading>
228+
229+
<.graph :let={graph} id="math_stuff" for={@math_stuff} width={800} height={250}>
230+
<.x_axis :let={degrees} scale={graph[:x_scale]} ticks={9}>
231+
<%= round(degrees) %>°
232+
</.x_axis>
233+
234+
<.y_axis :let={y} scale={graph[:y_scale]} ticks={7}>
235+
<%= y %>
236+
</.y_axis>
237+
238+
<.line_plot dataset={graph[:sine]} color="#8FDA5D" line_style={:dashed} />
239+
240+
<.line_plot dataset={graph[:cosine]} color="#35A9C0" width="2" line_style={:dotted} />
241+
<.points_plot dataset={graph[:cosine]} color="#35A9C0" />
242+
243+
<.line_plot dataset={graph[:arctangent]} color="#FF5954" width="1" />
244+
<.points_plot dataset={graph[:arctangent]} color="#FF5954" radius="3" />
245+
246+
<.marker at={-180} scale={graph[:x_scale]}>
247+
Start
248+
</.marker>
249+
250+
<.marker at={180} scale={graph[:x_scale]}>
251+
End
252+
</.marker>
253+
</.graph>
254+
</div>
255+
"""
256+
end
257+
188258
slot :inner_block, required: true
189259

190260
defp heading(assigns) do

0 commit comments

Comments
 (0)