Top
You are looking at an outdated 7.14.0 version of this document. Switch to the 8.0.1 version to see the up to date information.

class anychart.data.Set Improve this Doc

Extends: anychart.core.Base

Linear data storage.
Data is stored as an array or rows where each row contains several columns (see Listing 1 below). To start working with this storage you need to map columns using anychart.data.Set#mapAs method (you can create as many mappings as you like).
Each field can be a number, a string, a function, an array or an object. Data fields can of any type and they way you read them depends on mapping only: anychart.data.Set#mapAs. Sample mappings are shown in code samples 3, 4 and 5.
Note: To create an instance of this class use anychart.data#set method.

Sample 1. Data notion.
// Col1 Col2 Col3
 [
  [110, 112, 114], // row1
  [210, 212, 214], // row2
  [310, 312, 314], // row3
  [410, 412, 414]  // row4
 ]
// Col1
 [
   114, // row1
   214, // row2
   314, // row3
   414  // row4
 ]
Sample 2. Sample data.
// An array with numbers, strings and functions:
 anychart.data.set([
   20,
   7,
   '10',
   function(smth){ return smth*10; }
   ]);
// An array of arrays:
 anychart.data.set([
   [1, 22, 13],
   [13, 22, 23],
   [17, 22, 33],
   [21, 22, 43]
 ]);
// An array of objects.
 anychart.data.set([
   {name: 'Point 1', value: 10},
   {name: 'Point 2', value: 7},
   {name: 'Point 3', value: 20},
   {name: 'Point 4', value: 14}
 ]);
// A multi-typed array:
 anychart.data.set([
   {value: 10, name: 'Point 1'},
   {value: 7, name: 'Point 2'},
   [20, 'Point 3'],
   [14, 'Point 4'],
   [-14, 'Point 5', function (params) { do_smth; return smth; }],
   '17',
   22,
   function (params) { do_smth; return smth; }
 ]);
Sample 3. Default data mapping. Numbers.
// 'x' is an index of an element and 'value' is its value.
  // Raw data         Mapped as
  [
   1,                        {x: 0, value: 1}
   2,                        {x: 1, value: 2}
   '-5',                     {x: 2, value: -5}
   function(){ return 1;}    {x: 3, value: 1}
  ]
  // so this will not work with OHLC
Sample 4. Default data mapping. Arrays.
// 'x' is an element with the index of 0,
// 'value' is an element with the index of 1,
// 'size' is an element with the index of 2.
// All elements with the index greater than 2 are ignored.
  // Raw data          Mapped as
  [
     [2],                     {x: 2}
     [5, 13],                 {x: 5, value: 13}
     [7, '4', 21],            {x: 7, value: 4, size: 21}
     [11, 21, 34, 45]         {x: 11, value: 21, size: 34}
  ]
  // In case of OHLC
    // 'open' is an element with the index of 0
    // 'high' is an element with the index of 1
    // 'low' is an element with the index of 2
    // 'close' is an element with the index of 3
    //  All elements with the index greater than 3 are ignored.
    [
       [11, 21, 34, 45]         {open: 11, high: 21, low: 34, close: 45}
    ]
Sample 5. Default data mapping. Objects.
// In objects everything corresponds to the names of properties, but you can define several mappings and a priority.
// E.g.: 'x' can be mapped to 'x' and 'value' can be looked for
// in 'value', then 'y', then in 'close'.
  // Raw data                 Mapped as
  [
     {x: 2},                           {x: 2}
     {x: 5, value: 13},                {x: 5, value: 13}
     {x: 7, y: 4, size: 21},           {x: 7, value: 4, size: 21}
     {x: 11, close: 21, size: 34}      {x: 11, value: 21, size: 34}
  ]
  // In case of OHLC
  [
    {open: 11, high: 21, low: 34, close: 45}   {open: 11, high: 21, low: 34, close: 45}
  ]
    // 'open' is an element with the index of 0,
    // 'high' is an element with the index of 1,
    // 'low' is an element with the index of 2,
    // 'close' is an element with the index of 3.
    // All elements with the index greater than 3 are ignored.

Methods Overview

Data
append()Appends new rows to the set. Each argument is a row that will be appended to the Set.
data()Data settings.
getRowsCount()Returns the number of the rows in the current data set.
insert()Inserts the row to the set at the specified position.
mapAs()Defines data mapping.
remove()Removes the row by index.
row()Row of the set by the index
Events
listen()Adds an event listener.
listenOnce()Adds a single time event listener.
removeAllListeners()Removes all listeners.
unlisten()Removes the listener.
unlistenByKey()Removes the listener by the key.

Methods Description

append

Appends new rows to the set. Each argument is a row that will be appended to the Set.

Params:

Name Type Description
var_args * Rows to append.

Returns:

anychart.data.Set - Self instance for method chaining.

Try it:

data

Getter for the data in the Set.

Returns:

Array - Data array of the Set.
Setter for Set data.

Params:

Name Type Description
value Array | string A value to set.
settings anychart.enums.TextParsingMode | anychart.data.TextParsingSettings If CSV string is passed, you can pass CSV parser settings here as a hash map.

Returns:

anychart.data.Set - Self instance for method chaining.

getRowsCount

Returns the number of the rows in the current data set.

Returns:

number - The number of the rows in the set.

Try it:

insert

Inserts the row to the set at the specified position.

Params:

Name Type Default Description
row *
Row to insert.
index number
0
The index at which to insert the object. A negative index is counted from the end of an array.

Returns:

anychart.data.Set - Self instance for method chaining.

Try it:

listen

Adds an event listener to an implementing object.

Params:

Name Type Default Description
type string
The event type id.
listener function
Callback method. Function that looks like function(event){ // event.actualTarget - actual event target // event.currentTarget - current event target // event.iterator - event iterator // event.originalEvent - original event // event.point - event point // event.pointIndex - event point index }.
useCapture boolean
false
Whether to fire in capture phase. Learn more about capturing https://javascript.info/bubbling-and-capturing
listenerScope Object
Object in whose scope to call the listener.

Returns:

Object - Unique key for the listener.

Try it:

listenOnce

Adds an event listener to an implementing object.

Params:

Name Type Default Description
type string
The event type id.
listener function
Callback method.
useCapture boolean
false
Whether to fire in capture phase. Learn more about capturing https://javascript.info/bubbling-and-capturing
listenerScope Object
Object in whose scope to call the listener.

Returns:

Object - Unique key for the listener.

Try it:

mapAs

Defines data mapping.

Params:

Name Type Default Description
arrayMapping Object.<Array.<number>>
{
  'x': [0],
  'value': [1, 0],
  'size': [2],
  'open': [1],
  'high': [2],
  'low': [3, 1],
  'close': [4]
}
Column mapping for the rows which are arrays.
objectMapping Object.<Array.<string>>
{'value': ['value', 'y', 'close']}
Column mapping for the rows which are objects.
defaultProps Array.<string>
['value', 'close']
The names of the fields to map to if a row is a string, number or a function. Does not work in cases when a row is an object.
indexProps Array.<string>
['x']
The names of the fields to be mapped to the current index if other options failed.

Returns:

anychart.data.Mapping - An instance of the class for method chaining.
Custom data mapping.
// Simple mapping
 dataSet.mapAs({
   'value': [0],
   'x': [1],
   'fill': [2]
 });
  // Raw data                 Mapped as
  [
   [11, 1, 'red 0.5'],       {x: 1, value: 11, fill: 'red 0.5'}
   [21, 2, 'green 0.5'],     {x: 2, value: 21, fill: 'green 0.5'}
   [14, 3, 'blue 0.5'],      {x: 3, value: 14, fill: 'blue 0.5'}
   [11, 4, 'yellow 0.5']     {x: 4, value: 11, fill: 'yellow 0.5'}
  ]
// Combined mapping
 dataSet.mapAs({
   'value': [0],
   'x': [1],
   'fill': [2]
  },{
   'value': ['close', 'customY'],
   'fill': ['fill', 'color']
  }, null, ['close']
 );
 // Raw data                  Mapped as
  [
   [11, 1, 'red 0.5'],       {x: 1, value: 11, fill: 'red 0.5'}
   [21, 2, 'green 0.5'],     {x: 2, value: 21, fill: 'green 0.5'}
   {
     value: 14,
     x: 3,                   {x: 3, value: 14, fill: 'blue 0.5'}
     fill: 'blue 0.5'
   },{
     customY: '71',
     x: 3,                   {x: 3, value: 71, fill: 'blue 0.5', size 14}
     color: 'blue 0.5',
     size: 14
   },
   11,                       {close: 4, value: 11}
   function(){ return 99;}   {close: 5, value: 99}
  ]

remove

Removes the row by index.

Params:

Name Type Description
index number Index of the row to remove.

Returns:

anychart.data.Set - Self instance for method chaining.

Try it:

removeAllListeners

Removes all listeners from an object. You can also optionally remove listeners of some particular type.

Params:

Name Type Description
type string Type of event to remove, default is to remove all types.

Returns:

number - Number of listeners removed.

Try it:

row

Gets the full row of the set by the index.

Params:

Name Type Description
rowIndex number The index of the row to fetch.

Returns:

* - The current row.
Example.
// Data
 [
   [1, 2, 4, 7],
   {'high': 14, 'low': 3},
   7
 ]
 dataSet.row(0); // returns [1, 2, 4, 7]
 dataSet.row(1); // returns {'high': 14, 'low': 3}
 dataSet.row(2); // returns 7
 dataSet.row(3); // returns undefined

Try it:

Sets the row in the set by the index.

Params:

Name Type Description
rowIndex number The index of the row to fetch.
value * The value to set.

Returns:

* - The previous value of the row.
Example.
// Data
 [
   [1, 2, 4, 7],
   {'high': 14, 'low': 3},
   7
 ]
 dataSet.row(2, [2, 2, 2, 2]); // returns 7
 dataSet.row(3, {'low': 4, 'high': 11}); // returns undefined
// Data after the changes
 [
   [1, 2, 4, 7],
   {'high': 14, 'low': 3},
   [2, 2, 2, 2],
   {'low': 4, 'high': 11}
 ]

Try it:

unlisten

Removes a listener added using listen() or listenOnce() methods.

Params:

Name Type Default Description
type string
The event type id.
listener function
Callback method.
useCapture boolean
false
Whether to fire in capture phase. Learn more about capturing https://javascript.info/bubbling-and-capturing
listenerScope Object
Object in whose scope to call the listener.

Returns:

boolean - Whether any listener was removed.

Try it:

unlistenByKey

Removes an event listener which was added with listen() by the key returned by listen() or listenOnce().

Params:

Name Type Description
key Object The key returned by listen() or listenOnce().

Returns:

boolean - Whether any listener was removed.

Try it: