Creating a Chart Using API Functions

Previous sections explained how to modify the data binding and element properties of an existing chart. This section explains how to create a new chart from the ground-up using Chart API commands.

In this example, you will create a new chart, define the chart data, and display the data on the chart. Follow the steps below.

  1. Add a new Chart to the Report. (Do not open the 'Data Binding' dialog box for the chart.) Note: Chart API script (which operates on the Chart's 'Egraph' property) should be placed in element scope. See Adding Element-Level Script for more information.
  2. Click the Chart to select it. Right-click and select 'Script' from the context menu. This opens the Script Editor, where you can enter the chart script.
  3. (Optional) Enter the following lines to import the packages required by the script. You only need to import packages that the script actually uses.
importPackage(inetsoft.graph)
importPackage(inetsoft.graph.aesthetic)
importPackage(inetsoft.graph.coord)
importPackage(inetsoft.graph.data)
importPackage(inetsoft.graph.element)
importPackage(inetsoft.graph.guide.form)
importPackage(inetsoft.graph.scale)
importPackage(inetsoft.graph.schema)

When you import a package, you can refer to its classes by their sim­ple class names, e.g., HeatColorFrame(). If you do not import the package, then you need to use the fully-qualified class names, for example, inetsoft.graph.aesthetic.HeatColorFrame().

Tip: To reference chart data, use the DataSet object.

  1. Define the data: Define the dataset for the chart using the Chart's Data property.
  2. data = runQuery("sales by state"); 

    In most cases, you will obtain the data from a query or data model by using runQuery(). You can also define your own dataset by passing a JavaScript array to the DefaultDataSet() method. EGraph is the global chart object, including all axes, legends, visual elements, etc.

  3. Create the Chart object. Create a new graph using the EGraph constructor. Assign it to the Chart's graph property.
  4. graph = new EGraph(); 
  5. Create the chart data elements. Pass the field names (column headers) to a GraphElement constructor. This creates the representational elements for the chart. var elem = new IntervalElement("State", "Sales"); The IntervalElement is a particular type of GraphElement that creates “bars” or “intervals” as the representational elements. Other Graph­Elements, such as PointElement and LineElement, generate other kinds of chart types, such as scatter plots and line plots, respectively.
  6. Add the elements to the Chart object. Pass the GraphElement object to the Chart's EGraph.addElement(elem) method. This adds the “bar” elements to the existing Chart object.
  7. graph.addElement(elem); 
  8. Close the Script Editor and preview the report.

These are the basic operations required to create a chart from the ground-up using the Chart API. The complete script is shown below, along with the resulting chart.

importPackage(inetsoft.graph)
importPackage(inetsoft.graph.aesthetic)
importPackage(inetsoft.graph.coord)
importPackage(inetsoft.graph.data)
importPackage(inetsoft.graph.element)
importPackage(inetsoft.graph.guide.form)
importPackage(inetsoft.graph.scale)
importPackage(inetsoft.graph.schema)

data = runQuery("sales by state");
graph = new EGraph();
var elem = new IntervalElement("State", "Sales");
graph.addElement(elem);

More Articles About Charts

Metrics for a Maker of Virtual Reality (VR) Products - As VR products continue to gain traction in the market, it becomes increasingly crucial for manufacturers to leverage data-driven insights to optimize performance, enhance user experience, and drive business growth. This necessitates the identification and tracking of key performance indicators (KPIs) and metrics tailored to the unique characteristics of the VR industry. Understanding KPIs and Metrics in the VR Industry: Before delving into specific KPIs and metrics, it's essential to grasp their significance within the context of VR product manufacturing. KPIs and metrics serve as quantifiable measures that gauge various aspects of business performance and provide actionable insights for decision-making. In the VR industry, these indicators offer invaluable guidance for assessing product effectiveness, market penetration, customer engagement, and overall business success...

Reviews About Business Dashboard Software - Since 1996 InetSoft has been an innovating developer of business dashboard software. To date, our software has been used by over 5,000 organizations across the world and has been embedded into commercial solutions of other ISVs and SaaS providers. Read customer and partner reviews on G2 Crowd and Software Advice: What I liked the most was the drag-and-drop feature for putting together a data query…with other solutions you have no choice but to sit and write code…but with Style Intelligence, it doesn't take two to three hours to design reports...

Top 10 Worthy Dashboard Solution - Priced to suit your organizations budget, InetSoft's dashboard solution combines competitive pricing with powerful tools. With our interactive dashboards, users will have a multi-dimensional solution that thoroughly prepares them for reporting and analyzing data sets. In addition to the multi-dimensional aspect of our dashboards, the user-friendly drag-and-drop design can be used with a wide range of sophisticated chart types...

Previous: Modifying a Chart Element using API Functions
We will help you get started Contact us