Integration with Highcharts

This detailed tutorial will walk you through the integration of WebDataRocks with Highcharts.

Supported chart types

WebDataRocks supports the following chart types of the Highcharts library:

Follow the steps below to start creating interactive data visualizations.

Step 1. Add WebDataRocks to your project

Step 1.1. Complete the Quick start guide. Your code of the component with WebDatarocks should look similar to the following:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
    }
    <app-wbr-pivot
     [toolbar]="true">
    </app-wbr-pivot>

    Step 1.2. Create a report for WebDataRocks — connect to the data source and define which fields should be displayed in rows, columns, and measures:

      import { Component} from "@angular/core";
      
      @Component({
        selector: "app-root",
        templateUrl: "./app.component.html",
        styleUrls: ["./app.component.css"],
      })
      export class AppComponent {
        report = {
          dataSource: {
            filename: "https://cdn.webdatarocks.com/data/data.csv",
          },
          slice: {
            rows: [
              {
                uniqueName: "Country",
              },
            ],
            columns: [
              {
                uniqueName: "Business Type",
              },
              {
                uniqueName: "Measures",
              },
            ],
            measures: [
              {
                uniqueName: "Price",
                aggregation: "sum",
              },
            ],
          },
        };
      }
      <app-wbr-pivot
       [toolbar]="true"
       [report]="report">
      </app-wbr-pivot>

      The fields you’ve specified in the report will be shown on the chart.

      Step 2. Get a reference to the WebDataRocks instance

      Some of WebDataRocks methods and events are needed to create a chart. Using a reference to the WebDataRocks instance, we can access WebDataRocks API.

      Import WebdatarocksComponent and get a reference to the <app-wbr-pivot> instance using a template variable and the @ViewChild decorator:

        import { Component, ViewChild } from "@angular/core";
        import { WebdatarocksComponent } from "@webdatarocks/ngx-webdatarocks";
        
        @Component({
          selector: "app-root",
          templateUrl: "./app.component.html",
          styleUrls: ["./app.component.css"],
        })
        export class AppComponent {
          @ViewChild("pivot") pivotRef!: WebdatarocksComponent;
        
          // ...
        }
        <app-wbr-pivot #pivot
         [toolbar]="true"
         [report]="report">
        </app-wbr-pivot>

        Now it’s possible to interact with the component through this.pivotRef.webdatarocks.

        Note. If you are using the legacy @webdatarocks/ng-webdatarocks wrapper, import WebdatarocksComponent from it.

        Step 3. Add Highcharts

        Step 3.1. Download the Highcharts and Highcharts Angular wrapper npm packages:

        npm install highcharts highcharts-angular

        Step 3.2. Import the HighchartsChartModule into src/app/app.module.ts:

        // ...
        import { WebdatarocksPivotModule } from "@webdatarocks/ngx-webdatarocks";
        import { HighchartsChartModule } from "highcharts-angular";

        @NgModule({
        // ...
        imports: [
        // ...
        WebdatarocksPivotModule,
        HighchartsChartModule
        ],
        // ...
        })
        // ...

        Step 3.3. Import Highcharts into your component:

        import * as Highcharts from "highcharts";

        Step 3.4. Add Highcharts in the .html file of your component using the highcharts-chart directive:

        <app-wbr-pivot #pivot
        [toolbar]="true"
        [report]="report">
        </app-wbr-pivot>
        <highcharts-chart>
        </highcharts-chart>

        Step 3.5. Specify the following attributes and variables to configure Highcharts:

          <highcharts-chart
            [Highcharts]="Highcharts"
            [options]="chartOptions"
            [oneToOne]="true"
            style="display: block;">
          </highcharts-chart>
          Highcharts = Highcharts;
          chartOptions = {};

          Step 4. Show the data from the pivot table on the chart

          Step 4.1. Import the WebDataRocks Connector for Highcharts:

          import "@webdatarocks/webdatarocks/webdatarocks.highcharts.js";

          The Connector provides the highcharts.getData() method, which gets data from WebDataRocks and converts it to the format required for a specific chart type.

          Step 4.2. If we call the highcharts.getData() method before WebDataRocks is fully loaded and ready to use, it will return an empty result. To know when WebDataRocks is ready to provide data for the chart, handle the reportcomplete event:

            onReportComplete() {
              // Unsubscribing from reportcomplete
              // We need it only to track the initialization of WebDataRocks 
              this.pivotRef.webDataRocks.off("reportcomplete");
              this.createChart();
            }
            <app-wbr-pivot #pivot
             [toolbar]="true"
             [report]="report"
             <b>(reportcomplete)="onReportComplete()"</b>>
            </app-wbr-pivot>

            Now the chart will be created only when the data is loaded and the report is ready. 

            Step 4.3. Implement the createChart() function. It will use the highcharts.getData() method from the Connector:

            createChart() {
            this.pivotRef.webDataRocks.highcharts?.getData(
            {
            type: "spline"
            },
            // Function called when data for the chart is ready
            (data) => {
            this.chartOptions = data;
            },
            // Function called on report changes (filtering, sorting, etc.)
            (data) => {
            this.chartOptions = data;
            }
            );
            }

            Step 5. Run the project

            Run your project with the following command:

            ng serve --open

            Open http://localhost:4200/ in the browser to see how the pivot table looks in combination with Highcharts.

            To see what a real-time interaction is, try experimenting: filter the data, change the measures and the aggregation functions — the results are reflected on the chart at once.

            Check out the full code

            After completing this tutorial, the full code of the component should look as follows:

              import { Component, ViewChild } from "@angular/core";
              import { WebdatarocksComponent } from "@webdatarocks/ngx-webdatarocks";
              import * as Highcharts from "highcharts";
              import "@webdatarocks/webdatarocks/webdatarocks.highcharts.js";
              
              @Component({
                selector: "app-root",
                templateUrl: "./app.component.html",
                styleUrls: ["./app.component.css"],
              })
              export class AppComponent {
                @ViewChild("pivot") pivotRef!: WebdatarocksComponent;
              
                report = {
                  dataSource: {
                    filename: "https://cdn.webdatarocks.com/data/data.csv",
                  },
                  slice: {
                    rows: [
                      {
                        uniqueName: "Country",
                      },
                    ],
                    columns: [
                      {
                        uniqueName: "Business Type",
                      },
                      {
                        uniqueName: "Measures",
                      }
                    ],
                    measures: [
                      {
                        uniqueName: "Price",
                        aggregation: "sum",
                      },
                    ],
                  },
                };
              
                Highcharts = Highcharts;
                chartOptions = {};
              
                onReportComplete() {
                  this.pivotRef.webDataRocks.off("reportcomplete");
                  this.createChart();
                }
              
                createChart() {
                  this.pivotRef.webDataRocks.highcharts?.getData(
                    {
                      type: "spline"
                    },
                    // Function called when data for the chart is ready
                    (data) => {
                      this.chartOptions = data;
                    },
                    // Function called on report changes (filtering, sorting, etc.) 
                    (data) => {
                      this.chartOptions = data;
                    }
                  );
                }
              }
              <app-wbr-pivot #pivot
               [toolbar]="true"
               [report]="report"
               (reportcomplete)="onReportComplete()">
              </app-wbr-pivot>
              <highcharts-chart
                [Highcharts]="Highcharts"
                [options]="chartOptions"
                [oneToOne]="true"
                style="display: block;">
              </highcharts-chart>

              See also