Skip to content

Commit b07c747

Browse files
committed
Update bar chart with customers data
1 parent 5e78a04 commit b07c747

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

fullstack/lib/fullstack/financial.ex

+15-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,21 @@ defmodule Fullstack.Financial do
8585

8686
defp parse_chart_data([%Customer{} | _] = customers) do
8787
customers
88-
|> Enum.map(fn c -> "c #{c.id}" end)
88+
|> Enum.reduce(%{}, fn c, acc ->
89+
acc
90+
|> Map.update(
91+
c.inserted_at
92+
|> NaiveDateTime.to_date()
93+
|> to_string()
94+
|> String.split("-")
95+
|> Enum.take(2),
96+
1,
97+
&(&1 + 1)
98+
)
99+
end)
100+
|> Enum.map(fn {[_, m], value} ->
101+
[Timex.month_name(String.to_integer(m)), value]
102+
end)
89103
end
90104

91105
defp parse_chart_data(transactions) do

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

+29-19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
2828
socket =
2929
socket
3030
|> assign(:info, Financial.build_transactions_analytics(params))
31+
|> assign(:customers, Financial.build_customers_analytics(params))
3132
|> assign(:devices, Devices.list_devices())
3233
|> assign(:form, to_form(params))
3334
|> make_test_data()
@@ -70,8 +71,8 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
7071
end
7172

7273
defp make_plot(data, bar_options, selected_bar) do
73-
series_cols = ["Count", "Amount"]
74-
test_data = Dataset.new(data, ["Day" | series_cols])
74+
series_cols = ["customers"]
75+
test_data = Dataset.new(data, ["Month" | series_cols])
7576

7677
options = [
7778
type: :grouped,
@@ -80,18 +81,18 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
8081
show_selected: "no",
8182
show_axislabels: "yes",
8283
# custom_value_scale: "no",
83-
title: "Sales",
84-
subtitle: "Month",
84+
title: "New Customers",
85+
subtitle: "Per Month",
8586
colour_scheme: :default,
8687
legend_setting: :legend_right,
87-
mapping: %{category_col: "Day", value_cols: series_cols},
88+
mapping: %{category_col: "Month", value_cols: series_cols},
8889
data_labels: true,
8990
phx_event_handler: "chart1_bar_clicked",
9091
colour_palette: :default
9192
]
9293

9394
Plot.new(test_data, BarChart, 500, 400, options)
94-
|> Plot.axis_labels("Day", "Count / Amount")
95+
|> Plot.axis_labels("Month", "New Customers")
9596
|> Plot.to_svg()
9697
end
9798

@@ -139,6 +140,14 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
139140
assign(socket, test_data: result)
140141
end
141142

143+
def format_trx_period(value) when value <= 0.0, do: ""
144+
145+
def format_trx_period(value) do
146+
value
147+
|> round()
148+
|> Timex.month_name()
149+
end
150+
142151
def build_pointplot(dataset, chart_options) do
143152
module =
144153
case chart_options.type do
@@ -154,6 +163,7 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
154163
colour_palette: :defatult,
155164
# custom_x_scale: custom_x_scale,
156165
# custom_y_scale: custom_y_scale,
166+
custom_x_formatter: &format_trx_period/1,
157167
# custom_y_formatter: y_tick_formatter,
158168
smoothed: chart_options.smoothed == "yes"
159169
]
@@ -185,17 +195,17 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
185195
defp make_point_data(socket) do
186196
options =
187197
%{
188-
series: 4,
189-
points: 30,
190-
title: nil,
198+
series: 2,
199+
points: 12,
200+
title: "Sales / Transactions",
191201
type: "line",
192202
smoothed: "yes",
193203
colour_scheme: "default",
194-
legend_setting: "legend_none",
195-
custom_x_scale: "no",
204+
legend_setting: "legend_right",
205+
custom_x_scale: "yes",
196206
custom_y_scale: "no",
197207
custom_y_ticks: "no",
198-
time_series: "no"
208+
time_series: "yes"
199209
}
200210

201211
time_series = options.time_series == "yes"
@@ -214,22 +224,22 @@ defmodule FullstackWeb.Public.TransactionsLive.PublicTransactions do
214224

215225
series_data =
216226
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
227+
s * 8.0 + random_within_range(x * (0.1 * s), x * (0.35 * s))
223228
end
224229

225-
[calc_x(x, i, time_series) | series_data]
230+
[i | series_data]
231+
## [calc_x(x, i, time_series) | series_data]
226232
end
227233

234+
dbg(data)
235+
228236
series_cols =
229237
for s <- 1..series do
230238
"Series #{s}"
231239
end
232240

241+
dbg(series_cols)
242+
233243
test_data =
234244
case needs_update do
235245
true -> Dataset.new(data, ["X" | series_cols])

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="bg-white h-screen font-sans flex flex-col ">
33
<div class="flex flex-wrap ">
44
<div class="md:p-2 p-1 w-1/2">
5-
<%= make_plot(@info.daily_data, @bar_options, @selected_bar) %>
5+
<%= make_plot(@customers, @bar_options, @selected_bar) %>
66
</div>
77
<div class="md:p-2 p-1 w-1/2">
88
<%= build_pointplot(@point_data, @point_options) %>

0 commit comments

Comments
 (0)