Creating a Chart Using an iScript
You can also create a chart using an iScript. The following example creates a chart, populates it, and then displays it using HTML written to the Response object. The complete code sample is shown.
Note:
You can create only Chart, LedGauge, RatingGaugeChart, StatusMeterGauge, and SparkChart class charts using an iScript.
Function IScript_GetChart()
Local Chart &cChart;
Local Rowset &rsRowset;
&cChart = CreateObject("Chart");
&rsRowset = CreateRowset(Record.QE_CHART_RECORD);
&rsRowset.Fill("where QE_CHART_REGION= :1", "MIDWEST");
&cChart.SetData(&rsRowset);
&cChart.Width = 400;
&cChart.Height = 300;
&cChart.SetDataYAxis(QE_CHART_RECORD.QE_CHART_SALES);
&cChart.SetDataXAxis(QE_CHART_RECORD.QE_CHART_PRODUCT);
&cChart.SetDataSeries(QE_CHART_RECORD.QE_CHART_REGION);
&cChart.HasLegend = True;
&cChart.LegendPosition = %ChartLegend_Right;
%Response.Write("<HTML>" | &oChart.Html | "</HTML>");
End-Function;See PeopleCode Language Reference: CreateObject function.
Complete these steps to create a chart using an iScript:
-
Create the chart object.
For this example, no chart control is available on a page to be referenced. To create the chart object, use the CreateObject function. The string passed in to the function must be the name of the class you are instantiating. For instance, to instantiate a chart from the Chart class, pass the string “Chart”.
&cChart = CreateObject("Chart"); -
Create a rowset for the chart data and set the chart data.
The CreateRowset function creates a standalone rowset data structure. Use the Fill method to populate the empty rowset with data. The SetData method associates the rowset data with the chart.
&rsRowset = CreateRowset(Record.QE_CHART_RECORD); &rsRowset.Fill("where QE_CHART_REGION= :1", "MIDWEST"); &cChart.SetData(&rsRowset); -
Set the height and width of the chart.
Because the chart is not associated with a chart control on a page, you have to specify the size of the chart image to be generated using the Height and Width properties. The unit of measurement for both of these properties is pixels.
&cChart.Width = 400; &cChart.Height = 300; -
Set the data axes, series, and legend for the chart.
As with all charts, you must set the data axes. If necessary for your data, also set the data series and the legend.
&cChart.SetDataYAxis(QE_CHART_RECORD.QE_CHART_SALES); &cChart.SetDataXAxis(QE_CHART_RECORD.QE_CHART_PRODUCT); &cChart.SetDataSeries(QE_CHART_RECORD.QE_CHART_REGION); &cChart.HasLegend = True; &cChart.LegendPosition = %ChartLegend_Right; -
Create the response html.
Use the Write Response class method to generate the chart, retrieving the html for the chart using the Chart class property “Html”.
%Response.Write("<HTML>" | &oChart.Html | "</HTML>");
Note:
If one or more labels does not render in the chart, increase the width of the chart, use shorter label text, or reduce the data set being charted.