CanvasXpress Constructor Arguments

A guide to the key arguments for creating a new visualization.

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.

Constructor Syntax

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
});

Constructor Arguments

targetId

The DOM ID of the <canvas> element where the graph will be rendered.

Learn more →
data

A JSON object or URL pointing to a file (png, svg, json, text delimited, gpml, xml) containing the data to plot.

Learn more →
config

A JSON object to customize the graph's appearance and behavior.

Learn more →
events

A JSON object with user-defined mouse events for interactivity and custom behaviors.

Learn more →

DOM - targetId and the <canvas> element

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.

Responsive Visualizations

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.

Data Structure

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.

Data Components

  • Sample information: Metadata for samples.
  • Variable information: Metadata for variables.
  • Data values: The actual data to be visualized.
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" }
  }
}

Supported File Formats

  • JSON files
  • CSV/TSV files
  • XML files (GPML, KGML, XGMML)
  • PNG/SVG image files (with embedded data)

Configuration Options

The config parameter is a JSON object that customizes the appearance and behavior of the visualization.

Common Configuration Properties

  • graphType - The type of visualization (e.g., 'Bar', 'Scatter2D', 'Heatmap')
  • colorScheme - Color palette for data representation
  • title - Title of the visualization
  • subtitle - Subtitle of the visualization
  • xAxisTitle - Label for the X-axis
  • yAxisTitle - Label for the Y-axis
var config = {
  "graphType": "Scatter2D",
  "colorScheme": "Rainbow",
  "title": "Gene Expression Analysis"
}

For a complete list of configuration options, refer to the API documentation.

Event Handling

The events parameter is a JSON object that defines custom event handlers for user interactions with the visualization, such as clicks and mouseovers.

Common Events

  • click - Triggered when a user clicks on an element
  • dblclick - Triggered on double-click
  • mouseover - Triggered when the mouse moves over an element
  • mouseout - Triggered when the mouse leaves an element
var events = {
  "click": function(o, e, t) {
    console.log("Clicked on:", o);
  },
  "mouseover": function(o, e, t) {
    console.log("Mouse over:", o);
  }
}

Instance Management

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();

Initialization Techniques

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(...);
});

Frequently Asked Questions

What are the essential arguments for the CanvasXpress constructor?

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.

How can I make my CanvasXpress visualization responsive?

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.

What data formats does CanvasXpress support?

CanvasXpress supports various data formats, including JSON, CSV/TSV, XML (GPML, KGML, XGMML), and PNG/SVG image files with embedded data.