ReGraph comes with a number of default behaviors built in to respond to common user gestures. For example, clicking on a node or a link selects that item (updating selection state and drawing a halo around the selected item).
By passing functions through to the event props, you can both hook in to chart events (to know which item was clicked) and override default behavior (to stop the selection changing).
Event handlers will be called by ReGraph and passed a single object containing all the details of the event. Calling the preventDefault() function in these handlers will override any default behavior.
For example, to respond to the onClick event:
const handleClick = ({ id, x, y, button, subItem }) => {
// id: the node, link or navigation control that was clicked
// x, y: the position of the item relative to the view
// button: the logical mouse button that was clicked
// subItem: the sub-item that was interacted with
};
<Chart items={items} onClick={handleClick} />;To prevent an item from being selected on onClick:
const preventSelection = ({ preventDefault }) => {
preventDefault();
};
<Chart items={items} onClick={preventSelection} />;p#events-subitems.
Some items on the chart can include sub-items. When you click on a sub-item, like a label,
details about it will be passed to the event handler.
const handleClick = ({ id, x, y, button, subItem }) => {
if (subItem && subItem.type === "label") {
// Label was clicked
}
};
<Chart items={items} onClick={handleClick} />;Handle errors directly in the event listener:
const handleChange = (change) => {
try {
functionWhichMightThrow(change);
} catch (e) {
// handle the error
console.error(e);
}
};The only exception to this are errors produced by onCombineNodes and onCombineLinks events. Their errors are logged to the console.
The firing order of chart pointer events is onPointerDown, onPointerMove, onPointerUp, onClick (updates selection), onChange (contains new selection).
See the API Reference for a complete list of events on the chart and time bar component. A detailed description of the sub-item type is available in the sub-item section of the API reference.
See also the interactive Interaction / Events and Time Bar / Events examples.