Skip to content

Commit 44e460a

Browse files
committed
Add line plot sample
1 parent 6028721 commit 44e460a

File tree

2 files changed

+117
-4
lines changed

2 files changed

+117
-4
lines changed

fullstack/lib/fullstack_web/live/public/public_transactions.ex

+116-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
33

44
alias Fullstack.Devices
55
alias Fullstack.Financial
6-
alias Contex.{BarChart, Plot, Dataset, Sparkline, PointPlot}
6+
alias Contex.{BarChart, Plot, Dataset, Sparkline, LinePlot}
77
alias Contex
88

99
@impl true
@@ -52,8 +52,8 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
5252
]
5353
)
5454
|> assign(bar_clicked: "Click a bar. Any bar", selected_bar: nil)
55-
56-
# |> make_transactions_data()
55+
|> assign(prev_series: 0, prev_points: 0, prev_time_series: nil)
56+
|> make_point_data
5757

5858
{:noreply, socket}
5959
end
@@ -138,4 +138,117 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
138138

139139
assign(socket, test_data: result)
140140
end
141+
142+
def build_pointplot(dataset, chart_options) do
143+
module =
144+
case chart_options.type do
145+
"line" -> LinePlot
146+
_ -> PointPlot
147+
end
148+
149+
# custom_x_scale = make_custom_x_scale(chart_options)
150+
# custom_y_scale = make_custom_y_scale(chart_options)
151+
152+
options = [
153+
mapping: %{x_col: "X", y_cols: chart_options.series_columns},
154+
colour_palette: :defatult,
155+
# custom_x_scale: custom_x_scale,
156+
# custom_y_scale: custom_y_scale,
157+
# custom_y_formatter: y_tick_formatter,
158+
smoothed: chart_options.smoothed == "yes"
159+
]
160+
161+
plot_options =
162+
case chart_options.legend_setting do
163+
"legend_right" -> %{legend_setting: :legend_right}
164+
"legend_top" -> %{legend_setting: :legend_top}
165+
"legend_bottom" -> %{legend_setting: :legend_bottom}
166+
_ -> %{}
167+
end
168+
169+
plot =
170+
Plot.new(dataset, module, 600, 400, options)
171+
|> Plot.titles(chart_options.title, nil)
172+
|> Plot.plot_options(plot_options)
173+
174+
Plot.to_svg(plot)
175+
end
176+
177+
@date_min ~N{2019-10-01 10:00:00}
178+
@interval_seconds 600
179+
defp calc_x(x, _, false), do: x
180+
181+
defp calc_x(_, i, _) do
182+
NaiveDateTime.add(@date_min, i * @interval_seconds)
183+
end
184+
185+
defp make_point_data(socket) do
186+
options =
187+
%{
188+
series: 4,
189+
points: 30,
190+
title: nil,
191+
type: "line",
192+
smoothed: "yes",
193+
colour_scheme: "default",
194+
legend_setting: "legend_none",
195+
custom_x_scale: "no",
196+
custom_y_scale: "no",
197+
custom_y_ticks: "no",
198+
time_series: "no"
199+
}
200+
201+
time_series = options.time_series == "yes"
202+
prev_series = socket.assigns.prev_series
203+
prev_points = socket.assigns.prev_points
204+
prev_time_series = socket.assigns.prev_time_series
205+
series = options.series
206+
points = options.points
207+
208+
needs_update =
209+
prev_series != series or prev_points != points or prev_time_series != time_series
210+
211+
data =
212+
for i <- 1..points do
213+
x = i * 5 + random_within_range(0.0, 3.0)
214+
215+
series_data =
216+
for s <- 1..series do
217+
val = s * 8.0 + random_within_range(x * (0.1 * s), x * (0.35 * s))
218+
# simulate nils in data
219+
case s == 2 and ((i > 3 and i < 6) or (i > 7 and i < 10)) do
220+
true -> nil
221+
_ -> val
222+
end
223+
end
224+
225+
[calc_x(x, i, time_series) | series_data]
226+
end
227+
228+
series_cols =
229+
for s <- 1..series do
230+
"Series #{s}"
231+
end
232+
233+
test_data =
234+
case needs_update do
235+
true -> Dataset.new(data, ["X" | series_cols])
236+
_ -> socket.assigns.test_data
237+
end
238+
239+
options = Map.put(options, :series_columns, series_cols)
240+
241+
assign(socket,
242+
point_data: test_data,
243+
point_options: options,
244+
prev_series: series,
245+
prev_points: points,
246+
prev_time_series: time_series
247+
)
248+
end
249+
250+
defp random_within_range(min, max) do
251+
diff = max - min
252+
:rand.uniform() * diff + min
253+
end
141254
end

fullstack/lib/fullstack_web/live/public/public_transactions.html.heex

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<%= make_plot(@info.daily_data, @bar_options, @selected_bar) %>
66
</div>
77
<div class="md:p-2 p-1 w-1/2">
8-
<%= make_point_plot(@info.daily_data, @bar_options, @selected_bar) %>
8+
<%= build_pointplot(@point_data, @point_options) %>
99
</div>
1010
<div class="flex flex-wrap md:p-2 p-1 w-3/4">
1111
<div class="p-1 w-full">

0 commit comments

Comments
 (0)