This tutorial describes how to integrate Flexmonster Pivot with Electron.js – a JavaScript framework that allows creating cross-platform desktop applications.
To work with Electron.js, you need Node.js and npm. Get it here if it’s not already installed on your machine.
One more tool needed is Flexmonster CLI, which is the most convenient way to work with Flexmonster. Install the CLI globally using npm:
npm install -g flexmonster-cli
Now a new flexmonster
command is available in the console. Learn more about Flexmonster CLI and its commands in our documentation.
After that, choose one of the following options:
Create the sample project with the following CLI command:
flexmonster create electron -r
The flexmonster create electron
command will do the following:
.zip
archive with the sample Electron project from GitHub.flexmonster-electron-project/
folder will appear in your working directory.The -r
option, which stands for --run
, completes these tasks:
package.json
.As a result, a desktop application with the pivot table embedded will be launched.
This guide is based on the first Electron app tutorial. Follow the steps below to integrate Flexmonster Pivot into a new Electron.js application. If you already have the Electron project, jump to Step 6. Install Flexmonster.
Create an empty folder for the new Electron application (e.g., pivot-electron
) and run the following command from that folder:
npm init
npm will start creating a basic package.json
file. When initializing package.json
, notice the "main"
field – a script specified in this field is the application’s entry point. In this tutorial, main.js
is the entry point.
As a result, the package.json
file should look similar to the following:
{
"name": "pivot-electron",
"version": "1.0.0",
"main": "main.js"
}
If there is no "main"
field specified in package.json
, the default entry point is index.js
.
By default, the npm start
command runs the application with Node.js. To run the application with Electron, add the following start script to package.json
:
"scripts": {
"start": "electron .",
},
Install Electron.js in the project by running the following command in the console:
npm install electron
HTML files define the Electron application’s user interface. Create the HTML file (e.g., index.html
) with a simple markup:
<!DOCTYPE html>
<html>
<head>
<title>Pivot table for Desktop</title>
</head>
<body>
</body>
</html>
In this tutorial, the entry point for the application is main.js
. Create a simple main.js
file that waits for the application to be ready and then opens the window:
const { app, BrowserWindow } = require('electron')
function createWindow () {
// create the browser window
let win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
// load the previously created index.html file
win.loadFile('index.html')
}
// open the window when the application is ready
app.whenReady().then(createWindow)
Install Flexmonster by running this CLI command from the folder containing package.json
:
flexmonster add flexmonster
The add
command will install the flexmonster
npm package to node_modules/
and add it as an npm dependency to package.json
.
In the index.html
file, add a container for Flexmonster:
<body>
<div id="pivotContainer"></div>
</body>
Create a new .js
file (e.g., pivot.js
) for Flexmonster and include the Flexmonster npm module into it:
require('flexmonster');
Add a new Flexmonster instance to the pivot.js
file:
const pivot = new Flexmonster({
container: "pivotContainer",
toolbar: true,
report: "https://cdn.flexmonster.com/reports/report.json"
});
Include the script embedding Flexmonster to the index.html
file:
<div id="pivotContainer"></div>
<script src="./pivot.js"></script>
Include Flexmonster styles into the index.html
file. It should be done as follows:
<head>
<title>Pivot table for Desktop</title>
<link rel="stylesheet" type="text/css"
href="https://cdn.flexmonster.com/flexmonster.min.css"
/>
</head>
Run the project from the console with the following command:
npm start
A desktop application with the pivot table embedded will be launched.
The application can be shut down manually with Ctrl+C
.
You may be interested in the following articles: