As a rule, connection parameters to data source are saved in report template. To work with file data source, such as XML, JSON, Excel, CSV you don't need to make any additional actions. So as all algorithms are in the report generator script. If necessary, you can use other methods to connect data using JavaScript functions of the reporting tool. Use the onBeforeRender event of the report object. Data can be loaded directly into the DataSet special object, used to save them. It contains functions for data loading from XML/XSD and JSON file formats. After file loading, you should invoke the regData() function of the report object to connect data. In the function arguments the prepared DataSet object is specified.

 

Sample of data loading from an XML file using XSD scheme.

 

index.php

 

<?php

$report = new \Stimulsoft\Report\StiReport();

$report->onBeforeRender = 'onBeforeRender';

$report->renderHtml();

?>

 

function onBeforeRender(args) {

var dataSet = new Stimulsoft.System.Data.DataSet("SimpleDataSet");

dataSet.readXmlSchemaFile("Demo.xsd");

dataSet.readXmlFile("Demo.xml");

 

var report = args.report;

report.regData(dataSet.dataSetName, "", dataSet);

report.dictionary.synchronize();

}

 

 

Information

 

Data scheme loading is not essential. If you want to use data scheme, you should add it before XML data loading.

 

 

 

Data loading from a JSON file sample.

 

index.php

 

<?php

$report = new \Stimulsoft\Report\StiReport();

$report->onBeforeRender = 'onBeforeRender';

$report->renderHtml();

?>

 

function onBeforeRender(args) {

var dataSet = new Stimulsoft.System.Data.DataSet("SimpleDataSet");

dataSet.readJsonFile("Demo.json");

 

var report = args.report;

report.regData(dataSet.dataSetName, "", dataSet);

report.dictionary.synchronize();

}

 

 

 

There are the readXml() and the readJson() functions apart from the readXmlFile() and the readJsonFile(). They take data as a string or an object.

 

Information

 

The report.dictionary.synchronize() function is needed to synchronize connected data with data dictionary of report template. So as, when invoking this function, report dictionary created on the basis of data structure, loaded in the DataSet. The synchronization function is not required, if dictionary created in advance and its structure matches connected data.

 

 

 

Events of work with data

There is a capability to subscribe to events of work with data for more flexible work with file data.

 

 

Data loading event

To view and change parameters of file data connection before load them, you should define the onBeginProcessData event of a report object or the viewer or the designer component.

 

index.php

 

<?php

$report = new \Stimulsoft\Report\StiReport();

$report->onBeginProcessData = 'onBeginProcessData';

$report->renderHtml();

?>

 

function onBeginProcessData(args) {

var pathData = args.pathData;

}

 

 

 

In arguments of the event the information about connection to a file data source – name and type of connection in a report template and the path to the data file will be transferred. You can find a detailed description of available values of arguments in the Engine events chapter.

 

You can change the path to the data file. In this case, after the event completed, the report generator will request a file by a new path, specified in the arguments. For example, you need to change the path to the JSON data file for a specified connection.

 

index.php

 

<?php

$report = new \Stimulsoft\Report\StiReport();

$report->onBeginProcessData = 'onBeginProcessData';

$report->renderHtml();

?>

 

function onBeginProcessData(args) {

if (args.connection == "MyJsonConnection")

args.pathData = "Data/Demo.json";

}

 

 

Information

 

The onBeginProcessData event will be invoked twice for an XML data source: first time to read an XSD scheme, second time to read an XML data file.

 

 

 

Data connection event

To view or correct loaded data before connect them and render a report you should define the onEndProcessData for a report object or the viewer or the designer component.

 

index.php

 

<?php

$report = new \Stimulsoft\Report\StiReport();

$report->onEndProcessData = 'onEndProcessData';

$report->renderHtml();

?>

 

function onEndProcessData(args) {

var dataSet = args.dataSet;

}

 

 

 

In arguments of the event information about connection to a file data source – name and type of connection, saved in a report template and prepared DataSet object, which contains tables and data rows, received from the file source. You can find a detailed description of available values of the arguments in the Engine events chapter.

 

Loading data files on the PHP server-side

Sometimes you have to control loading of a data file on the server-side or for example create data array using a code. To do it you should specify the path to a PHP script or the service, which contains logic of getting data instead of specifying the path to the data file. The simplest PHP script of data loading will look like this.

 

json.php

 

<?php

echo file_get_contents('Data/Demo.json');

?>

 

 

 

In this case, you should specify URL address as the path to data in a report template to this file, for example.

 

File Data Source

 

https://localhost/data/json.php

 

 

 

Using variables in data files

There is an ability, which allows you to use variables as expressions (and use expressions) when specifying the path to a file data source. A variable or an expression is specified in braces. You can use several expressions in any place of the path to a data file. For example.

 

File Data Source

 

https://localhost/data/{VariableJsonFileName}.json

https://localhost/data/json.php?id={VariableId}

https://localhost/{VariableCategory}/{VariableId}

 

 

 

This way, one data source can be converted to the REST syntax. It will allow you not to create several the same data sources to get similar data and also in conjunction with the server PHP logic and the report generator events to make the data source more flexible.

 

Using the OData

Also, you can use data to create reports, received from the OData storages. In this case you should authorize using a user's name, user's password or a token. Authorization parameters are specified in the string of connection to a OData storage with the help of the ";" separator.

 

index.php

 

// Authorization using a user account

var oDataDatabase = new Stimulsoft.Report.Dictionary.StiODataDatabase("OData", "OData", "https://services.odata.org/V4/Northwind/Northwind.svc;AddressBearer=adress;UserName=UserName;Password=Password;Client_Id=Your Client ID", false, null);

 

// Authorization using a user token

var oDataDatabase = new Stimulsoft.Report.Dictionary.StiODataDatabase("OData", "OData", "https://services.odata.org/V4/Northwind/Northwind.svc;Token=Enter your token", false, null);

 

report.dictionary.databases.add(oDataDatabase);

report.dictionary.synchronize();

 

// Query with data filter

var productsDataSource = report.dictionary.dataSources.getByName("Products");

if (productsDataSource != null) productsDataSource.sqlCommand = "Products?$filter=ProductID eq 2";