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.
// 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 ]
// 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; }
]);// '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// '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}
]// 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
Params:
| Name | Type | Description |
|---|---|---|
| var_args | * | Rows to append. |
Returns:
anychart.data.Set - Self instance for method chaining.Try it:
data
Returns:
Array - Data array of the Set.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:
number - The number of the rows in the set.Try it:
insert
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
Note Notice that if the existing listener is one-off (added using listenOnce), it will cease to be such after calling the listen() method.
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
If the event handler being added already exists, listenOnce will do nothing.
Note In particular, if the handler is already registered using listen(), listenOnce() will not make it one-off. Similarly, if a one-off listener already exists, listenOnce will not change it (it wil remain one-off).
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
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.// 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
Params:
| Name | Type | Description |
|---|---|---|
| index | number | Index of the row to remove. |
Returns:
anychart.data.Set - Self instance for method chaining.Try it:
removeAllListeners
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
Params:
| Name | Type | Description |
|---|---|---|
| rowIndex | number | The index of the row to fetch. |
Returns:
* - The current row.// 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 undefinedTry it:
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.// 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
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
Params:
| Name | Type | Description |
|---|---|---|
| key | Object | The key returned by listen() or listenOnce(). |
Returns:
boolean - Whether any listener was removed.Try it:
