Class: DataModel

DataModel~DataModel(data, schema, optionsopt)

new DataModel(data, schema, optionsopt)

Creates a new DataModel instance by providing data and schema. Data could be in the form of - Flat JSON - DSV String - 2D Array By default DataModel finds suitable adapter to serialize the data. DataModel also expects a schema for identifying the variables present in data.
Parameters:
Name Type Attributes Description
data Array.<Object> | string | Array.<Array> Input data in any of the mentioned formats. Checkout this example for practical example on how feed different data format.
schema Array.<Schema> Defination of the variables. Order of the variables in data and order of the variables in schema has to be same.
options object <optional>
Optional arguments to specify more settings regarding the creation part
Properties
Name Type Attributes Default Description
name string <optional>
Name of the datamodel instance. If no name is given an auto generated name is assigned to the instance.
fieldSeparator string <optional>
',' specify field separator type if the data is of type dsv string.
Source:
Example
const DataModel = muze.DataModel; // Retrieves reference to DataModel from muze namespace
 const data = [
     { Name:'chevrolet chevelle malibu', Miles_per_Gallon:18, Cylinders:8, Horsepower:130, Year:'1970' },
     { Name:'ford fiesta', Miles_per_Gallon:36.1, Cylinders:4, Horsepower:66, Year:'1978' },
     { Name:'bmw 320i', Miles_per_Gallon:21.5, Cylinders:4, Horsepower:110, Year:'1977' },
     { Name:'chevrolet chevelle malibu', Miles_per_Gallon:18, Cylinders:8, Horsepower:130, Year:'1970' },
     { Name:'ford fiesta', Miles_per_Gallon:36.1, Cylinders:4, Horsepower:66, Year:'1978' },
     { Name:'bmw 320i', Miles_per_Gallon:21.5, Cylinders:4, Horsepower:110, Year:'1977' }
 ];
 const schema = [
     { name: 'Name', type: 'dimension' },
     { name: 'Miles_per_Gallon', type: 'measure', unit : 'gallon', numberformat: val => `${val}G`},
     { name: 'Cylinders', type: 'dimension' },
     { name: 'Horsepower', type: 'measure' },
     { name: 'Year', type: 'dimension', subtype: 'datetime', format: '%Y' }
];
const dm = new DataModel(data, schema, { name: 'Cars' });
printDM(dm); // internal function to print datamodel, available only in this interface

Members

(static) Reducers

Reducers are simple functions which reduces an array of numbers to a representative number of the set. Like an array of numbers `[10, 20, 5, 15]` can be reduced to `12.5` if average / mean reducer function is applied. All the measure fields in datamodel (variables in data) needs a reducer to handle aggregation.
Source:

Methods

calculateVariable(schema, resolver) → {DataModel}

Creates a new variable calculated from existing variable. This method expects definition of the newly created variable and a function which resolves value of the new variable from existing variables. Creates a new measure based on existing variables
Parameters:
Name Type Description
schema Schema Schema of newly defined variable
resolver VariableResolver Resolver format to resolve the current variable
Source:
Returns:
Instance of DataModel with the new field
Type
DataModel
Examples
//@preamble_start
 Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
     const data = params[0];
     const schema = params[1];
     const dm = new muze.DataModel(data, schema);
 //@preamble_end
 // DataModel instance is created from https://www.charts.com/static/cars.json data,
 // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm.
 const outputDM = dm.calculateVariable({
     name: 'powerToWeight',
     type: 'measure' // Schema of variable
 }, ['Horsepower', 'Weight_in_lbs', (hp, weight) => hp / weight ]);
 //@preamble_start
 printDM(outputDM);
 });
 //@preamble_end
//@preamble_start
 Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
     const data = params[0];
     const schema = params[1];
     const dm = new muze.DataModel(data, schema);
 //@preamble_end
 // DataModel instance is created from https://www.charts.com/static/cars.json data,
 // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm.
 const outputDM= dm.calculateVariable(
    {
      name: 'Efficiency',
      type: 'dimension'
    }, ['Horsepower', (hp) => {
     if (hp < 80) { return 'low'; }
     else if (hp < 120) { return 'moderate'; }
     else { return 'high' }
 }]);
 //@preamble_start
 printDM(outputDM);
 });
 //@preamble_end

getData(optionsopt) → {Array}

Retrieve the data attached to an instance in JSON format.
Parameters:
Name Type Attributes Description
options Object <optional>
Options to control how the raw data is to be returned.
Properties
Name Type Attributes Default Description
order string <optional>
'row' Defines if data is retieved in row order or column order. Possible values are `'rows'` and `'columns'`
formatter function <optional>
null Formats the output data. This expects an object, where the keys are the name of the variable needs to be formatted. The formatter function is called for each row passing the value of the cell for a particular row as arguments. The formatter is a function in the form of ``` function (value, rowId, schema) => { ... } ``` Know more about Fomatter.
Source:
Returns:
Returns a multidimensional array of the data with schema. The return format looks like ``` { data, schema } ```
Type
Array
Example
//@preamble_start
 Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
     const data = params[0];
     const schema = params[1];
     const dm = new muze.DataModel(data, schema);
 //@preamble_end
 // DataModel instance is created from https://www.charts.com/static/cars.json data,
 // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm.
 const serializedData = dm.getData({
     order: 'column',
     formatter: {
         origin: (val) => val === 'European Union' ? 'EU' : val
     }
 });
 console.log(serializedData);
 //@preamble_start
 });
 //@preamble_end

groupBy(fieldsArr, reducersopt) → {DataModel}

Groups the data using particular dimensions by reducing measures. It expects a list of dimensions using which it projects the datamodel and perform aggregations to reduce the duplicate tuples. Refer this document to know the intuition behind groupBy. DataModel by default provides definition of few Reducers for reducing a measure when aggregation is required for `groupBy`. User defined reducers can also be registered. This is the chained implementation of `groupBy`. `groupBy` also supports composability.
Parameters:
Name Type Attributes Default Description
fieldsArr Array.<string> Array containing the name of dimensions using which groupBy should happen.
reducers Object <optional>
{} A simple key value pair whose key is the variable name and value is the name of the reducer. If its not passed, or any variable is ommitted from the object, default aggregation function is used from the schema of the variable.
Source:
Returns:
Returns a new DataModel instance after performing the groupby.
Type
DataModel
Example
//@preamble_start
 Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
     const data = params[0];
     const schema = params[1];
     const dm = new muze.DataModel(data, schema);
 //@preamble_end
 // DataModel instance is created from https://www.charts.com/static/cars.json data,
 // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm.
 const outputDM = dm.groupBy(['Year'], { horsepower: 'max' } );
 //@preamble_start
 printDM(outputDM);
 });
 //@preamble_end

on(eventName, callback) → {DataModel}

Associates a callback with an event name.
Parameters:
Name Type Description
eventName string The name of the event.
callback function The callback to invoke.
Source:
Returns:
Returns this current DataModel instance itself.
Type
DataModel

propagate(identifiers, payload) → {DataModel}

Propagates changes across all the connected DataModel instances.
Parameters:
Name Type Description
identifiers Array A list of identifiers that were interacted with.
payload Object The interaction specific details.
Source:
Returns:
DataModel instance.
Type
DataModel

sort(sortingDetails) → {DataModel}

Performs sorting according to the specified sorting details.Like every other operator it doesn't mutate the current DataModel instance on which it was called, instead returns a new DataModel instance containing the sorted data. DataModel support multi level sorting by listing the variables using which sorting needs to be performed and the type of sorting `ASC` or `DESC`. In the following example, data is sorted by `Origin` field in `DESC` order in first level followed by another level of sorting by `Acceleration` in `ASC` order.
Parameters:
Name Type Description
sortingDetails Array.<Array> Sorting details based on which the sorting will be performed.
Source:
Returns:
Returns a new instance of DataModel with sorted data.
Type
DataModel
Examples
//@preamble_start
 Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
 const data = params[0];
 const schema = params[1];
 const dm = new muze.DataModel(data, schema);
 //@preamble_end
 // DataModel instance is created from https://www.charts.com/static/cars.json data,
 // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm.
 let outputDM = dm.sort([
     ['Origin', 'DESC'],
     ['Acceleration'] // Default value is ASC
 ]);
 //@preamble_start
 printDM(outputDM);
 });
 //@preamble_end
//@preamble_start
 Promise.all([loadData('/static/cars.json'), loadData('/static/cars-schema.json')]).then(function (params) {
 const data = params[0];
 const schema = params[1];
 const DataModel = muze.DataModel;
 const dm = new muze.DataModel(data, schema);
 //@preamble_end
 // DataModel instance is created from https://www.charts.com/static/cars.json data,
 // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm. DataModel is extracted
 // from muze namespace and assigned to DataModel variable.
 const avg = DataModel.Stats.avg;
 const outputDM = dm.sort([
     ['Origin', ['Acceleration', (a, b) => avg(a.Acceleration) - avg(b.Acceleration)]]
 ]);
 //@preamble_start
 printDM(outputDM);
 });
 //@preamble_end

unsubscribe(eventName) → {DataModel}

Unsubscribes the callbacks for the provided event name.
Parameters:
Name Type Description
eventName string The name of the event to unsubscribe.
Source:
Returns:
Returns the current DataModel instance itself.
Type
DataModel