Skip to main content Skip to footer

How to Use a JavaScript Reporting Tool in Your PHP Web Application

ActiveReportsJS is a 100% client-side reporting tool with zero server dependencies. It means that you can use ActiveReportsJS and any web server, including a PHP application. This article contains a simple yet thorough tutorial on integrating ActiveReportsJS with a PHP application. By the end, you will be able to do the following.

Prerequisites 

The following content assumes that you have Laravel installed on your machine. If you don't have it, you can obtain it from the Laravel website. It would help if you also had ActiveReportsJS installed on your device. You can get it from the ActiveReportsJS website if you don't have it.

Create a PHP Application

For this tutorial, we will use the Laravel web framework. To create a new PHP application that uses the Laravel web framework, follow the official documentation at the Laravel website. This article assumes that the name of the newly created application is "ReportingOnLaravel."

Make sure that the application is up and running by visiting http://localhost and then opening the newly created ReportingOnLaravel directory in your favorite code editor, such as Visual Studio Code.

Add Application Data

We will use the Sales dataset that you can download from the E for Excel website. It offers datasets of various sizes, starting from 100 records to 5M records. For simplicity, we will use the first dataset with 100 records.

Download and unzip the data from the 100-Sales-Records.zip  archive into the "storage" directory of the application. 

There are many fields in the dataset, but we will only use several of them in this tutorial. 

Configure JSON API Endpoints

Open the "routes\web.php" file of the application and add the following code:

Route::get('/api/sales', function () {  
    $sales = [];
    if (($open = fopen(storage_path() . "/100 Sales Records.csv", "r")) !== FALSE) {
        $header = fgetcsv($open);
        $keys=array("region", "itemType", "unitsSold");
        while (($data = fgetcsv($open)) !== FALSE) {
            $sales[] = array_combine($keys, array($data[0], $data[2], $data[8]));
        }
        fclose($open);
    }    
    return response()->json($sales);
});

Make sure that the application runs and open the browser to http://localhost/api/sales to see the data the JSON API returns.

Create an ActiveReportsJS Report

Let's now create a report that visualizes the data from the JSON API.

In the Standalone Report Designer Application, click the File menu and select the Continuous Page Layout template for a newly created report.

Open the Data panel of the property inspector and click the Add button.

In the Data Source editor dialog, type http://localhost/api/sales in the ENDPOINT field and click the Save Changes button.

Data Source

Click the + icon near DataSource in the Data panel.

Type Sales in the NAME field in the Data Set Editor dialog and $.* in the JSON Path field.

Click the Validate button, ensure that the DataBase Fields section displays [3 items] text, and click the Save Changes button.

Sales

Expand the toolbox using the Hamburger menu located on the toolbar’s left side.

Drag and drop the Chart item from the toolbox to the report page area's top-left corner. The Chart Wizard dialog appears. Select the Bar type and click the Next button on the first screen.

On the second screen of the dialog, configure the data as shown in the following image and click the Next button.

Chart Wizard

On the third screen, click the Finish button.

Resize the chart report item to fill the entire width of the report page. Click the chart legend to load its properties into the properties panel and set the Orientation property to Horizontal and Position property to Bottom.

Click the File menu and save the newly created report in the public folder of the application under the name SalesReport.rdlx-json.

Create a Static HTML Page to Display the Report

In the application's public folder, create the report.html file and replace its content with the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sales Report</title>
    <link
    rel="stylesheet"
    href="https://cdn.grapecity.com/activereportsjs/3.latest/styles/ar-js-ui.css"
    type="text/css"
  />
  <link
    rel="stylesheet"
    href="https://cdn.grapecity.com/activereportsjs/3.latest/styles/ar-js-viewer.css"
    type="text/css"
  />
  <script src="https://cdn.grapecity.com/activereportsjs/3.latest/dist/ar-js-core.js"></script>
  <script src="https://cdn.grapecity.com/activereportsjs/3.latest/dist/ar-js-viewer.js"></script>
  <script src="https://cdn.grapecity.com/activereportsjs/3.latest/dist/ar-js-pdf.js"></script>
  <style>
    #viewer-host {
      width: 100%;
      height: 100vh;
    }
  </style>      
</head>
   
<body>
    <div id="viewer-host"></div>
    <script>
        var viewer = new ActiveReports.Viewer("#viewer-host");
        viewer.open('SalesReport.rdlx-json');
      </script>  
</body>
</html>

Open the http://localhost/report.html in the browser to see the report. If you followed the steps correctly, you should see a report displaying the JSON API data.

Output

comments powered by Disqus