Need a special offer?Find out if your project fits.
+
All documentation
  • Introduction
  • Connecting to Data Source
  • Browser compatibility
  • Documentation for older versions
  • Integration with Electron.js

    This tutorial describes how to integrate Flexmonster Pivot with Electron.js – a JavaScript framework that allows creating cross-platform desktop applications.

    Prerequisites

    Run a sample project from GitHub

    Create the sample project with the following CLI command:

    flexmonster create electron -r

    The flexmonster create electron command does the following:

    • Downloads a .zip archive with the sample Electron project from GitHub.
    • Automatically unpacks the files in the current folder — as a result, the flexmonster-electron-project/ folder will appear in your working directory.

    The -r option, which is short for --run:

    • Installs all the npm dependencies described in package.json.
    • Compiles the application and runs it.

    Once the command is executed, a desktop application with the pivot table embedded will be launched.

    Integrate Flexmonster into an Electron application

    This guide is based on the Electron quick start 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

    Step 1. Initialize the package.json file

    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.

    The newly created package.json file should look similar to the following:

    {
       "name": "pivot-electron",
       "version": "1.0.0",
       "main": "main.js"
    }

    Note If there is no "main" field specified in package.json, the default entry point is index.js.

    Step 2. Add a script to run the Electron app

    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 .",
    },

    Step 3. Install Electron.js

    Install Electron.js in the project by running the following command in the console:

    npm install electron

    Step 4. Create an HTML file

    HTML files define the Electron application’s user interface. Create an HTML file (e.g., index.html) with a simple markup:

    <!DOCTYPE html>
    <html>
        <head>
            <title>Pivot table for Desktop</title>
        </head>
        <body>
        </body>
    </html> 

    Step 5. Create the main.js file

    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,
                contextIsolation: false
            }
        })
        
        // load the previously created index.html file
        win.loadFile('index.html')
    }
    
    // open the window when the application is ready
    app.whenReady().then(createWindow)

    Step 6. Install Flexmonster

    Install Flexmonster by running this CLI command inside your project:

    flexmonster add js-flexmonster

    The add command installs the flexmonster npm package to node_modules/ and adds it as an npm dependency to package.json.

    Step 7. Add a container for Flexmonster

    In the index.html file, add a container for Flexmonster:

    <body>
        <div id="pivotContainer"></div>
    </body> 

    Step 8. Create a file for a Flexmonster instance

    Create a new .js file (e.g., pivot.js) for Flexmonster and include the Flexmonster npm module into it:

    require('flexmonster');

    Step 9. Add a Flexmonster instance

    Add a new Flexmonster instance to the pivot.js file:

    const pivot = new Flexmonster({
      container: "pivotContainer",
      toolbar: true,
      report: "https://cdn.flexmonster.com/github/demo-report.json"
    });

    Step 10. Add Flexmonster to the index.html file

    Include the script to embed Flexmonster to the index.html file:

    <div id="pivotContainer"></div>
    <script src="./pivot.js"></script> 

    Step 11. Include Flexmonster styles

    Include Flexmonster styles into the index.html file as follows:

    <head>
        <title>Pivot table for Desktop</title>
        <link rel="stylesheet" type="text/css" 
         href="node_modules/flexmonster/flexmonster.min.css"
        />
    </head> 

    Step 12. Run the project

    Run the project from the console with the following command:

    npm start

    A desktop application with the embedded pivot table will be launched.

    The application can be shut down manually with Ctrl + C.

    What’s next?

    You may be interested in the following articles: