Syncfusion AI Assistant

How can I help you?

Cross hair in EJ2 JavaScript Stock chart control

19 Mar 202621 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.

var stockChart = new ej.charts.StockChart({
     primaryXAxis: {
            valueType: 'DateTime',
        },
        series: [
            {
                dataSource: chartData, type: 'Candle'
            }
        ],
        crosshair: { enable: true },
        legendSettings: { visible: true },
        title: 'Weather Condition'
});
stockChart.appendTo('#element');
<!DOCTYPE html><html lang="en"><head>
    <title>EJ2 Animation</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="./index.css" rel="stylesheet">
    <link href="http://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/33.1.44/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Tooltip for axis

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

var stockChart = new ej.charts.StockChart({
     primaryXAxis: {
            valueType: 'DateTime',
        },
        series: [
            {
                dataSource: chartData, type: 'Candle'
            }
        ],
        crosshair: { enable: true },
        legendSettings: { visible: true },
        title: 'Weather Condition'
});
stockChart.appendTo('#element');
<!DOCTYPE html><html lang="en"><head>
    <title>EJ2 Animation</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="./index.css" rel="stylesheet">
    <link href="http://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/33.1.44/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

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.

var stockChart = new ej.charts.StockChart({
       primaryYAxis: {
            lineStyle: { color: 'transparent' },
            majorTickLines: { color: 'transparent', width: 0 },
            crosshairTooltip: {enable:true,fill: 'green'}
        },
        primaryXAxis: { majorGridLines: { color: 'transparent' },
        crosshairTooltip: {enable:true,fill: 'green'}},
        series: [
            {
                dataSource: chartData,
                type: 'Candle'
            }
        ],
       crosshair: {
            enable: true,
            line: {width: 2, color: 'green'}
        },
        title: 'AAPL Stock Price'
});
stockChart.appendTo('#element');
<!DOCTYPE html><html lang="en"><head>
    <title>EJ2 Animation</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="./index.css" rel="stylesheet">
    <link href="http://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/33.1.44/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Snap to data

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

var stockChart = new ej.charts.StockChart({
       primaryYAxis: {
            lineStyle: { color: 'transparent' },
            majorTickLines: { color: 'transparent', width: 0 },
            crosshairTooltip: {enable:true,fill: 'green'}
        },
        primaryXAxis: { majorGridLines: { color: 'transparent' },
        crosshairTooltip: {enable:true,fill: 'green'}},
        series: [
            {
                dataSource: chartData,
                type: 'Candle'
            }
        ],
       crosshair: {
            enable: true,
            line: {width: 2, color: 'green'},
            snapToData: true
        },
        title: 'AAPL Stock Price'
});
stockChart.appendTo('#element');
<!DOCTYPE html><html lang="en"><head>
    <title>EJ2 Animation</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="./index.css" rel="stylesheet">
    <link href="http://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/33.1.44/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Note: To use crosshair feature, we need to inject Crosshair module StockChart.Inject(Crosshair) method.

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.
var stockData1 = [
  { 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 },
];
var crosshairLabelRender = (args) => {
        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.color = '#d32f2f';
                args.fill = '#ffebee';
            }
        }
};

var chart = new ej.charts.StockChart({
    primaryXAxis: { valueType: 'DateTime', majorGridLines: { width: 0 }, crosshairTooltip: { enable: true } },
    primaryYAxis: { lineStyle: { color: 'transparent' }, majorTickLines: { color: 'transparent', height: 0 }, crosshairTooltip: { enable: true } },
    seriesType: [], indicatorType: [], chartArea: { border: { width: 0 } }, crosshair: { enable: true, lineType: 'Both' },
    crosshairLabelRender: crosshairLabelRender,
    series: [{
        dataSource: stockData1, xName: 'x', yName: 'high', type: 'SplineArea', opacity: 0.5
    }]
});
chart.appendTo('#element');
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Animation</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <script src="https://cdn.syncfusion.com/ej2/33.1.44/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    <div id="container">
        <div id="element"></div>
    </div>
    <script>
        var ele = document.getElementById('container');
        if (ele) {
            ele.style.visibility = "visible";
        }   
    </script>
    <script src="index.js" type="text/javascript"></script>
</body>

</html>