A guide to the key arguments for creating a new visualization.
The CanvasXpress object is instantiated by calling the new
constructor function. This function takes
either four arguments: targetId
, data
, configuration
, and events
, or a single JSON object that
combines these four arguments. The sections below provide a detailed look at each.
var cX = new CanvasXpress(targetId, data, config, events);
Or, using a single JSON object:
var cX = new CanvasXpress({
"renderTo": targetId,
"data": data,
"config": config,
"events": events
});
A JSON object or URL pointing to a file (png, svg, json, text delimited, gpml, xml) containing the data to plot.
Learn more →A JSON object with user-defined mouse events for interactivity and custom behaviors.
Learn more →
The targetId
is the DOM ID of the <canvas>
element inside the <body>
element of the web
page. The width
and height
attributes are used to set the initial size of the CanvasXpress
visualization.
To make visualizations responsive, use the responsive
and aspectRatio
attributes. The responsive
attribute automatically resizes the visualization to its parent container, while aspectRatio
maintains
its proportions.
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<canvas id='bar1' width='290' height='540' aspectRatio='1:1' responsive='true'></canvas>
</div>
</div>
It's important that the <canvas>
element is not programmatically changed to avoid conflicts with
the CanvasXpress library.
The data
parameter is a JSON object that contains the data to be visualized. Its structure depends on
the visualization type. Data can also be a URL pointing to a file in various formats.
var data = {
"y": {
"vars": ["Gene1", "Gene2", "Gene3"],
"smps": ["Sample1", "Sample2", "Sample3"],
"data": [[10, 20, 30], [15, 25, 35], [5, 15, 25]]
},
"x": {
"Treatment": ["Control", "Control", "Treatment"]
},
"z": {
"Gene1": { "Pathway": "Pathway1" },
"Gene2": { "Pathway": "Pathway2" },
"Gene3": { "Pathway": "Pathway1" }
}
}
The config
parameter is a JSON object that customizes the appearance and behavior of the visualization.
graphType
- The type of visualization (e.g., 'Bar', 'Scatter2D', 'Heatmap')colorScheme
- Color palette for data representationtitle
- Title of the visualizationsubtitle
- Subtitle of the visualizationxAxisTitle
- Label for the X-axisyAxisTitle
- Label for the Y-axisvar config = {
"graphType": "Scatter2D",
"colorScheme": "Rainbow",
"title": "Gene Expression Analysis"
}
For a complete list of configuration options, refer to the API documentation.
The events
parameter is a JSON object that defines custom event handlers for user interactions with the
visualization, such as clicks and mouseovers.
click
- Triggered when a user clicks on an elementdblclick
- Triggered on double-clickmouseover
- Triggered when the mouse moves over an elementmouseout
- Triggered when the mouse leaves an elementvar events = {
"click": function(o, e, t) {
console.log("Clicked on:", o);
},
"mouseover": function(o, e, t) {
console.log("Mouse over:", o);
}
}
CanvasXpress maintains a registry of all instances created on a page. You can retrieve an instance by its ID. It's also important to properly destroy an instance when it's no longer needed.
// Get an instance by ID
var cX = CanvasXpress.$('canvasId');
// Destroy an instance
cX.destroy();
When initializing CanvasXpress, ensure the <canvas>
element is fully loaded. The simplest way is
to place the script after the canvas element.
// Using document.addEventListener
document.addEventListener('DOMContentLoaded', function() {
var cX = new CanvasXpress(...);
});
The essential arguments are targetId
, data
, config
, and events
. These define where the visualization will be rendered, what data it will display, how it will be styled, and how it will respond to user interactions.
To make your visualization responsive, ensure your <canvas>
element includes the responsive='true'
and aspectRatio
attributes. This allows the visualization to automatically resize while maintaining its proportions within its parent container.
CanvasXpress supports various data formats, including JSON, CSV/TSV, XML (GPML, KGML, XGMML), and PNG/SVG image files with embedded data.