Skip to content

Commit

Permalink
Uses columns_for from VegaLite (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: José Valim <[email protected]>
  • Loading branch information
cristineguadelupe and josevalim authored Aug 20, 2023
1 parent 14e38e8 commit 79a94ce
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 46 deletions.
41 changes: 3 additions & 38 deletions lib/kino_vega_lite/chart_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -555,23 +555,15 @@ defmodule KinoVegaLite.ChartCell do

defp missing_dep() do
unless Code.ensure_loaded?(VegaLite) do
~s/{:vega_lite, "~> 0.1.4"}/
~s/{:vega_lite, "~> 0.1.8"}/
end
end

defp columns_for(data) do
with true <- implements?(Table.Reader, data),
data = {_, %{columns: [_ | _] = columns}, _} <- Table.Reader.init(data),
true <- Enum.all?(columns, &implements?(String.Chars, &1)) do
types = infer_types(data)
Enum.zip_with(columns, types, fn column, type -> %{name: to_string(column), type: type} end)
else
_ -> nil
end
if cols = VegaLite.Data.columns_for(data),
do: Enum.map(cols, fn {k, v} -> %{name: k, type: to_string(v)} end)
end

defp implements?(protocol, value), do: protocol.impl_for(value) != nil

defp encode(@count_field), do: :encode
defp encode(_), do: :encode_field

Expand All @@ -590,33 +582,6 @@ defmodule KinoVegaLite.ChartCell do
end
end

defp infer_types({:columns, %{columns: _columns}, data}) do
Enum.map(data, fn data -> data |> Enum.at(0) |> type_of() end)
end

defp infer_types({:rows, %{columns: columns}, data}) do
case Enum.fetch(data, 0) do
{:ok, row} -> Enum.map(row, &type_of/1)
:error -> Enum.map(columns, fn _ -> nil end)
end
end

defp type_of(%mod{}) when mod in [Decimal], do: "quantitative"
defp type_of(%mod{}) when mod in [Date, NaiveDateTime, DateTime], do: "temporal"

defp type_of(data) when is_number(data), do: "quantitative"

defp type_of(data) when is_binary(data) do
if date?(data) or date_time?(data), do: "temporal", else: "nominal"
end

defp type_of(data) when is_atom(data), do: "nominal"

defp type_of(_), do: nil

defp date?(value), do: match?({:ok, _}, Date.from_iso8601(value))
defp date_time?(value), do: match?({:ok, _, _}, DateTime.from_iso8601(value))

defp default_layer() do
%{
"chart_type" => "point",
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule KinoVegaLite.MixProject do
[
{:kino, "~> 0.7"},
{:table, "~> 0.1.0"},
{:vega_lite, "~> 0.1.4"},
{:vega_lite, "~> 0.1.8"},
{:ex_doc, "~> 0.28", only: :dev, runtime: false}
]
end
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"table": {:hex, :table, "0.1.2", "87ad1125f5b70c5dea0307aa633194083eb5182ec537efc94e96af08937e14a8", [:mix], [], "hexpm", "7e99bc7efef806315c7e65640724bf165c3061cdc5d854060f74468367065029"},
"vega_lite": {:hex, :vega_lite, "0.1.4", "97f3f59f1a8b65169aa7dd861b0e13d10fb73e18b1f8546b953c34a74cab33ef", [:mix], [{:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: false]}], "hexpm", "b9d0ec3e978b9d1b1a37024e218391f9c1d5a90b783ed2ed988bf5a9b2fddefb"},
"vega_lite": {:hex, :vega_lite, "0.1.8", "7f6119126ecaf4bc2c1854084370d7091424f5cce4795fbac044eee9963f0752", [:mix], [{:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: false]}], "hexpm", "6c8a9271f850612dd8a90de8d1ebd433590ed07ffef76fc2397c240dc04d3fdc"},
}
12 changes: 6 additions & 6 deletions test/kino_vega_lite/chart_cell_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ defmodule KinoVegaLite.ChartCellTest do

data_options = [
%{
variable: "row_data",
columns: [%{name: "x", type: "quantitative"}, %{name: "y", type: "quantitative"}]
columns: [%{name: "x", type: "quantitative"}, %{name: "y", type: "quantitative"}],
variable: "row_data"
},
%{
variable: "column_data",
columns: [%{name: "x", type: "quantitative"}, %{name: "y", type: "quantitative"}]
columns: [%{name: "x", type: "quantitative"}, %{name: "y", type: "quantitative"}],
variable: "column_data"
},
%{
variable: "temporal_data",
columns: [%{name: "x", type: "nominal"}, %{name: "y", type: "temporal"}]
columns: [%{name: "x", type: "nominal"}, %{name: "y", type: "temporal"}],
variable: "temporal_data"
}
]

Expand Down

0 comments on commit 79a94ce

Please sign in to comment.