HelpBot Assistant

How can I help you?

Cross hair in Angular Stock chart component

19 Mar 202623 minutes to read

Crosshair has a vertical and horizontal line to view the value of the axis at mouse or touch position.

Crosshair lines can be enabled by using enable property in the crosshair. Likewise tooltip label for an axis can be enabled by using enable property of crosshairTooltip in the corresponding axis.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ChartModule, StockChartAllModule, ChartAllModule } from '@syncfusion/ej2-angular-charts'
import { DateTimeService, LineSeriesService} from '@syncfusion/ej2-angular-charts'
import { LegendService, CrosshairService } from '@syncfusion/ej2-angular-charts'



import { Component, OnInit } from '@angular/core';
import { chartData } from './datasource';

@Component({
imports: [
         ChartModule, StockChartAllModule, ChartAllModule
    ],

providers: [ DateTimeService, LineSeriesService, LegendService, CrosshairService],
standalone: true,
    selector: 'app-container',
    template: `<ejs-stockchart id="chart-container" [primaryXAxis]='primaryXAxis' [title]='title' [crosshair]='crosshair'>
        <e-stockchart-series-collection>
            <e-stockchart-series [dataSource]='chartData' type='Candle' xName='date' yName='open' name='India' width=2 ></e-stockchart-series>
        </e-stockchart-series-collection>
    </ejs-stockchart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis?: Object;
    public chartData?: Object[];
    public title?: string;
    public crosshair?: Object;
    ngOnInit(): void {
        this.chartData = chartData;
        this.title = 'Efficiency of oil-fired power production';
        this.primaryXAxis = {
           valueType: 'DateTime'
        };
        this.crosshair= {
            enable: true
        };
    }

}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Tooltip for axis

Tooltip label for an axis can be enabled by using enable property of crosshairTooltip in the corresponding axis.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ChartModule, StockChartAllModule, ChartAllModule } from '@syncfusion/ej2-angular-charts'
import { DateTimeService, LineSeriesService} from '@syncfusion/ej2-angular-charts'
import { LegendService, CrosshairService } from '@syncfusion/ej2-angular-charts'



import { Component, OnInit } from '@angular/core';
import { chartData } from './datasource';

@Component({
imports: [
         ChartModule, StockChartAllModule, ChartAllModule
    ],

providers: [ DateTimeService, LineSeriesService, LegendService, CrosshairService],
standalone: true,
    selector: 'app-container',
    template: `<ejs-stockchart id="chart-container" [primaryXAxis]='primaryXAxis'[primaryYAxis]='primaryYAxis' [title]='title' [crosshair]='crosshair'>
        <e-stockchart-series-collection>
            <e-stockchart-series [dataSource]='chartData' type='Candle' xName='date' yName='open' name='India' width=2 ></e-stockchart-series>
        </e-stockchart-series-collection>
    </ejs-stockchart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis?: Object;
    public primaryYAxis?: Object;
    public chartData?: Object[];
    public title?: string;
    public crosshair?: Object;
    ngOnInit(): void {
        this.chartData = chartData;
        this.title = 'Efficiency of oil-fired power production';
        this.primaryXAxis = {
           valueType: 'DateTime',
           crosshairTooltip: {enable:true}
        };
        this.primaryYAxis = {
           majorTickLines: { color: 'transparent', width: 0 },
           crosshairTooltip: {enable:true}
        };
        this.crosshair= {
            enable: true
        };
    }

}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Customization

The fill and textStyle property of the crosshairTooltip is used to customize the background color and font style of the crosshair label respectively.
Color and width of the crosshair line can be customized by using the line property in the crosshair.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ChartModule, StockChartAllModule, ChartAllModule } from '@syncfusion/ej2-angular-charts'
import { DateTimeService, LineSeriesService} from '@syncfusion/ej2-angular-charts'
import { LegendService, CrosshairService } from '@syncfusion/ej2-angular-charts'



import { Component, OnInit } from '@angular/core';
import { chartData } from './datasource';

@Component({
imports: [
         ChartModule, StockChartAllModule, ChartAllModule
    ],

providers: [ DateTimeService, LineSeriesService, LegendService, CrosshairService],
standalone: true,
    selector: 'app-container',
    template: `<ejs-stockchart id="chart-container" [primaryXAxis]='primaryXAxis'[primaryYAxis]='primaryYAxis' [title]='title' [crosshair]='crosshair'>
        <e-stockchart-series-collection>
            <e-stockchart-series [dataSource]='chartData' type='Candle' xName='date' yName='open' name='India' width=2 ></e-stockchart-series>
        </e-stockchart-series-collection>
    </ejs-stockchart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis?: Object;
    public primaryYAxis?: Object;
    public chartData?: Object[];
    public title?: string;
    public crosshair?: Object;
    ngOnInit(): void {
        this.chartData = chartData;
        this.title = 'Efficiency of oil-fired power production';
        this.primaryXAxis = {
           valueType: 'DateTime',
           crosshairTooltip: {enable:true}
        };
        this.primaryYAxis = {
           majorTickLines: { color: 'transparent', width: 0 },
           crosshairTooltip: {enable:true}
        };
        this.crosshair= {
            enable: true,
            line: {width: 2, color: 'green'}
        };
    }

}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Snap to data

Enabling the snapToData property in the crosshair aligns it with the nearest data point instead of following the exact mouse position.

import { ChartModule, StockChartAllModule, ChartAllModule } from '@syncfusion/ej2-angular-charts';
import { DateTimeService, LineSeriesService} from '@syncfusion/ej2-angular-charts';
import { LegendService, CrosshairService } from '@syncfusion/ej2-angular-charts';
import { Component, OnInit } from '@angular/core';
import { chartData } from './datasource';

@Component({
imports: [
         ChartModule, StockChartAllModule, ChartAllModule
    ],

providers: [ DateTimeService, LineSeriesService, LegendService, CrosshairService],
standalone: true,
    selector: 'app-container',
    template: `<ejs-stockchart id="chart-container" [primaryXAxis]='primaryXAxis' [primaryYAxis]='primaryYAxis' [title]='title' [crosshair]='crosshair'>
        <e-stockchart-series-collection>
            <e-stockchart-series [dataSource]='chartData' type='Candle'></e-stockchart-series>
        </e-stockchart-series-collection>
    </ejs-stockchart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis?: Object;
    public primaryYAxis?: Object;
    public chartData?: Object[];
    public title?: string;
    public crosshair?: Object;
    ngOnInit(): void {
        this.chartData = chartData;
        this.title = 'AAPL Stock Price';
        this.primaryXAxis = {
            majorGridLines: { color: 'transparent' },
            crosshairTooltip: { enable: true, fill: 'green' }
        };
        this.primaryYAxis = {
            lineStyle: { color: 'transparent' },
            majorTickLines: { color: 'transparent', width: 0 },
            crosshairTooltip: { enable: true, fill: 'green' }
        };
        this.crosshair= {
            enable: true,
            line: { width: 2, color: 'green' },
            snapToData: true
        };
    }

}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Crosshair Label Customization

The crosshairLabelRender event is triggered before each crosshair axis label is rendered in the stock chart. This event provides the ability to customize the appearance and content of crosshair labels, or to conditionally prevent specific labels from being displayed.

The event arguments include:

  • text – The default text for the crosshair label. You can modify this value to display custom content.
  • value – The actual data value at the crosshair position.
  • axisName – The name of the axis associated with the label.
  • axisOrientation – The orientation of the axis, either Horizontal or Vertical.
  • textStyle – Font properties for the label text, allowing customization of font family, size, weight, and color.
  • fill – The background color of the crosshair label.
  • cancel – Set this to true to prevent the label from being rendered.
import { Component, OnInit } from '@angular/core';
import { ICrosshairLabelRenderEventArgs, StockChartModule } from '@syncfusion/ej2-angular-charts';
import {
  DateTimeService, SplineAreaSeriesService, RangeTooltipService, CrosshairService,
  LineSeriesService, SplineSeriesService, CandleSeriesService, HiloOpenCloseSeriesService,
  HiloSeriesService, RangeAreaSeriesService, TrendlinesService, EmaIndicatorService,
  RsiIndicatorService, BollingerBandsService, TmaIndicatorService, MomentumIndicatorService,
  SmaIndicatorService, AtrIndicatorService, ExportService, AccumulationDistributionIndicatorService,
  MacdIndicatorService, StochasticIndicatorService
} from '@syncfusion/ej2-angular-charts';

import { stockData1 } from './datasource';

@Component({
    imports: [
        StockChartModule
    ],
    providers: [
        DateTimeService, SplineAreaSeriesService, RangeTooltipService, CrosshairService,
        LineSeriesService, SplineSeriesService, CandleSeriesService, HiloOpenCloseSeriesService,
        HiloSeriesService, RangeAreaSeriesService, TrendlinesService, EmaIndicatorService,
        RsiIndicatorService, BollingerBandsService, TmaIndicatorService, MomentumIndicatorService,
        SmaIndicatorService, AtrIndicatorService, ExportService, AccumulationDistributionIndicatorService,
        MacdIndicatorService, StochasticIndicatorService
    ],
    standalone: true,
    selector: 'app-container',
    template: `<ejs-stockchart id="stockchartsplinearea" [primaryXAxis]="primaryXAxis" [primaryYAxis]="primaryYAxis"
        [chartArea]="chartArea" [crosshair]="crosshair" (crosshairLabelRender)="crosshairLabelRender($event)">
        <e-stockchart-series-collection>
            <e-stockchart-series [dataSource]="chartData" xName="x" yName="high" type="SplineArea" [opacity]="0.5">
            </e-stockchart-series>
        </e-stockchart-series-collection>
    </ejs-stockchart>`
})
export class AppComponent implements OnInit {
    public primaryXAxis?: Object;
    public primaryYAxis?: Object;
    public chartArea?: Object;
    public crosshair?: Object;
    public chartData?: Object[];

    ngOnInit(): void {
        this.chartData = stockData1;

        this.primaryXAxis = {
            valueType: 'DateTime',
            majorGridLines: { width: 0 },
            crosshairTooltip: { enable: true }
        };

        this.primaryYAxis = {
            lineStyle: { color: 'transparent' },
            majorTickLines: { color: 'transparent', height: 0 },
            crosshairTooltip: { enable: true }
        };

        this.chartArea = {
            border: { width: 0 }
        };

        this.crosshair = {
            enable: true,
            lineType: 'Both'
        };
    }

    crosshairLabelRender(args: ICrosshairLabelRenderEventArgs): void {
        if (args.axisName === 'primaryXAxis') {
            const date = args.value instanceof Date ? args.value : new Date(args.text);
            args.text = date.toLocaleDateString('en-IN', { day: '2-digit', month: 'short', year: 'numeric' });
        }

        if (args.axisName === 'primaryYAxis') {
            const price = typeof args.value === 'number' ? args.value : Number(args.text);
            args.text = '₹' + price.toLocaleString('en-IN', { maximumFractionDigits: 0 });

            if (price > 310) {
                args.textStyle = args.textStyle || {};
                args.textStyle.color = '#d32f2f';
                args.fill = '#ffebee';
            }
        }
    }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';

bootstrapApplication(AppComponent).catch((err) => console.error(err));
export let stockData1: object[] = [
    { x: new Date('2012-04-02'), open: 320.705719, high: 324.074066, low: 317.737732, close: 323.783783, volume: 45638000 },
    { x: new Date('2012-04-03'), open: 323.028015, high: 324.299286, low: 319.639648, close: 321.631622, volume: 40857000 },
    { x: new Date('2012-04-04'), open: 319.544556, high: 319.819824, low: 315.865875, close: 317.892883, volume: 32519000 },
    { x: new Date('2012-04-05'), open: 316.436432, high: 318.533539, low: 314.599609, close: 316.476471, volume: 46327000 },
    { x: new Date('2012-04-09'), open: 314.554565, high: 317.982971, low: 312.957947, close: 315.735748, volume: 43610000 },
    { x: new Date('2012-04-10'), open: 317.077087, high: 317.567566, low: 312.587585, close: 313.743744, volume: 49590000 },
    { x: new Date('2012-04-11'), open: 317.302307, high: 318.318329, low: 315.965973, close: 318.298309, volume: 43936000 },
    { x: new Date('2012-04-12'), open: 321.49649, high: 326.896912, low: 320.450439, close: 325.830841, volume: 11501600 },
    { x: new Date('2012-04-13'), open: 324.099091, high: 324.819824, low: 312.082092, close: 312.61261, volume: 16302200 },
    { x: new Date('2012-04-16'), open: 311.811798, high: 312.217224, low: 301.131134, close: 303.338348, volume: 11372800 },
    { x: new Date('2012-04-17'), open: 304.584595, high: 309.154144, low: 303.808807, close: 305.090088, volume: 60417000 },
    { x: new Date('2012-04-18'), open: 304.329315, high: 306.706696, low: 301.706696, close: 304.029022, volume: 53336000 },
    { x: new Date('2012-04-19'), open: 303.148163, high: 308.438446, low: 299.799805, close: 299.949951, volume: 65826000 },
    { x: new Date('2012-04-20'), open: 302.427429, high: 304.729736, low: 298.213226, close: 298.328339, volume: 61106000 },
    { x: new Date('2012-04-23'), open: 296.746735, high: 299.524536, low: 295.395386, close: 299.099091, volume: 43912000 },
    { x: new Date('2012-04-24'), open: 299.419434, high: 303.618622, low: 298.958954, close: 300.935944, volume: 38543000 },
    { x: new Date('2012-04-25'), open: 302.302307, high: 305.980988, low: 301.74173, close: 305.165161, volume: 36385000 },
    { x: new Date('2012-04-26'), open: 305.760773, high: 309.309296, low: 305.155151, close: 308.04303, volume: 41808000 },
    { x: new Date('2012-04-27'), open: 307.81781, high: 308.67868, low: 305.605591, close: 307.797791, volume: 32695000 },
    { x: new Date('2012-04-30'), open: 306.801788, high: 308.348358, low: 300.605591, close: 302.727722, volume: 48097000 }
];