Explore HelloPhx Data attaching
alias VegaLite, as: Vl
alias HelloPhx.Catalog.Product
alias HelloPhx.Repo
import Ecto.Query
Explore
query = from(p in Product,
group_by: p.views,
select: {p.views, count(p.id)}
)
views_counts = query
|> Repo.all
|> Enum.into(%{})
# Convert the raw data into a Pie Chart friendly format
data =
Enum.map(views_counts, fn {views, count} ->
%{"views" => "views#{views}", "value" => count}
end)
Vl.new()
|> Vl.data_from_values(data)
|> Vl.mark(:arc)
|> Vl.encode_field(:theta, "value", type: :quantitative)
|> Vl.encode_field(:color, "views", type: :nominal)
|> Vl.config(view: [stroke: nil])
# Convert the raw data into a Bar Chart friendly format
data2 =
Enum.map(views_counts, fn {views, count} ->
%{"Views" => "views#{views}", "Views Count" => count}
end)
# Sort the data by counts to order the results
data2 = Enum.sort_by(data, & &1["Views Count"], :desc)
|> Kino.inspect()
Vl.new(width: 600, height: 600)
|> Vl.data_from_values(data2)
|> Vl.mark(:bar)
|> Vl.encode_field(:x, "Views",
type: :nominal,
axis: [label_angle: 0],
sort: [field: "Views Count", order: "descendingsr"]
)
|> Vl.encode_field(:y, "Views Count", type: :quantitative)