Bind Data to Chart in Script
There are several ways to bind data to a Chart using script. The following sections present the various approaches.
| It is not necessary to use script to do this. In fact, the easiest way to bind data to a Chart is to use the Chart Editor (see Create a Chart). After you have bound data to the Chart using the Chart Editor, you can proceed to modify the chart using script operations as described in Modify Chart Element. |
Bind Data in Component-Level Script
To bind data to a chart from an arbitrary query or Data Worksheet in element-level script, use the runQuery(name [,parameters]) function and assign the result to the Chart’s data or dataset property.
Chart1.data = runQuery("ws:global:Examples/AllSales");
graph = new EGraph();
var elem = new IntervalElement("State", "Sales");
graph.addElement(elem);
Use runQuery() only in component-level script.
|
You can see an example of this approach in Create a Chart with API. If you want to access a data block in the Data Worksheet on which the current Dashboard is based, you can use the simpler data syntax shown below:
Chart1.data = Sales; // “Sales” is the data block name
graph = new EGraph();
var elem = new IntervalElement("State", "Sales");
graph.addElement(elem);
If the data block name contains spaces, use the syntax viewsheet['data block name']. See Access Datasource Data for more information about accessing Data Worksheet data in Dashboard script.
Bind Data from Array
You can define a Chart dataset entirely in component-level script by setting a JavaScript array as the data or dataset property.
Chart1.data = [["State","Quantity"],["NJ",200],["NY",300]];
graph = new EGraph();
var elem = new IntervalElement("State", "Quantity");
graph.addElement(elem);
You can see an example of this approach in Change Chart Scaling.
Bind Data in Dashboard-Level Script
You can bind the Chart to one of the data blocks available to the Dashboard by using the Chart’s query property (for example, in onRefresh script).
For example, assume that ‘Orders And Returns’ is the name of a data block. The following onRefresh script binds this data block to the Chart.
Chart1.query = "Orders And Returns";
Chart1.bindingInfo.xFields = [["Company",Chart.STRING]];
Chart1.bindingInfo.yFields = [["Total",Chart.NUMBER]];
You can see an example of this approach in Modify Chart Data Binding.