Search results

Zooming in Angular Chart component

Enable Zooming

Chart can be zoomed in three ways.

  • Selection - By setting enableSelectionZooming property to true in zoomSettings, you can zoom the chart by using the rubber band selection.
  • Mousewheel - By setting enableMouseWheelZooming property to true in zoomSettings, you can zoomin and zoomout the chart by scrolling the mouse wheel.
  • Pinch - By setting enablePinchZooming property to true in zoomSettings, you can zoom the chart through pinch gesture in touch enabled devices.

Pinch zooming is supported only in browsers that support multi-touch gestures. Currently IE11, Chrome and Opera

browsers support multi-touch in desktop devices.

To known about Zooming and Panning, you can check on this video:

Source
Preview
app.component.ts
app.module.ts
chartdata.ts
main.ts
import { Component, OnInit } from '@angular/core';
import { series1 } from 'datasource.ts';

@Component({
    selector: 'app-container',
    template:
    `<ejs-chart id="chart-container" [primaryXAxis]='primaryXAxis'[primaryYAxis]='primaryYAxis' [title]='title' [zoomSettings]='zoom' [legendSettings]='legend'>
        <e-series-collection>
            <e-series [dataSource]='chartData' type='Area' xName='x' yName='y' name='Product X' [border]='border' [animation]='animation' opacity=0.3></e-series>
        </e-series-collection>
    </ejs-chart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis: Object;
    public chartData: Object[];
    public title: string;
    public primaryYAxis: Object;
    public border: Object;
    public zoom: Object;
    public animation: Object;
    public legend: Object;
    ngOnInit(): void {
        this.chartData = series1;
        this.primaryXAxis = {
            valueType: 'DateTime',
            labelFormat: 'yMMM',
        };
        this.zoom = {
            enableMouseWheelZooming: true,
            enablePinchZooming: true,
            enableSelectionZooming: true
        };
        this.animation = { enable: false};
        this.legend = { visible: false };

    }

}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ChartModule } from '@syncfusion/ej2-angular-charts';
import { DateTimeService, AreaSeriesService } from '@syncfusion/ej2-angular-charts';
import { LegendService, ZoomService } from '@syncfusion/ej2-angular-charts';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, ChartModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ DateTimeService, AreaSeriesService, LegendService, ZoomService]
})
export class AppModule { }
/**
 * chart data source
 */
      export let series1: Object[] = [];
      let point1: Object;
      let value: number = 80;
      let i: number;
      for (i = 1; i < 500; i++) {
         if (Math.random() > .5) {
            value += Math.random();
         } else {
            value -= Math.random();
         }
         point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
         series1.push(point1);
      }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Note: To use zooming feature, we need to inject ZoomService into the NgModule.providers.

After zooming the chart, a zooming toolbar will appear with zoom,zoomin, zoomout, pan and reset buttons. Selecting the Pan option will allow to pan the chart and selecting the Reset option will reset the zoomed chart.

Modes

The mode property in zoomSettings specifies whether the chart is allowed to scale along the horizontal axis or vertical axis. The default value of the mode is XY (both axis).

There are three types of mode.

  • X - Allows us to zoom the chart horizontally.
  • Y - Allows us to zoom the chart vertically.
  • XY - Allows us to zoom the chart both vertically and horizontally.
Source
Preview
app.component.ts
app.module.ts
chartdata.ts
main.ts
import { Component, OnInit } from '@angular/core';
import { series1 } from 'datasource.ts';

@Component({
    selector: 'app-container',
    template:
    `<ejs-chart id="chart-container" [primaryXAxis]='primaryXAxis'[primaryYAxis]='primaryYAxis' [title]='title' [zoomSettings]='zoom' [legendSettings]='legend'>
        <e-series-collection>
            <e-series [dataSource]='chartData' type='Area' xName='x' yName='y' name='Product X' [border]='border' [animation]='animation' opacity=0.3></e-series>
        </e-series-collection>
    </ejs-chart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis: Object;
    public chartData: Object[];
    public primaryYAxis: Object;
    public zoom: Object;
    public animation: Object;
    public legend: Object;
    ngOnInit(): void {
        this.chartData = series1;
        this.primaryXAxis = {
            valueType: 'DateTime',
            labelFormat: 'yMMM',
        };
        this.zoom = {
            enableSelectionZooming: true,
              mode: 'X'
        };
        this.animation = { enable: false};
        this.legend = { visible: false };

    }

}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ChartModule } from '@syncfusion/ej2-angular-charts';
import { DateTimeService, AreaSeriesService } from '@syncfusion/ej2-angular-charts';
import { LegendService, ZoomService } from '@syncfusion/ej2-angular-charts';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, ChartModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ DateTimeService, AreaSeriesService, LegendService, ZoomService]
})
export class AppModule { }
/**
 * chart data source
 */
      export let series1: Object[] = [];
      let point1: Object;
      let value: number = 80;
      let i: number;
      for (i = 1; i < 500; i++) {
         if (Math.random() > .5) {
            value += Math.random();
         } else {
            value -= Math.random();
         }
         point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
         series1.push(point1);
      }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Toolbar

By default, zoomin, zoomout, pan and reset buttons will be displayed for zoomed chart. You can customize to show your desire tools in the toolbar using toolbarItems property.

Source
Preview
app.component.ts
app.module.ts
chartdata.ts
main.ts
import { Component, OnInit } from '@angular/core';
import { series1 } from 'datasource.ts';

@Component({
    selector: 'app-container',
    template:
    `<ejs-chart id="chart-container" [primaryXAxis]='primaryXAxis'[primaryYAxis]='primaryYAxis' [title]='title' [zoomSettings]='zoom' [legendSettings]='legend'>
        <e-series-collection>
            <e-series [dataSource]='chartData' type='Area' xName='x' yName='y' name='Product X' [border]='border' [animation]='animation' opacity=0.3></e-series>
        </e-series-collection>
    </ejs-chart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis: Object;
    public chartData: Object[];
    public primaryYAxis: Object;
    public zoom: Object;
    public animation: Object;
    public legend: Object;
    ngOnInit(): void {
        this.chartData = series1;
        this.primaryXAxis = {
            valueType: 'DateTime',
            labelFormat: 'yMMM',
        };
        this.zoom = {
            enableSelectionZooming: true,
            toolbarItems: ['Zoom', 'Pan', 'Reset']
        };
        this.animation = { enable: false};
        this.legend = { visible: false };

    }

}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ChartModule } from '@syncfusion/ej2-angular-charts';
import { DateTimeService, AreaSeriesService } from '@syncfusion/ej2-angular-charts';
import { LegendService, ZoomService } from '@syncfusion/ej2-angular-charts';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, ChartModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ DateTimeService, AreaSeriesService, LegendService, ZoomService]
})
export class AppModule { }
/**
 * chart data source
 */
      export let series1: Object[] = [];
      let point1: Object;
      let value: number = 80;
      let i: number;
      for (i = 1; i < 500; i++) {
         if (Math.random() > .5) {
            value += Math.random();
         } else {
            value -= Math.random();
         }
         point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
         series1.push(point1);
      }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Enable pan

Using enablePan property you can able to pan the zoomed chart without help of toolbar items.

Source
Preview
app.component.ts
app.module.ts
chartdata.ts
main.ts
import { Component, OnInit } from '@angular/core';
import { series1 } from 'datasource.ts';

@Component({
    selector: 'app-container',
    template:
    `<ejs-chart id="chart-container" [primaryXAxis]='primaryXAxis'[primaryYAxis]='primaryYAxis' [title]='title' [zoomSettings]='zoom' [legendSettings]='legend'>
        <e-series-collection>
            <e-series [dataSource]='chartData' type='Area' xName='x' yName='y' name='Product X' [border]='border' [animation]='animation' opacity=0.3></e-series>
        </e-series-collection>
    </ejs-chart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis: Object;
    public chartData: Object[];
    public primaryYAxis: Object;
    public zoom: Object;
    public animation: Object;
    public legend: Object;
    ngOnInit(): void {
        this.chartData = series1;
        this.primaryXAxis = {
            valueType: 'DateTime',
            labelFormat: 'yMMM',
            zoomFactor: 0.2, zoomPosition: 0.6
        };
        this.zoom = {
            enableSelectionZooming: true,
            enablePan: true
        };
        this.animation = { enable: false};
        this.legend = { visible: false };

    }

}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ChartModule } from '@syncfusion/ej2-angular-charts';
import { DateTimeService, AreaSeriesService } from '@syncfusion/ej2-angular-charts';
import { LegendService, ZoomService } from '@syncfusion/ej2-angular-charts';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, ChartModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ DateTimeService, AreaSeriesService, LegendService, ZoomService]
})
export class AppModule { }
/**
 * chart data source
 */
      export let series1: Object[] = [];
      let point1: Object;
      let value: number = 80;
      let i: number;
      for (i = 1; i < 500; i++) {
         if (Math.random() > .5) {
            value += Math.random();
         } else {
            value -= Math.random();
         }
         point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
         series1.push(point1);
      }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Enable Scrollbar

Using enableScrollbar property, you can able to add scrollbar for zoomed chart. Using this scrollbar, you can pan or zoom the chart.

Source
Preview
app.component.ts
app.module.ts
chartdata.ts
main.ts
import { Component, OnInit } from '@angular/core';
import { series1 } from 'datasource.ts';

@Component({
    selector: 'app-container',
    template:
    `<ejs-chart id="chart-container" [primaryXAxis]='primaryXAxis'[primaryYAxis]='primaryYAxis' [title]='title' [zoomSettings]='zoom' [legendSettings]='legend'>
        <e-series-collection>
            <e-series [dataSource]='chartData' type='Area' xName='x' yName='y' name='Product X' [border]='border' [animation]='animation' opacity=0.3></e-series>
        </e-series-collection>
    </ejs-chart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis: Object;
    public chartData: Object[];
    public primaryYAxis: Object;
    public zoom: Object;
    public animation: Object;
    public legend: Object;
    ngOnInit(): void {
        this.chartData = series1;
        this.primaryXAxis = {
            valueType: 'DateTime',
            labelFormat: 'yMMM',
            zoomFactor: 0.2, zoomPosition: 0.6
        };
        this.zoom = {
            enableSelectionZooming: true,
            enableScrollbar: true
        };
        this.animation = { enable: false};
        this.legend = { visible: false };

    }

}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ChartModule } from '@syncfusion/ej2-angular-charts';
import { DateTimeService, AreaSeriesService } from '@syncfusion/ej2-angular-charts';
import { LegendService, ZoomService, ScrollBarService } from '@syncfusion/ej2-angular-charts';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, ChartModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ DateTimeService, AreaSeriesService, LegendService, ZoomService, ScrollBarService]
})
export class AppModule { }
/**
 * chart data source
 */
      export let series1: Object[] = [];
      let point1: Object;
      let value: number = 80;
      let i: number;
      for (i = 1; i < 500; i++) {
         if (Math.random() > .5) {
            value += Math.random();
         } else {
            value -= Math.random();
         }
         point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
         series1.push(point1);
      }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Auto interval on zooming

By using enableAutoIntervalOnZooming property, the axis interval will get calculated automatically with respect to the zoomed range.

Source
Preview
app.component.ts
app.module.ts
chartdata.ts
main.ts
import { Component, OnInit } from '@angular/core';
import { series1 } from 'datasource.ts';

@Component({
    selector: 'app-container',
    template:
    `<ejs-chart id="chart-container" [primaryXAxis]='primaryXAxis'[primaryYAxis]='primaryYAxis' [title]='title' [zoomSettings]='zoom' [legendSettings]='legend'>
        <e-series-collection>
            <e-series [dataSource]='chartData' type='Area' xName='x' yName='y' name='Product X' [border]='border' [animation]='animation' opacity=0.3></e-series>
        </e-series-collection>
    </ejs-chart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis: Object;
    public chartData: Object[];
    public primaryYAxis: Object;
    public zoom: Object;
    public animation: Object;
    public legend: Object;
    ngOnInit(): void {
        this.chartData = series1;
        this.primaryXAxis = {
            valueType: 'DateTime',
            labelFormat: 'yMMM'
        };
        this.zoom = {
            enableSelectionZooming: true,
        };
        enableAutoIntervalOnZooming: true,
        this.animation = { enable: false};
        this.legend = { visible: false };

    }

}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ChartModule } from '@syncfusion/ej2-angular-charts';
import { DateTimeService, AreaSeriesService } from '@syncfusion/ej2-angular-charts';
import { LegendService, ZoomService } from '@syncfusion/ej2-angular-charts';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, ChartModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ DateTimeService, AreaSeriesService, LegendService, ZoomService]
})
export class AppModule { }
/**
 * chart data source
 */
      export let series1: Object[] = [];
      let point1: Object;
      let value: number = 80;
      let i: number;
      for (i = 1; i < 500; i++) {
         if (Math.random() > .5) {
            value += Math.random();
         } else {
            value -= Math.random();
         }
         point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
         series1.push(point1);
      }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);