diff --git a/guides/data.livemd b/guides/data.livemd new file mode 100644 index 0000000..b928605 --- /dev/null +++ b/guides/data.livemd @@ -0,0 +1,265 @@ +# VegaLite Data + +```elixir +Mix.install([ + {:explorer, "~> 0.6.1"}, + {:kino, "~> 0.10.0"}, + {:vega_lite, + git: "https://github.com/cristineguadelupe/vega_lite", branch: "cg-stats", override: true}, + {:kino_vega_lite, "~> 0.1.9"} +]) +``` + +## Introduction + +The `VegaLite.Data` module is designed to provide a shorthand API to plot commonly used charts and high-level abstractions for specialized plots. + +The API can be combined with the main `VegaLite` module at any level and at any point, providing flexibility to achieve the same results in a more concise way without compromising expressiveness. + +Throughout this guide, we will look at how to use the API alone, in combination with the `VegaLite` module, and also show some comparisons between all the possible paths to achieve the same plotting results. + +**Limitations**: `VegaLite.Data` relies on internal type inference, and although all options may be overridden, only data that implements the `Table.Reader` protocol is supported. + +For meaningful examples, we will use the *fuels* dataset directly from `Explorer` + +```elixir +alias Explorer.DataFrame, as: DF +alias VegaLite, as: Vl +alias VegaLite.Data + +fuels = Explorer.Datasets.fossil_fuels() + +data = [ + %{"category" => "A", "score" => 28}, + %{"category" => "B", "score" => 50}, + %{"category" => "C", "score" => 34}, + %{"category" => "D", "score" => 42}, + %{"category" => "E", "score" => 39} +] +``` + +## Chart - the shorthand api + +`VegaLite.Data.chart/3` and `VegaLite.Data.chart/4` are the shorthand API. We will use these functions to get quick and concise plots. It's shine for plots that don't require a lot of configuration or customization. + +`VegaLite.Data.chart/3` takes 3 arguments: the data, the mark and a list of fields to be encoded while `VegaLite.Data.chart/4` works similarly, but takes a valid `VegaLite` specification as the first argument. + +```elixir +# A simple bar plot +Vl.new() +|> Vl.data_from_values(data) +|> Vl.mark(:bar) +|> Vl.encode_field(:y, "score", type: :quantitative) +|> Vl.encode_field(:x, "category", type: :nominal) +``` + +```elixir +# The same chart with the shorthand api +Data.chart(data, :bar, x: "category", y: "score") +``` + +Plotting a simple chart is a breeze! As we can see from the comparison above, the code becomes much leaner and handleable. However, the API also accepts a list of options for each argument, allowing more complex results. + +```elixir +# A line plot with point: true without the shorthand api +Vl.new() +|> Vl.data_from_values(fuels, only: ["total", "solid_fuel"]) +|> Vl.mark(:line, point: true) +|> Vl.encode_field(:x, "total", type: :quantitative) +|> Vl.encode_field(:y, "solid_fuel", type: :quantitative) +``` + +```elixir +# A line plot with point: true using the shorthand api +Data.chart(fuels, [type: :line, point: true], x: "total", y: "solid_fuel") +``` + +Now let's see a bit of iteroperability between the api and the main module. We'll plot the same line chart but now with a title and a custom width. + +```elixir +# Without the shorthand api +Vl.new(title: "Fuels", width: 400) +|> Vl.data_from_values(fuels, only: ["total", "solid_fuel"]) +|> Vl.mark(:line, point: true) +|> Vl.encode_field(:x, "total", type: :quantitative) +|> Vl.encode_field(:y, "solid_fuel", type: :quantitative) +``` + +```elixir +# With the shorthand api +Vl.new(title: "Fuels", width: 400) +|> Data.chart(fuels, [type: :line, point: true], x: "total", y: "solid_fuel") +``` + +If a channel requires more configuration, the flexibility of the API comes into play. + +```elixir +Vl.new(width: 500, height: 300, title: "Fuels") +|> Vl.data_from_values(fuels, only: ["total", "solid_fuel"]) +|> Vl.mark(:point) +|> Vl.encode_field(:x, "total", type: :quantitative) +|> Vl.encode_field(:y, "solid_fuel", type: :quantitative) +|> Vl.encode_field(:color, "total", type: :quantitative, scale: [scheme: "category10"]) +``` + +In the example above, we have a color channel that requires more customization. While it's possible to get the exact same plot using only the shorthand API, the expressiveness may be sacrificed. It's precisely in these cases that using the API together with the main module will probably result in more readable code. Let's take a look and compare the possible combinations between the API and the `VegaLite` module. + +```elixir +# Using mainly the shorthand api +Vl.new(width: 500, height: 300, title: "Combined") +|> Data.chart(fuels, :point, + x: "total", + y: "solid_fuel", + color: [field: "total", type: :quantitative, scale: [scheme: "category10"]] +) +``` + +```elixir +# Piping the shorthand api into a enconde_field +Vl.new(width: 500, height: 300, title: "Fuels") +|> Data.chart(fuels, :point, x: "total", y: "solid_fuel") +|> Vl.encode_field(:color, "total", type: :quantitative, scale: [scheme: "category10"]) +``` + +As we can see, the API is flexible enough to allow it to be piped from `VegaLite`, piped to `VegaLite` or both! In principle, you are free to choose the code that best suits your needs, ideally aiming for a balance between conciseness and expressiveness. + +## Specialized plots + +Specialized plots provide high-level abstractions for commonly used complex charts. + +### Heatmap + +Plotting heatmaps directly from VegaLite requires a lot of code. + +For a more concrete example, we will use precomputed data from the correlation matrix of the wine dataset. + + + +```elixir +corr_to_plot = %{ + "corr_val" => [1.0, -0.02, 0.29, 0.09, 0.02, -0.05, 0.09, 0.27, -0.43, -0.02, + -0.12, -0.11, -0.02, 1.0, -0.15, 0.06, 0.07, -0.1, 0.09, 0.03, -0.03, -0.04, + 0.07, -0.19, 0.29, -0.15, 1.0, 0.09, 0.11, 0.09, 0.12, 0.15, -0.16, 0.06, + -0.08, -0.01, 0.09, 0.06, 0.09, 1.0, 0.09, 0.3, 0.4, 0.84, -0.19, -0.03, + -0.45, -0.1, 0.02, 0.07, 0.11, 0.09, 1.0, 0.1, 0.2, 0.26, -0.09, 0.02, -0.36, + -0.21, -0.05, -0.1, 0.09, 0.3, 0.1, 1.0, 0.62, 0.29, 0.0, 0.06, -0.25, 0.01, + 0.09, 0.09, 0.12, 0.4, 0.2, 0.62, 1.0, 0.53, 0.0, 0.13, -0.45, -0.17, 0.27, + 0.03, 0.15, 0.84, 0.26, 0.29, 0.53, 1.0, -0.09, 0.07, -0.78, -0.31, -0.43, + -0.03, -0.16, -0.19, -0.09, 0.0, 0.0, -0.09, 1.0, 0.16, 0.12, 0.1, -0.02, + -0.04, 0.06, -0.03, 0.02, 0.06, 0.13, 0.07, 0.16, 1.0, -0.02, 0.05, -0.12, + 0.07, -0.08, -0.45, -0.36, -0.25, -0.45, -0.78, 0.12, -0.02, 1.0, 0.44, + -0.11, -0.19, -0.01, -0.1, -0.21, 0.01, -0.17, -0.31, 0.1, 0.05, 0.44, 1.0], + "x" => ["fixed acidity", "volatile acidity", "citric acid", "residual sugar", + "chlorides", "free sulfur dioxide", "total sulfur dioxide", "density", "pH", + "sulphates", "alcohol", "quality", "fixed acidity", "volatile acidity", + "citric acid", "residual sugar", "chlorides", "free sulfur dioxide", + "total sulfur dioxide", "density", "pH", "sulphates", "alcohol", "quality", + "fixed acidity", "volatile acidity", "citric acid", "residual sugar", + "chlorides", "free sulfur dioxide", "total sulfur dioxide", "density", "pH", + "sulphates", "alcohol", "quality", "fixed acidity", "volatile acidity", + "citric acid", "residual sugar", "chlorides", "free sulfur dioxide", + "total sulfur dioxide", "density", "pH", "sulphates", "alcohol", "quality", + "fixed acidity", "volatile acidity", "citric acid", "residual sugar", + "chlorides", "free sulfur dioxide", "total sulfur dioxide", "density", "pH", + "sulphates", "alcohol", "quality", "fixed acidity", "volatile acidity", + "citric acid", "residual sugar", "chlorides", "free sulfur dioxide", + "total sulfur dioxide", "density", "pH", "sulphates", "alcohol", "quality", + "fixed acidity", "volatile acidity", "citric acid", "residual sugar", + "chlorides", "free sulfur dioxide", "total sulfur dioxide", "density", "pH", + "sulphates", "alcohol", "quality", "fixed acidity", "volatile acidity", + "citric acid", "residual sugar", "chlorides", "free sulfur dioxide", + "total sulfur dioxide", "density", "pH", "sulphates", "alcohol", "quality", + "fixed acidity", "volatile acidity", "citric acid", "residual sugar", + "chlorides", "free sulfur dioxide", "total sulfur dioxide", "density", "pH", + "sulphates", "alcohol", "quality", "fixed acidity", "volatile acidity", + "citric acid", "residual sugar", "chlorides", "free sulfur dioxide", + "total sulfur dioxide", "density", "pH", "sulphates", "alcohol", "quality", + "fixed acidity", "volatile acidity", "citric acid", "residual sugar", + "chlorides", "free sulfur dioxide", "total sulfur dioxide", "density", "pH", + "sulphates", "alcohol", "quality", "fixed acidity", "volatile acidity", + "citric acid", "residual sugar", "chlorides", "free sulfur dioxide", + "total sulfur dioxide", "density", "pH", "sulphates", "alcohol", "quality"], + "y" => ["fixed acidity", "fixed acidity", "fixed acidity", "fixed acidity", + "fixed acidity", "fixed acidity", "fixed acidity", "fixed acidity", + "fixed acidity", "fixed acidity", "fixed acidity", "fixed acidity", + "volatile acidity", "volatile acidity", "volatile acidity", + "volatile acidity", "volatile acidity", "volatile acidity", + "volatile acidity", "volatile acidity", "volatile acidity", + "volatile acidity", "volatile acidity", "volatile acidity", "citric acid", + "citric acid", "citric acid", "citric acid", "citric acid", "citric acid", + "citric acid", "citric acid", "citric acid", "citric acid", "citric acid", + "citric acid", "residual sugar", "residual sugar", "residual sugar", + "residual sugar", "residual sugar", "residual sugar", "residual sugar", + "residual sugar", "residual sugar", "residual sugar", "residual sugar", + "residual sugar", "chlorides", "chlorides", "chlorides", "chlorides", + "chlorides", "chlorides", "chlorides", "chlorides", "chlorides", "chlorides", + "chlorides", "chlorides", "free sulfur dioxide", "free sulfur dioxide", + "free sulfur dioxide", "free sulfur dioxide", "free sulfur dioxide", + "free sulfur dioxide", "free sulfur dioxide", "free sulfur dioxide", + "free sulfur dioxide", "free sulfur dioxide", "free sulfur dioxide", + "free sulfur dioxide", "total sulfur dioxide", "total sulfur dioxide", + "total sulfur dioxide", "total sulfur dioxide", "total sulfur dioxide", + "total sulfur dioxide", "total sulfur dioxide", "total sulfur dioxide", + "total sulfur dioxide", "total sulfur dioxide", "total sulfur dioxide", + "total sulfur dioxide", "density", "density", "density", "density", + "density", "density", "density", "density", "density", "density", "density", + "density", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", + "pH", "sulphates", "sulphates", "sulphates", "sulphates", "sulphates", + "sulphates", "sulphates", "sulphates", "sulphates", "sulphates", "sulphates", + "sulphates", "alcohol", "alcohol", "alcohol", "alcohol", "alcohol", + "alcohol", "alcohol", "alcohol", "alcohol", "alcohol", "alcohol", "alcohol", + "quality", "quality", "quality", "quality", "quality", "quality", "quality", + "quality", "quality", "quality", "quality", "quality"] +} +|> Explorer.DataFrame.new() +``` + +```elixir +Vl.new(title: "Correlation matrix", width: 600, height: 600) +|> Vl.layers([ + Vl.new() + |> Vl.data_from_values(corr_to_plot) + |> Vl.mark(:rect) + |> Vl.encode_field(:x, "x", type: :nominal) + |> Vl.encode_field(:y, "y", type: :nominal) + |> Vl.encode_field(:color, "corr_val", type: :quantitative), + Vl.new() + |> Vl.data_from_values(corr_to_plot) + |> Vl.mark(:text) + |> Vl.encode_field(:x, "x", type: :nominal) + |> Vl.encode_field(:y, "y", type: :nominal) + |> Vl.encode_field(:text, "corr_val", type: :quantitative) +]) +``` + +We can use our already explored shorthand API to simplify it. + +```elixir +Vl.new(title: "Correlation matrix", width: 600, height: 600) +|> Vl.layers([ + Data.chart(corr_to_plot, :rect, + x: [field: "x", type: :nominal], + y: [field: "y", type: :nominal], + color: "corr_val" + ), + Data.chart(corr_to_plot, :text, + x: [field: "x", type: :nominal], + y: [field: "y", type: :nominal], + text: "corr_val" + ) +]) +``` + +Or we can go even further and use the `VegaLite.Data.heatmap/2` function alone or the `VegaLite.Data.heatmap/3` function in combination with `VegaLite`. + +The specialized plots follow the same principle as the shorthand API, they can be combined with the main module, and each argument can also take a list of options to override the defaults. + +```elixir +Vl.new(title: "Correlation matrix", width: 600, height: 600) +|> Data.heatmap(corr_to_plot, + x: "x", + y: "y", + color: "corr_val", + text: "corr_val" +) +``` diff --git a/lib/vega_lite/data.ex b/lib/vega_lite/data.ex new file mode 100644 index 0000000..abf481c --- /dev/null +++ b/lib/vega_lite/data.ex @@ -0,0 +1,231 @@ +defmodule VegaLite.Data do + @moduledoc """ + Data is a VegaLite module designed to provide a shorthand API for commonly used charts and + high-level abstractions for specialized plots. + + Optionally accepts and always returns a valid `VegaLite` spec, fostering flexibility to be used + alone or in combination with the `VegaLite` module at any level and at any point. + + It relies on internal type inference, and although all options can be overridden, + only data that implements the [Table.Reader](https://hexdocs.pm/table/Table.Reader.html) + protocol is supported. + """ + + alias VegaLite, as: Vl + + @doc """ + Returns the specification for a given data and a list of fields to be encoded. + + See `chart/3`. + """ + @spec chart(Table.Reader.t(), keyword()) :: VegaLite.t() + def chart(data, fields), do: chart(Vl.new(), data, fields) + + @doc """ + Returns the specification for the a given data, a mark and a list of fields to be encoded. + + Each argument that is not a `VegaLite` specification nor a data is accepted as the argument + itself or a keyword list of options. All options must follow the specifications of the + `VegaLite` module. + + ## Examples + + data = [ + %{"category" => "A", "score" => 28}, + %{"category" => "B", "score" => 55} + ] + + Data.chart(data, :bar, x: "category", y: "score") + + The above examples achieves the same results as the example below. + + Vl.new() + |> Vl.data_from_values(data, only: ["category", "score"]) + |> Vl.mark(:bar) + |> Vl.encode_field(:x, "category", type: :nominal) + |> Vl.encode_field(:y, "score", type: :quantitative) + """ + @spec chart(VegaLite.t(), Table.Reader.t(), keyword()) :: VegaLite.t() + def chart(%Vl{} = vl, data, fields) do + cols = columns_for(data) + used_fields = used_fields(fields) + root = vl |> Vl.data_from_values(data, only: used_fields) + build_fields(fields, root, cols) + end + + @spec chart(Table.Reader.t(), atom() | keyword(), keyword()) :: VegaLite.t() + def chart(data, mark, fields), do: chart(Vl.new(), data, mark, fields) + + @doc """ + Same as chart/3 but receives a valid `VegaLite` specification as a first argument. + + ## Examples + + data = [ + %{"category" => "A", "score" => 28}, + %{"category" => "B", "score" => 55} + ] + + Vl.new(title: "With title") + |> Data.chart(data, :bar, x: "category", y: "score") + + Vl.new(title: "With title") + |> Vl.mark(:bar) + |> Data.chart(data, x: "category", y: "score") + + The above example achieves the same results as the example below. + + Vl.new(title: "With title") + |> Vl.data_from_values(data, only: ["category", "score"]) + |> Vl.mark(:bar) + |> Vl.encode_field(:x, "category", type: :nominal) + |> Vl.encode_field(:y, "score", type: :quantitative) + """ + @spec chart(VegaLite.t(), Table.Reader.t(), atom() | keyword(), keyword()) :: VegaLite.t() + def chart(vl, data, mark, fields) do + cols = columns_for(data) + used_fields = used_fields(fields) + root = vl |> Vl.data_from_values(data, only: used_fields) |> encode_mark(mark) + build_fields(fields, root, cols) + end + + @doc """ + Returns the specification of a heat map for a given data and a list of fields to be encoded. + + As a specialized chart, the heatmap expects an `:x` and `:y` and optionally a `:color` and a + `:text` field. Defaults to `:nominal` for the axes and `:quantitative` for color and text if + types are not specified. + + ## Examples + + data = [ + %{"category" => "A", "score" => 28}, + %{"category" => "B", "score" => 55} + ] + + Data.heatmap(data, x: "category", y: "score", color: "score", text: "category") + + """ + @spec heatmap(Table.Reader.t(), keyword()) :: VegaLite.t() + def heatmap(data, fields), do: heatmap(Vl.new(), data, fields) + + @doc """ + Same as heatmap/2, but takes a valid `VegaLite` specification as the first argument. + + ## Examples + + data = [ + %{"category" => "A", "score" => 28}, + %{"category" => "B", "score" => 55} + ] + + Vl.new(title: "Heatmap", width: 500) + |> Data.heatmap(data, x: "category", y: "score", color: "score", text: "category") + """ + @spec heatmap(VegaLite.t(), Table.Reader.t(), keyword()) :: VegaLite.t() + def heatmap(vl, data, fields) do + fields = standardize_fields(fields) |> Enum.map(&heatmap_defaults/1) + + case {fields[:x], fields[:y], fields[:text]} do + {nil, _, _} -> raise ArgumentError, "the x axis is required to plot a heatmap" + {_, nil, _} -> raise ArgumentError, "the y axis is required to plot a heatmap" + {_, _, nil} -> chart(vl, data, :rect, fields) + _ -> annotated_heatmap(vl, data, fields) + end + end + + defp annotated_heatmap(vl, data, fields) do + text_fields = Keyword.take(fields, [:text, :x, :y]) + used_fields = used_fields(fields) + rect_fields = Keyword.delete(fields, :text) + + vl + |> Vl.data_from_values(data, only: used_fields) + |> Vl.layers([layer(data, :rect, rect_fields), layer(data, :text, text_fields)]) + end + + defp heatmap_defaults({field, opts}) when field in [:x, :y] do + {field, Keyword.put_new(opts, :type, :nominal)} + end + + defp heatmap_defaults({field, opts}) when field in [:color, :text] do + {field, Keyword.put_new(opts, :type, :quantitative)} + end + + defp encode_mark(vl, opts) when is_list(opts) do + {mark, opts} = Keyword.pop!(opts, :type) + Vl.mark(vl, mark, opts) + end + + defp encode_mark(vl, mark) when is_atom(mark), do: Vl.mark(vl, mark) + + defp encode_field(schema, cols, field, opts) do + {col, opts} = Keyword.pop!(opts, :field) + opts = Keyword.put_new_lazy(opts, :type, fn -> cols[col] end) + Vl.encode_field(schema, field, col, opts) + end + + defp layer(data, mark, fields) do + cols = columns_for(data) + root = Vl.new() |> encode_mark(mark) + build_fields(fields, root, cols) + end + + defp used_fields(fields) do + for {_key, field} <- fields, uniq: true do + if is_list(field), do: field[:field], else: field + end + end + + defp build_fields(fields, root, cols) do + fields + |> standardize_fields() + |> Enum.reduce(root, fn {field, opts}, acc -> encode_field(acc, cols, field, opts) end) + end + + defp standardize_fields(fields) do + for {field, opts} <- fields do + if is_list(opts), do: {field, opts}, else: {field, [field: opts]} + 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 -> {to_string(column), type} end) |> Map.new() + else + _ -> nil + end + end + + defp implements?(protocol, value), do: protocol.impl_for(value) != nil + + 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)) +end diff --git a/mix.exs b/mix.exs index 7327e40..9740f42 100644 --- a/mix.exs +++ b/mix.exs @@ -35,7 +35,8 @@ defmodule VegaLite.MixProject do [ main: "VegaLite", source_url: "https://github.com/livebook-dev/vega_lite", - source_ref: "v#{@version}" + source_ref: "v#{@version}", + extras: ["guides/data.livemd"] ] end diff --git a/test/vega_lite/data_test.exs b/test/vega_lite/data_test.exs new file mode 100644 index 0000000..8d0c8b6 --- /dev/null +++ b/test/vega_lite/data_test.exs @@ -0,0 +1,309 @@ +defmodule VegaLite.DataTest do + use ExUnit.Case + + alias VegaLite.Data + alias VegaLite, as: Vl + + @data [ + %{"height" => 170, "weight" => 80, "width" => 10, "unused" => "a"}, + %{"height" => 190, "weight" => 85, "width" => 20, "unused" => "b"} + ] + + describe "shorthand api" do + test "single field" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height"]) + |> Vl.mark(:bar) + |> Vl.encode_field(:x, "height", type: :quantitative) + + assert vl == Data.chart(@data, :bar, x: "height") + end + + test "multiple fields" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height", "weight"]) + |> Vl.mark(:bar) + |> Vl.encode_field(:x, "height", type: :quantitative) + |> Vl.encode_field(:y, "weight", type: :quantitative) + + assert vl == Data.chart(@data, :bar, x: "height", y: "weight") + end + + test "mark with options" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height"]) + |> Vl.mark(:point, line: true) + |> Vl.encode_field(:x, "height", type: :quantitative) + + assert vl == Data.chart(@data, [type: :point, line: true], x: "height") + end + + test "mark from pipe" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height"]) + |> Vl.mark(:point, line: true) + |> Vl.encode_field(:x, "height", type: :quantitative) + + assert vl == Vl.new() |> Vl.mark(:point, line: true) |> Data.chart(@data, x: "height") + end + + test "pipe to mark" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height"]) + |> Vl.encode_field(:x, "height", type: :quantitative) + |> Vl.mark(:point, line: true) + + assert vl == Vl.new() |> Data.chart(@data, x: "height") |> Vl.mark(:point, line: true) + assert vl == Data.chart(@data, x: "height") |> Vl.mark(:point, line: true) + end + + test "single field with options" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height"]) + |> Vl.mark(:bar) + |> Vl.encode_field(:x, "height", type: :nominal) + + assert vl == Data.chart(@data, :bar, x: [field: "height", type: :nominal]) + end + + test "multiple fields with options" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height", "weight"]) + |> Vl.mark(:bar) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + + assert vl == + Data.chart(@data, :bar, + x: [field: "height", type: :nominal], + y: [field: "weight", type: :nominal] + ) + end + + test "nested field options" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height"]) + |> Vl.mark(:bar) + |> Vl.encode_field(:x, "height", type: :quantitative) + |> Vl.encode_field(:color, "height", type: :quantitative, scale: [scheme: "category10"]) + + assert vl == + Data.chart(@data, :bar, + x: "height", + color: [field: "height", type: :quantitative, scale: [scheme: "category10"]] + ) + end + + test "piped from VegaLite" do + vl = + Vl.new(title: "With title") + |> Vl.data_from_values(@data, only: ["height"]) + |> Vl.mark(:point) + |> Vl.encode_field(:x, "height", type: :quantitative) + + assert vl == Vl.new(title: "With title") |> Data.chart(@data, :point, x: "height") + end + + test "piped into VegaLite" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height"]) + |> Vl.mark(:point) + |> Vl.encode_field(:x, "height", type: :quantitative) + |> Vl.encode_field(:color, "height", type: :quantitative, scale: [scheme: "category10"]) + + assert vl == + Data.chart(@data, :point, x: "height") + |> Vl.encode_field(:color, "height", + type: :quantitative, + scale: [scheme: "category10"] + ) + end + + test "piped from and into VegaLite" do + vl = + Vl.new(title: "With title") + |> Vl.data_from_values(@data, only: ["height"]) + |> Vl.mark(:point) + |> Vl.encode_field(:x, "height", type: :quantitative) + |> Vl.encode_field(:color, "height", type: :quantitative, scale: [scheme: "category10"]) + + assert vl == + Vl.new(title: "With title") + |> Data.chart(@data, :point, x: "height") + |> Vl.encode_field(:color, "height", + type: :quantitative, + scale: [scheme: "category10"] + ) + end + + test "combined with layers" do + vl = + Vl.new(title: "Heatmap") + |> Vl.layers([ + Vl.new() + |> Vl.data_from_values(@data, only: ["height", "weight"]) + |> Vl.mark(:rect) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + |> Vl.encode_field(:color, "height", type: :quantitative), + Vl.new() + |> Vl.data_from_values(@data, only: ["height", "weight"]) + |> Vl.mark(:text) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + |> Vl.encode_field(:text, "height", type: :quantitative) + ]) + + sh = + Vl.new(title: "Heatmap") + |> Vl.layers([ + Data.chart(@data, :rect, + x: [field: "height", type: :nominal], + y: [field: "weight", type: :nominal], + color: "height" + ), + Data.chart(@data, :text, + x: [field: "height", type: :nominal], + y: [field: "weight", type: :nominal], + text: "height" + ) + ]) + + assert vl == sh + end + end + + describe "heatmap" do + test "simple heatmap" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height", "weight"]) + |> Vl.mark(:rect) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + + assert vl == Data.heatmap(@data, x: "height", y: "weight") + end + + test "simple heatmap with color" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height", "weight"]) + |> Vl.mark(:rect) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + |> Vl.encode_field(:color, "height", type: :quantitative) + + assert vl == Data.heatmap(@data, x: "height", y: "weight", color: "height") + end + + test "heatmap with color and text" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height", "weight"]) + |> Vl.layers([ + Vl.new() + |> Vl.mark(:rect) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + |> Vl.encode_field(:color, "height", type: :quantitative), + Vl.new() + |> Vl.mark(:text) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + |> Vl.encode_field(:text, "height", type: :quantitative) + ]) + + assert vl == Data.heatmap(@data, x: "height", y: "weight", color: "height", text: "height") + end + + test "heatmap with title" do + vl = + Vl.new(title: "Heatmap") + |> Vl.data_from_values(@data, only: ["height", "weight"]) + |> Vl.layers([ + Vl.new() + |> Vl.mark(:rect) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + |> Vl.encode_field(:color, "height", type: :quantitative), + Vl.new() + |> Vl.mark(:text) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + |> Vl.encode_field(:text, "height", type: :quantitative) + ]) + + assert vl == + Vl.new(title: "Heatmap") + |> Data.heatmap(@data, x: "height", y: "weight", color: "height", text: "height") + end + + test "heatmap with specified types" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height", "weight"]) + |> Vl.layers([ + Vl.new() + |> Vl.mark(:rect) + |> Vl.encode_field(:x, "height", type: :quantitative) + |> Vl.encode_field(:y, "weight", type: :quantitative) + |> Vl.encode_field(:color, "height", type: :nominal), + Vl.new() + |> Vl.mark(:text) + |> Vl.encode_field(:x, "height", type: :quantitative) + |> Vl.encode_field(:y, "weight", type: :quantitative) + |> Vl.encode_field(:text, "height", type: :quantitative) + ]) + + assert vl == + Data.heatmap(@data, + x: [field: "height", type: :quantitative], + y: [field: "weight", type: :quantitative], + color: [field: "height", type: :nominal], + text: "height" + ) + end + + test "heatmap with a text field different from the axes" do + vl = + Vl.new() + |> Vl.data_from_values(@data, only: ["height", "weight", "width"]) + |> Vl.layers([ + Vl.new() + |> Vl.mark(:rect) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + |> Vl.encode_field(:color, "height", type: :quantitative), + Vl.new() + |> Vl.mark(:text) + |> Vl.encode_field(:x, "height", type: :nominal) + |> Vl.encode_field(:y, "weight", type: :nominal) + |> Vl.encode_field(:text, "width", type: :quantitative) + ]) + + assert vl == Data.heatmap(@data, x: "height", y: "weight", color: "height", text: "width") + end + + test "raises an error when the x axis is not given" do + assert_raise ArgumentError, "the x axis is required to plot a heatmap", fn -> + Data.heatmap(@data, y: "y") + end + end + + test "raises an error when the y axis is not given" do + assert_raise ArgumentError, "the y axis is required to plot a heatmap", fn -> + Data.heatmap(@data, x: "x", text: "text") + end + end + end +end