CT.4 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.
Note that charts created by script do not provide end-user interactivity features. Therefore, for script-based charts you should deselect 'Enable Ad Hoc Editing' in the 'Chart Properties' dialog box (Advanced tab).
Walkthrough
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 or Viewsheet.
Note: Chart API script (which operates on the Chart's 'Egraph' property) should be placed in element-level script.
2. Open the Script Editor as follows:
a. In a report, right-click on the chart and select 'Script' from the context menu.
b. In a Viewsheet, right-click the chart and select 'Properties' from the context menu. Then select the Script tab.
Tip: To reference chart data, use the DataSet object.
3. Define the data: Define the dataset for the chart using the Chart's Data property.
data = runQuery("sales by state");
For reports, you will generally obtain the data from a query, Worksheet, or data model by using runQuery(). In Viewsheets, you can additionally bind data from any available Data Block. You can also define your own dataset in script by assigning a JavaScript array to the data or dataset property. See Binding Data to a Chart in Script for more details.
EGraph is the global chart object, which includes all axes, legends, visual elements, etc.
4. Create the Chart object. Create a new graph using the EGraph constructor. Assign it to the Chart's graph property.
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 GraphElements, such as PointElement and LineElement, generate other kinds of chart types, such as scatter plots and line plots, respectively.
6. Add the created 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.
graph.addElement(elem);
7. Close the Script Editor and preview the report or Viewsheet.
These are the basic operations required to create a new chart (from the ground-up) using the Chart API. The complete script is shown below, along with the resulting chart.
data = runQuery("sales by state");
graph = new EGraph();
var elem = new IntervalElement("State", "Sales");
graph.addElement(elem);

See Also
Modifying a Chart Element using API Functions, to modify an existing chart.
Modifying a Chart Data Binding, to modify data binding information.
Appendix CT:Chart Script Tutorial, for examples of modifying chart elements.
Appendix CR:Chart Script Reference, for information on objects and attributes.
API Documentation for the inetsoft.graph.* packages.
| << CT.3 Modifying a Chart Element using API Functions | © 1996-2013 InetSoft Technology Corporation (v11.5) | CT.5 Binding Data to a Chart in Script >> |