Connecting Data Files
Typically, the connection parameters for data sources are stored in the report template itself. For working with file-based data sources such as XML, JSON, Excel, or CSV, don't required additional actions, as all algorithms are contained within the report generator script. If necessary, other methods of data connection can be used with the report generator’s JavaScript functions. To do this, the onBeforeRender event of the StiReport object can be used. Data can be loaded directly into a special DataSet object, which is used for storing them. It contains functions for loading data from XML/XSD and JSON file formats. After loading the files, the regData() function of the report’s JavaScript object must be called to connect the data, with the prepared DataSet object specified in the function's arguments.
Example of loading data from a file:
app.py |
from stimulsoft_reports.report import StiReport
report = StiReport() report.onBeforeRender += 'beforeRender' report.loadFile(url_for('static', filename='reports/SimpleList.mrt')) report.render()
|
report.html |
<script> function beforeRender(args) { var dataSet = new Stimulsoft.System.Data.DataSet("SimpleDataSet");
dataSet.readXmlSchemaFile("Demo.xsd"); dataSet.readXmlFile("Demo.xml");
//dataSet.readJsonFile("Demo.json");
var report = args.report; report.regData(dataSet.dataSetName, "", dataSet); report.dictionary.synchronize(); } </script>
|
Information |
Loading the data schema isn’t necessarily. If you want to use a data schema, you should add it before loading the XML data.
|
In addition to the mentioned functions readXmlFile() and readJsonFile(), there are also functions readXml() and readJson(), which accept data in the form of a string or an object.
Information |
The report.dictionary.synchronize() function is required to synchronize the connected data with the report template's data dictionary. In other words, when this function is called, the report dictionary will be created based on the structure of the data loaded into the DataSet. The synchronization function is not needed if the dictionary is created in advance and its structure matches the connected data.
|
To view and modify the connection parameters of file data before loading, you need to define the onBeginProcessData event for the report object, or the viewer or designer component:
app.py |
from stimulsoft_reports.report import StiReport
report = StiReport() report.onBeginProcessData += 'beginProcessData' report.loadFile(url_for('static', filename='reports/SimpleList.mrt')) report.render()
|
report.html |
<script> function beginProcessData(args) { let pathData = args.pathData; } </script>
|
In the event arguments will contain information about the connection to the file data source, including the connection name and type in the report template, as well as the path to the data file. A detailed description of the available argument values can be found in the Report Engine Events section.
It is allowed to change the path to the data file. In this case, after the event completes, the report generator will request the file from the new path specified in the arguments. For example, if you need to change the path to the JSON data file for the specified connection:
report.html |
<script> function beginProcessData(args) { if (args.connection == "MyJsonConnection") args.pathData = "Data/Demo.json"; } </script>
|
Information |
For an XML data source, the onBeginProcessData event will be triggered twice: first for reading the XSD schema, and second for reading the actual XML data file.
|
To view and modify the connection parameters of file data before loading, you need to define the onEndProcessData event for the report object:
app.py |
from stimulsoft_reports.report import StiReport
report = StiReport() report.onEndProcessData += 'endProcessData' report.loadFile(url_for('static', filename='reports/SimpleList.mrt')) report.render()
|
report.html |
<script> function endProcessData(args) { let dataSet = args.dataSet; } </script>
|
In the event arguments will include information about the connection to the file data source such as the connection name and type saved in the report template and the prepared DataSet object containing tables and rows of data retrieved from the file source. A detailed description of the available argument values can be found in the Report Engine Events section.
Information |
The report viewer and report designer components also have properties for the events mentioned above, which can be used to manage data loading in the manner described.
|
Using variables in the data file
It is possible to use variables in the form of expressions (as well as using expressions) when specifying the path to the file data source. A variable or expression is defined within curly braces. Multiple expressions can be used anywhere in the file path, for example:
File Data Source |
https://localhost/data/{VariableJsonFileName}.json https://localhost/data/json?id={VariableId} https://localhost/{VariableCategory}/{VariableId}
|
So, a single data source can be transformed into REST syntax, eliminating the need to create multiple similar data sources to obtain uniform data. Combined with server-side Python logic and report generator events, this makes the data source even more flexible.
You can also use data retrieved from OData repositories for creating reports. In this case, authorization is required using a username, password, or token. The authorization parameters are specified in the connection string to the OData repository, using the ";" separator.
report.html |
<script> function beginProcessData(args) { let report = args.report;
// Authorization using a user account let 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 let 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"; } </script>
|