ContEx v0.2.0 Contex.Dataset View Source

Dataset is a simple wrapper around a datasource for plotting charts.

Dataset marshalls a couple of different data structures into a consistent form for consumption by the chart plotting functions. For example, it allows a list of lists or a list of tuples to be treated the same.

The most sensible way to work with a dataset is to provide column headers - it makes code elsewhere readable. If you don't want to, you can also refer to columns by index.

Dataset provides a few convenience functions for calculating data extents for a column, extracting unique values from columns, calculating combined extents for multiple columns (handy when plotting bar charts) and guessing column type (handy when determining whether to use a Contex.TimeScale or a Contex.ContinuousLinearScale).

The easiest pattern to create a dataset is:

iex> data = [
...>        {0.0, 0.0, "Hippo"},
...>        {0.5, 0.3, "Turtle"},
...>        {0.4, 0.3, "Turtle"},
...>        {0.2, 0.3, "Rabbit"}
...>        ] # Wherever your data comes from (e.g. could be straight from Ecto)
...> dataset = Dataset.new(data, ["x", "y", "category"]) # Attach descriptive headers
...> Dataset.column_extents(dataset, "x") # Get extents for a named column
{0.0, 0.5}
iex> Dataset.column_name(dataset, 0) # Get name of column by index
"x"
iex> cat_col = Dataset.column_index(dataset, "category") # Get index of column by name
2
iex> Enum.map(dataset.data, fn row -> # Enumerate values in a column
...>    Dataset.value(row, cat_col)
...> end)
["Hippo", "Turtle", "Turtle", "Rabbit"]
iex> Dataset.unique_values(dataset, "category") # Extract unique values for legends etc.
["Hippo", "Turtle", "Rabbit"]

While Dataset gives facilities to map between names and column indexes, you can only access data values via index. This is so that you don't have expensive mappings in tight loops.

Note There are very few validation checks (for example, to checks that number of headers supplied matches) the size of each array or tuple in the data. If there are any issues finding a value, nil is returned.

Link to this section Summary

Functions

Calculates the min and max value in the specified column

Looks up the index for a given column name. Returns nil if not found.

Looks up the column name for a given index.

Calculates the data extents for the sum of the columns supplied.

Tries to guess the data type for a column based on contained data.

Creates a new Dataset wrapper around some data.

Creates a new Dataset wrapper around some data with headers.

Optionally sets a title.

Extracts a list of unique values in the given column.

Looks up the value from a row based on the column index.

Link to this section Types

Link to this type

column_name()

View Source
column_name() :: String.t() | integer()
Link to this type

column_type()

View Source
column_type() :: :datetime | :number | :string | :unknown | nil

Link to this section Functions

Link to this function

column_extents(dataset, column_name)

View Source
column_extents(Contex.Dataset.t(), column_name()) :: {any(), any()}

Calculates the min and max value in the specified column

Link to this function

column_index(dataset, column_name)

View Source
column_index(Contex.Dataset.t(), column_name()) :: nil | integer()

Looks up the index for a given column name. Returns nil if not found.

Link to this function

column_name(dataset, column_index)

View Source
column_name(Contex.Dataset.t(), integer()) :: column_name()

Looks up the column name for a given index.

If there are no headers, or the index is outside the range of the headers the requested index is returned.

Link to this function

combined_column_extents(dataset, column_names)

View Source
combined_column_extents(Contex.Dataset.t(), [column_name()]) :: {any(), any()}

Calculates the data extents for the sum of the columns supplied.

It is the equivalent of evaluating the extents of a calculated row where the calculating is the sum of the values identified by column_names.

Link to this function

guess_column_type(dataset, column_name)

View Source
guess_column_type(Contex.Dataset.t(), column_name()) :: column_type()

Tries to guess the data type for a column based on contained data.

Looks through the rows and returns the first match it can find.

Creates a new Dataset wrapper around some data.

Data is expected to be a list of tuples of the same size or list of lists of same size. If no headers are specified, columns are access by index.

Creates a new Dataset wrapper around some data with headers.

Data is expected to be a list of tuples of the same size or list of lists of same size.

Optionally sets a title.

Not really used at the moment to be honest, but seemed like a good idea at the time. Might come in handy when overlaying plots.

Link to this function

unique_values(dataset, column_name)

View Source
unique_values(Contex.Dataset.t(), String.t() | integer()) :: [any()]

Extracts a list of unique values in the given column.

Note that the unique values will maintain order of first detection in the data.

Link to this function

value(row, column_index)

View Source
value(row(), integer()) :: any()

Looks up the value from a row based on the column index.

This simply provides a consistent wrapper regardless of whether the data is represented in a tuple or a list.