How to create charts with dojox/charting

DojoX is a powerful JavaScript library that offers a vast number of tools to develop web applications that are dynamic and interactive. One of its notable features is the DojoX charting module, which allows developers to generate visually appealing charts and graphs easily. In this answer, we'll explore the basics of charts in DojoX and provide a step-by-step guide on creating charts. Ultimately, we’llwe'll a line chart as an example to strengthen our charting concept in DojoX.

Setup DojoX charting

Before diving into chart creation, we must ensure we have the DojoX Charting library in our web application. We must include the script tags in the HTML document to load DojoX and its dependencies.

<script src='/node_modules/dojo/dojo.js'></script>
<script>
require([
"dojox/charting/Chart",
"dojox/charting/plot2d/Lines",
"dojox/charting/themes/Claro",
"dojox/charting/axis2d/Default",
"dojo/domReady!"]);
</script>

Creating a chart container

To display a chart, we need to define a container element in our HTML document. This container will serve as the placeholder for the chart. We can use a simple <div> element and give it an ID to refer it later.

<div id="myChart"></div>

Choosing the chart type

DojoX Charting offers a wide range of chart types, including line, bar, pie, and more. We must select the appropriate chart type that effectively represents the data and suits the visualization needs.

var chart = new Chart("myChart");
chart.addPlot("default", { type: Lines });

Preparing data for the chart

Next, we need to gather the data we want to visualize. Whether it’s statistical figures, sales data, or any other dataset, ensure it is well-structured and organized. DojoX Charting supports various data formats, such as arrays, JSON, or CSV.

var data=[0, 1, 4, 9, 16, 25, 36];

Configuring chart parameters

We will then modify the chart's parameters to personalize its appearance and behavior. DojoX provides comprehensive options to control various aspects, such as colors, labels, axis scales, legends, and tooltips. We can explore various configurations to attain the preferred aesthetics and functionality.

chart.addAxis("x", {
fixLower: "major",
fixUpper: "major",
title:"This is x-axis",
titleOrientation: "away"
});
chart.addAxis("y", {
vertical: true,
fixLower: "major",
fixUpper: "major",
title:"This is y-axis"
});
chart.addSeries("FirstSeries", data);

Rendering the chart

Using JavaScript, we'll create an instance of the desired chart type and provide the necessary configuration parameters. We'll then attach the chart to the previously defined container element using its ID. Finally, call the render() method to generate and display the chart within the container.

chart.render();

Example

Let's draw a line chart as given below to strengthen our DojoX charting concepts.

{
  "name": "dojoxproject",
  "version": "1.0.0",
  "description": "",
  "main": "index.html",
  "scripts": {
    "run":"node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dojo": "^1.17.3",
    "dojox": "^1.17.3"
  }
}
Draw a line chart using DojoX charting library

Code explanation

  • Line 10–16: We import all the necessary modules to draw a chart.

  • Line 18: We create an instance of the Chart class. The lineChart refers to the ID of the HTML element where we render the chart.

  • Line 19: This statement sets the theme for the chart. We can set various available themes or can create our own. In this line, we are using the available theme.

  • Line 20: It adds a plot to the chart. The type parameter specifies the plot type, which, in this case, is a line plot using Lines.

  • Line 21: We define the data to be plotted.

  • Line 20–32: We add the x-axis and y-axis to the chart and set their titles. The fixLower and fixUpper options set the scale of the axis to align with major ticks.

  • Line 35: This line adds a series we want to draw. The first argument FirstSeries identifies the series uniquely. We can add multiple series to draw in the chart.

  • Line 36: This step renders the chart, displaying it in the HTML element having ID lineChart , specified earlier in line 18.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved