Data Join-Action
Perform join between Data and Graphical elements
Perform join between Data and Graphical elements
Data Join functionality helps in rendering the elements dynamically with respect to data. It builds a relationship between the data and the graphical elements. I2Djs provides a simple and unique way to define the join.
To begin let's create a data array with 3 items uniquely identified by the id field.
Let's define the join actions for the above data array. The join method takes data, element selector, and action object as an argument. Actions object comprises of joinOn (join key) and actions - enter, update, and exit.
Enter method defines an action for the new data. It receives a data object, with elementSelector property holding the corresponding new data array. Since we are performing join with a circle, newData.circle will have the new data array with respect to circle join.
createEls API creates a circle for each data object. In the below example every new circle will be set with attr attributes and style properties. All new circles will be filled with red color.
The Update method defines an action on the nodes of the retained dataset. Update action receives an object with elementSelector property holding respective nodes collection. In the below example, we set the circle color to '#00ff00' as part of the update action.
Now let's append a new data object {id: 4, label: 'four} with the existing data array.
On join execution, the update action will be triggered with 3 previously rendered nodes, which will be set to '#00ff00'. And enter action will receive the newly added object, which appears in red in color.
The exit method defines action on the nodes of the obsolete dataset. Just like the update action, the exit action also receives an object with elementSelector property holding respective nodes collection. In the below example, we remove the circle nodes as part of the exit action.
now let's remove the 2nd object from the existing data array.
On join execution, exit action will be triggered with the nodes associated with the obsolete data. The same will be removed and the remaining nodes will be passed to update action.
Join with multi-elements can be performed by passing multi elementSelector string. In the below example, the data join is performed simultaneously between data and circle, text elements.