ActiveReportsJS is a 100% client-side reporting tool with zero server dependencies. It means that you can use ActiveReportsJS together with any web server, including a Ruby on Rails application. This article contains a simple yet thorough tutorial on integrating ActiveReportsJS with a Ruby on Rails application.
The following content assumes that you have Ruby on Rails installed on your machine. If you don’t have it, you can obtain it from the Ruby on Rails website. It would be best if you also had ActiveReportsJS installed on your machine. If you don’t have it, you can obtain it from the ActiveReportsJS website.
Create a Ruby on Rails Application
To create a new Ruby on Rails application, run the following command from a terminal or command prompt.
rails new ReportingOnRails --api
Then open the newly created ReportingOnRails directory in your favorite code editor, such as Visual Studio Code.
Create a Data Model for the JSON API
We will use the Sales dataset that you can download from 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, which has 100 records.
There are many fields in the dataset, but we will only use several of them in this tutorial. To create a data model for the JSON API, run the following commands from the ReportingOnRails directory.
rails g model Sale region:string country:string itemType:string unitsSold:float
# or the following for Windows:
# bin\rails g model Sale region:string country:string itemType:string unitsSold:float
# ruby bin\rake db:migrate
Initialize the Data from a CSV File
Download and unzip the data from the 100-Sales-Records.zip archive into the lib\seeds directory of the application.
Install the csv gem into the application.
Then open the db/seeds.rb file and replace its content with the following code.
csv_text = File.read(Rails.root.join('lib', 'seeds', '100 Sales Records.csv'))
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1')
# puts csv
csv.each do |row|
itemType: row['Item Type'],
unitsSold: row['Units Sold'])
Finally, run the following command from the terminal or command prompt.
# or ruby bin\rake db:seed in Windows
Configure JSON API Endpoints
Open the config/routes.rb file and replace its content with the following code:
get 'api/sales', to: 'application#sales'
Now open app/controllers/application_controller.rb and paste the following code snippet:
class ApplicationController < ActionController::API
@sales = Sale.all
render json: @sales
Then run the application by using the following command:
# or ruby bin\rails server for Windows
And open the browser to http://localhost:3000/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:3000/api/sales in the ENDPOINT field and click the Save Changes button.
Click the + icon near DataSource in the Data panel.
In the Data Set Editor dialog, type Sales in the NAME field and $.* in the JSON Path field.
Click the Validate button, ensure that the DataBase Fields section displays [7 items] text, and click the Save Changes button.
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.
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 an index.html file and replace its content with the following code:
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
var viewer = new ActiveReports.Viewer("#viewer-host");
Make sure that the application runs and visit the browser to http://localhost:3000/index.html to see the report. If you followed the steps correctly, you should see a report that displays the data from the JSON API.
Originally published at https://www.grapecity.com on May 25, 2022.