Table

The DataTable class represents tabular data in Mascot. A data table consists of tuples (rows) and attributes (columns). DataTable objects are usually created by importing Comma Separated Values (CSV) files using the csv function:

let table = await msc.csv("data.csv");

Mascot automatically infers the data type for each attribute and parses values into the inferred type. Small integer values in a column named year are treated as dates. When a table is initialized, Mascot also adds an internal row identifier named mascot_rowId, exposed as msc.ROW_ID. This row id is used internally by data-driven operations such as repeat, divide, densify, and aggregate encodings when no explicit attribute is provided.

Properties

propertyexplanationtypedefault value
data parsed row objects in the tableArray
dimensions non-numeric attributes, sorted by the number of unique valuesArray
id unique id of the data tableString
measures attributes inferred as Number or IntegerArray
name name of the data table, derived from the file name when the table is imported from a URLString
urlURL or path used to create the tableString

Methods

methodexplanationreturn type
addAttr(name, type, values)adds an attribute to the table, or updates the attribute if it already exists. The values array should contain one value for each rowvoid
attrs(type)returns all attribute names when type is omitted; otherwise returns attributes with the specified data typeArray
clone()returns a copy of the table with the same parsed data and attribute typesDataTable
count()returns the number of rows in the tableNumber
encodable(channel)returns attributes that can be encoded by the specified visual channel. For channels such as x, y, width, height, radius, fillColor, strokeColor, and text, Mascot returns both measures and dimensions. For channels such as area and strokeWidth, Mascot returns measuresArray
has(attr)returns true if the specified attribute exists in the data tableBoolean
load(rows)replaces the table’s current rows and recomputes cached summariesvoid
order(attr, values)sets the stored order of unique values for the specified attribute. This is useful when a categorical attribute should follow a custom ordervoid
parseDate(attr, format)parses the specified attribute as dates using a d3 time format string. Parsed dates are stored internally as millisecond timestampsvoid
raw(attr, value)returns the original raw value for a parsed date value; for non-date attributes, returns the value unchangedAny
rows(filters)returns rows matching the given filters. Filters can be exact values, arrays of accepted values, or interval objects with min and/or maxArray
summarize()recomputes attribute summaries for the current table datavoid
summary(attr)returns a summary of the specified attribute. Numeric and date attributes include min, max, extent, and unique; numeric attributes also include mean and median; string attributes include unique; boolean attributes include trueCount and falseCountObject
type(attr)returns the inferred type of the specified attributeData Type
unique(attr)returns the unique values of the specified attributeArray
values(attr)returns the values of the specified attribute for every row in the tableArray

Examples

Inspect table attributes

let table = await msc.csv("cars.csv");

table.attrs();
table.measures;
table.dimensions;
table.count();

Check an attribute before using it

if (table.has("weight(lbs)")) {
    let type = table.type("weight(lbs)");
    let values = table.values("weight(lbs)");
    let summary = table.summary("weight(lbs)");
}

Filter rows

let carsFromJapan = table.rows({ Origin: "Japan" });
let smallerCars = table.rows({ "weight(lbs)": { max: 2500 } });
let selectedCars = table.rows({ Origin: ["Japan", "Europe"] });

Parse a date attribute

let table = await msc.csv("stocks.csv");
table.parseDate("date", "%b %Y");

Transform table data

Data transformations are specified with msc.transform(...) and applied through the Scene class, not directly through DataTable:

let scene = msc.scene();
let table = await msc.csv("car-weight.csv");
let density = scene.derive(table, msc.transform("kde", {
    attribute: "weight(lbs)",
    newAttribute: "weight_density",
    min: 1500,
    interval: 100,
    bandwidth: 10
}));