Skip to content

API reference

cy.data — Core > Data and scratch. Cytoscape.js signatures, parameters and return contract.

cy.data — Core › Data and scratch

Store data on a graph: read application fields, write one field, or merge an object.

Preview · Core › Data and scratch

Read in the complete API reference · All API members

#cy.data()

// Get all data
cy.data(): Record<string, unknown>

Read all graph-level data. Graph data is separate from element data. Binary serialization includes it; constructor loading restores it, while cy.add(buffer) leaves the destination graph's data unchanged.

Returns Record<string, unknown>

the graph's data object

Example

cy.data('title', 'Example graph');
console.log(cy.data().title); // 'Example graph'

Example graph and handle setup

#cy.data(key)

// Get data by key
cy.data(key: string): unknown

Read one graph-level data value.

Parameters
NameDescription
key: string

the application field to read or write

Returns unknown

the value, or undefined when the key is unset

Example

cy.data('title', 'Example graph');
console.log(cy.data('title')); // 'Example graph'

Example graph and handle setup

#cy.data(key, value)

// Set data by key
cy.data(key: string, value: unknown): this

Write one graph-level data value. Writes emit a data event.

Parameters
NameDescription
key: string

the application field to read or write

value: unknown

the value to store

Returns this

this core, for chaining

Example

const result = cy.data('title', 'Example graph');
console.log(result === cy); // true

Example graph and handle setup

#cy.data(patch)

// Set several data fields
cy.data(patch: Record<string, unknown>): this

Merge several graph-level data fields. Writes emit a data event.

Parameters
NameDescription
patch: Record<string, unknown>

the fields to merge; other fields stay unchanged

Returns this

this core, for chaining

Example

cy.data({ title: 'Example graph' });
console.log(cy.data('title')); // 'Example graph'

Example graph and handle setup

Also spelled cy.attr.