AnyChart
API Reference
Still have questions?
Contact support
Top
You are looking at an outdated 7.13.0 version of this document. Switch to the 8.4.0 version to see the up to date information.

class anychart.data.Tree Improve this Doc

Extends: anychart.core.Base

Tree data model.

Methods Overview

Data
addChild()Adds a new root element and return it.
addChildAt()Inserts a new root element into a specified position by index and return 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 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 child at specified position.
removeChildren()Removes children of tree.
removeIndexOn()Removes index on a specified field.
search()Performs a data search.
searchItems()Performs an items search.
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

addChild

Adds a new root element and return it.

Params:

NameTypeDescription
childObjectChild object.

Returns:

anychart.data.Tree.DataItem - An instance of the class for method chaining.

addChildAt

Inserts a new root element into a specified position by index and return it.

Params:

NameTypeDescription
childObject | anychart.data.Tree.DataItem | anychart.data.TreeView.DataItemChild object.
indexnumberPosition.

Returns:

anychart.data.Tree.DataItem - An instance of the class for method chaining.

addData

Adds a data.

Params:

NameTypeDescription
dataArray.<Object> | stringRaw data.
fillingMethodanychart.enums.TreeFillingMethod | stringFilling method.
csvSettingsOrDepsObject | Array.<anychart.data.Tree.Dependency>CSV settings object or dependencies data.

Returns:

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

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:

NameTypeDescription
fieldstringField name.
asStringbooleanIf the value should be treated as string always.

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.TREE);
treeData.createIndexOn("name"); // Creates index.
treeData.search("name", 5);

dispatchEvents

Gets tree CRUD events dispatching.

Returns:

boolean - Current value.
Starts or stops tree CRUD events dispatching.

Params:

NameTypeDefaultDescription
valueboolean
true
Value to set.

Returns:

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

getChildAt

Gets the child by index.

Params:

NameTypeDescription
indexnumberIndex of child to find.

Returns:

anychart.data.Tree.DataItem | undefined - Child into a specified position.

getChildren

Returns a copy of roots array.

Returns:

Array.<anychart.data.Tree.DataItem> - Copy of roots array.

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.TREE);
treeData.getTraverser(); // creates traverser

indexOfChild

Gets index of child in a roots array.

Params:

NameTypeDescription
childanychart.data.Tree.DataItem | anychart.data.TreeView.DataItemChild for getting of index.

Returns:

number - Index of child.

listen

Adds an event listener to an implementing object.

Params:

NameTypeDescription
typestringThe event type id.
listenerfunctionCallback 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 }.
useCapturebooleanWhether to fire in capture phase (defaults to false).
listenerScopeObjectObject in whose scope to call the listener.

Returns:

Object - Unique key for the listener.

listenOnce

Adds an event listener to an implementing object.

Params:

NameTypeDescription
typestringThe event type id.
listenerfunctionCallback method.
useCapturebooleanWhether to fire in capture phase (defaults to false).
listenerScopeObjectObject in whose scope to call the listener.

Returns:

Object - Unique key for the listener.

mapAs

Returns a new mapping for the tree.

Params:

NameTypeDescription
mappingObjectMapping for the tree.

Returns:

anychart.data.TreeView - An instance of the class for method chaining.

numChildren

Returns a length of roots array.

Returns:

number - Number of roots.

removeAllListeners

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

Params:

NameTypeDescription
typestringType of event to remove, default is to remove all types.

Returns:

number - Number of listeners removed.

removeChild

Removes tree's root data item.

Params:

NameTypeDescription
childanychart.data.Tree.DataItemChild for removal.

Returns:

anychart.data.Tree.DataItem - An instance of the class for method chaining.

removeChildAt

Removes child at specified position.

Params:

NameTypeDescription
indexnumberIndex of data item for removal.

Returns:

anychart.data.Tree.DataItem - Removed item or null if item is not found.

removeChildren

Removes children of tree.

Returns:

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

removeIndexOn

Removes index on a specified field.

Params:

NameTypeDescription
fieldstringField 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.TREE);
treeData.createIndexOn('name');
treeData.search('name', 5);
treeData.removeIndexOn('name'); // removes index

searchItems

Performs a data search. Actually does the same as (anychart.data.Tree#search) but result is always an array.

Params:

NameTypeDescription
soughtFieldstringField for search. Literally means the name of field of data item.
valuestring | number | booleanSought value
comparisonFnfunctionComparison function.

Returns:

Array.<anychart.data.Tree.DataItem> - Array of found tree data items.

unlisten

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

Params:

NameTypeDescription
typestringThe event type id.
listenerfunctionCallback method.
useCapturebooleanWhether to fire in capture phase (defaults to false).
listenerScopeObjectObject in whose scope to call the listener.

Returns:

boolean - Whether any listener was removed.

unlistenByKey

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

Params:

NameTypeDescription
keyObjectThe key returned by listen() or listenOnce().

Returns:

boolean - Whether any listener was removed.