class anychart.data.Tree Improve this Doc
Extends: anychart.core.Base
Tree data model.
Methods Overview
Data | |
addChild() | Adds a new root element and returns it. |
addChildAt() | Inserts a new root element into a specified position by index and returns it. |
addData() | Adds a data. |
createIndexOn() | Creates an index on a specified field. |
dispatchEvents() | Tree CRUD events dispatching |
getChildAt() | Gets the child by index. |
getChildren() | Returns a copy of roots array. |
getTraverser() | Creates tree data traverser. |
indexOfChild() | Gets the index of child in a roots array. |
mapAs() | Returns a new mapping for the tree. |
numChildren() | Returns a length of roots array. |
removeChild() | Removes tree's root data item. |
removeChildAt() | Removes the child at specified position. |
removeChildren() | Removes children of the tree. |
removeIndexOn() | Removes index on a specified field. |
search() | Performs a data search. |
searchItems() | Performs search of an array of the items. |
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 |
Miscellaneous | |
filter() | Filters the tree data items using the filter-function. |
Methods Description
addChild
Adds a new root element and returns it.
Params:
Name | Type | Description |
---|---|---|
child | Object | Child object. |
Returns:
anychart.data.Tree.DataItem - An instance of the class for method chaining.Try it:
addChildAt
Inserts a new root element into a specified position by index and returns it.
Params:
Name | Type | Description |
---|---|---|
child | Object | anychart.data.Tree.DataItem | anychart.data.TreeView.DataItem | Child object. |
index | number | Position. |
Returns:
anychart.data.Tree.DataItem - An instance of the class for method chaining.Try it:
addData
Adds a data.
Params:
Name | Type | Description |
---|---|---|
data | Array.<Object> | string | Raw data. |
fillingMethod | anychart.enums.TreeFillingMethod | string | Filling method. |
csvSettingsOrDeps | Object | Array.<anychart.data.Tree.Dependency> | CSV settings object or dependencies data. |
Returns:
anychart.data.Tree - Self instance for method chaining.Try it:
createIndexOn
Creates an index on a specified field.
It can't be indexed by 'parent' or 'children' fields because these fields are not available by treeItem.get(field);.
Params:
Name | Type | Description |
---|---|---|
field | string | Field name. |
asString | boolean | If the value should be treated as string always. |
comparisonFn | function(value1:*, value2:*) | Indexing comparison function. Should take 2 arguments to compare and return a negative number, zero, or positive number depending on whether the first argument is less than, equal to or greater than the second. The binary insertion mechanism is used to index the data by a specified field (for further search acceleration using the binary search). There is a default sorting function for the number, string, and boolean values. Other types require custom function in order for indexing and search to work correctly. To search using the anychart.data.Tree#search and anychart.data.Tree#searchItems) methods on an indexed field with a custom comparison function, the comparison function specified for the indexing must be applied. The index on the 'id' field is created automatically when the data tree is created. |
Returns:
anychart.data.Tree - Self instance for method chaining. Sample for method createIndexOn.
var rawdata = [ {name: 1, children: [ {name: 2, children: [ {name: 3}, {name: 4, children: [ {name: 5}, {name: 6} ]} ]}, {name: 7, children: [ {name: 8} ]} ]} ]; var treeData = anychart.data.tree(rawdata, anychart.enums.TreeFillingMethod|string.TREE); treeData.createIndexOn("name"); // Creates index. treeData.search('name', 5);
dispatchEvents
Starts or stops tree CRUD events dispatching.
Params:
Name | Type | Default | Description |
---|---|---|---|
enabled | boolean | true | Enabled state to set. |
Returns:
anychart.data.Tree - Self instance for method chaining.Try it:
filter
Filters the tree data items using the filter-function.
Detailed description
The filter function accepts a data item. After the action with the data item, the function must return a value of "true"
or 'false', specifying whether to include this data item in the result of the filtering or not.
Note: the filter performs a full data passage.
Note: the filter performs a full data passage.
Params:
Name | Type | Description |
---|---|---|
filterFunction | function(item:anychart.data.Tree.DataItem|anychart.data.TreeView.DataItem) | Filter function. |
Returns:
Array.<(anychart.data.Tree.DataItem|anychart.data.TreeView.DataItem)> - An array of data items.Try it:
getChildAt
Gets the child by index.
Params:
Name | Type | Description |
---|---|---|
index | number | Index of child to find. |
Returns:
anychart.data.Tree.DataItem | undefined - Child into a specified position.Try it:
getChildren
Returns a copy of roots array.
Returns:
Array.<anychart.data.Tree.DataItem> - Copy of roots array.Try it:
getTraverser
Creates tree data traverser.
Returns:
anychart.data.Traverser - An instance of the class for method chaining. Sample for method getTraverser.
var rawdata = [ {name: 1, children: [ {name: 2, children: [ {name: 3}, {name: 4, children: [ {name: 5}, {name: 6} ]} ]}, {name: 7, children: [ {name: 8} ]} ]} ]; var treeData = anychart.data.tree(rawdata, anychart.enums.TreeFillingMethod|string.TREE); treeData.getTraverser(); // creates traverser
indexOfChild
Gets the index of child in a roots array.
Params:
Name | Type | Description |
---|---|---|
child | anychart.data.Tree.DataItem | anychart.data.TreeView.DataItem | Child for getting of index. |
Returns:
number - Index of child.Try it:
listen
Adds an event listener to an implementing object.
Detailed description
The listener can be added to an object once, and if it is added one more time, its key will be returned.
Note Notice that if the existing listener is one-off (added using listenOnce), it will cease to be such after calling the listen() method.
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(e:Object) | 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.
Detailed description
After the event is called, its handler will be deleted.
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).
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(e:Object) | 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
Returns a new mapping for the tree.
Params:
Name | Type | Description |
---|---|---|
mapping | Object | Mapping for the tree. |
Returns:
anychart.data.TreeView - An instance of the class for method chaining.Try it:
numChildren
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:
removeChild
Removes tree's root data item.
Params:
Name | Type | Description |
---|---|---|
child | anychart.data.Tree.DataItem | Child for removal. |
Returns:
anychart.data.Tree.DataItem - An instance of the class for method chaining.Try it:
removeChildAt
Removes the child at specified position.
Params:
Name | Type | Description |
---|---|---|
index | number | Index of data item for removal. |
Returns:
anychart.data.Tree.DataItem - Removed item or null if item is not found.Try it:
removeChildren
removeIndexOn
Removes index on a specified field.
Params:
Name | Type | Description |
---|---|---|
field | string | Field name. |
Returns:
anychart.data.Tree - Self instance for method chaining. Sample for method removeIndexOn.
var rawdata = [ {name: 1, children: [ {name: 2, children: [ {name: 3}, {name: 4, children: [ {name: 5}, {name: 6} ]} ]}, {name: 7, children: [ {name: 8} ]} ]} ]; var treeData = anychart.data.tree(rawdata, anychart.enums.TreeFillingMethod|string.TREE); treeData.createIndexOn('name'); treeData.search('name', 5); treeData.removeIndexOn('name'); // removes index
search
Performs a data search.
Learn more about searching.
Detailed description
Returns null if nothing is found, tree data item if there's a single result and an array of tree data items if there are multiple matches.
The search method is used to find the exact value.
The difference between the search method and the anychart.data.Tree#filter method:
- search is much faster when there is an index on a field
- this method is based on the binary search
Params:
Name | Type | Description |
---|---|---|
field | string | Search field name. |
value | * | Value to be found. |
comparisonFn | function(value1:*, value2:*) | An optional comparison function by which the array is ordered. Should take 2 arguments to compare and return a negative number, zero, or positive number depending on whether the first argument is less than, equal to or greater than the second. |
Returns:
anychart.data.Tree.DataItem | Array.<anychart.data.Tree.DataItem> | null - The found tree data item or null or array of found tree data items.Try it:
searchItems
Performs a data search.
Learn more about searching.
Detailed description
Same as anychart.data.Tree#search but the result is always an array.
The searchItems method is used to find the exact value.
The difference between the searchItems method and the anychart.data.Tree#filter method:
- search is much faster when there is an index on a field
- this method is based on the binary search
Params:
Name | Type | Description |
---|---|---|
field | string | Search field name. |
value | * | Value to be found. |
comparisonFn | function(value1:*, value2:*) | An optional comparison function by which the array is ordered. Should take 2 arguments to compare and return a negative number, zero, or positive number depending on whether the first argument is less than, equal to or greater than the second. |
Returns:
Array.<anychart.data.Tree.DataItem> - An array of found tree data items. If nothing is found, the array is returned empty.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(e:Object):boolean|undefined | 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: