Crosshair in ASP.NET CORE Chart Component

19 Dec 202417 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.

<ejs-chart id="container" width="60%" load="window.onload">
        <e-chart-crosshairsettings enable="true">
        </e-chart-crosshairsettings>
        <e-chart-primaryxaxis valueType="DateTime"></e-chart-primaryxaxis>
        <e-series-collection>
            <e-series xName="x" yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Line">
            </e-series>
        </e-series-collection>
    </ejs-chart>
<script>
    window.onload = function (args) {
        var series1 = [];
        var point1;
        var value = 40;
        var i;
        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);
        }
        args.chart.series[0].dataSource = series1;
    }
</script>
public ActionResult Index()
        {
            return View();
        }

Tooltip for axis

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

@{
 var tooltip = new { enable = true };
}
    <ejs-chart id="container" width="60%" load="window.onload">
        <e-chart-primaryyaxis crosshairTooltip="tooltip"></e-chart-primaryyaxis>
        <e-chart-crosshairsettings enable="true">
        </e-chart-crosshairsettings>
        <e-chart-primaryxaxis valueType="DateTime" crosshairTooltip="tooltip"></e-chart-primaryxaxis>
        <e-series-collection>
            <e-series xName="x" yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Line">
            </e-series>
        </e-series-collection>
    </ejs-chart>
<script>
    window.onload = function (args) {
        var series1 = [];
        var point1;
        var value = 40;
        var i;
        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);
        }
        args.chart.series[0].dataSource = series1;
    }
</script>
public ActionResult Index()
        {
            return View();
        }

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 tooltip = new { enable = true, fill="green" };
    var line = new { width = 2, color = "green" };
}
    <ejs-chart id="container" width="60%" load="window.onload">
        <e-chart-primaryyaxis crosshairTooltip="tooltip"></e-chart-primaryyaxis>
        <e-chart-crosshairsettings enable="true" line="line">
        </e-chart-crosshairsettings>
        <e-chart-primaryxaxis valueType="DateTime" crosshairTooltip="tooltip"></e-chart-primaryxaxis>
        <e-series-collection>
            <e-series xName="x" yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Line">
            </e-series>
        </e-series-collection>
    </ejs-chart>
<script>
    window.onload = function (args) {
        var series1 = [];
        var point1;
        var value = 40;
        var i;
        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);
        }
        args.chart.series[0].dataSource = series1;
    }
</script>
public ActionResult Index()
        {
            return View();
        }

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 tooltip = new { enable = true, fill="green" };
    var line = new { width = 2, color = "green" };
}
    <ejs-chart id="container" width="60%" load="window.onload">
        <e-chart-primaryyaxis crosshairTooltip="tooltip"></e-chart-primaryyaxis>
        <e-chart-crosshairsettings enable="true" lineType="Horizontal" snapToData="true">
        </e-chart-crosshairsettings>
        <e-chart-primaryxaxis valueType="DateTime" crosshairTooltip="tooltip"></e-chart-primaryxaxis>
        <e-series-collection>
            <e-series xName="x" yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Line">
            </e-series>
        </e-series-collection>
    </ejs-chart>
<script>
    window.onload = function (args) {
        var series1 = [];
        var point1;
        var value = 40;
        var i;
        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);
        }
        args.chart.series[0].dataSource = series1;
    }
</script>
public ActionResult Index()
        {
            return View();
        }

Trackball

Trackball is used to track a data point closest to the mouse or touch position. Trackball marker indicates the closest point and trackball tooltip displays the information about the point.

Trackball can be enabled by setting the enable property of the crosshair to true and shared property in tooltip to true in chart.

<ejs-chart id="container" width="60%" load="window.onload">
        <e-chart-crosshairsettings enable="true" lineType="Vertical">
        </e-chart-crosshairsettings>
        <e-chart-tooltipsettings enable="true" shared="true" format="${series.name} : ${point.x} : ${point.y}">
        </e-chart-tooltipsettings>
        <e-chart-primaryxaxis valueType="DateTime"></e-chart-primaryxaxis>
        <e-series-collection>
            <e-series dataSource="ViewBag.dataSource" xName="xValue" yName="yValue" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Line"></e-series>
            <e-series dataSource="ViewBag.dataSource" xName="xValue" yName="yValue1" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Line"></e-series>
            <e-series dataSource="ViewBag.dataSource" xName="xValue" yName="yValue2" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Line"></e-series>
        </e-series-collection>
    </ejs-chart>
public ActionResult Index()
        {
            List<TrackballChartData> chartData = new List<TrackballChartData>
            {
                new TrackballChartData { xValue = new DateTime(2000, 2, 11), yValue = 14, yValue1 = 39, yValue2 = 60 },
                new TrackballChartData { xValue = new DateTime(2000, 9, 4), yValue = 20, yValue1 = 30, yValue2 = 55 },
                new TrackballChartData { xValue = new DateTime(2001, 2, 11), yValue = 25, yValue1 = 28, yValue2 = 48 },
                new TrackballChartData { xValue = new DateTime(2001, 9, 16), yValue = 21, yValue1 = 35, yValue2 = 57 },
                new TrackballChartData { xValue = new DateTime(2002, 2, 7), yValue = 13, yValue1 = 39, yValue2 = 62 },
                new TrackballChartData { xValue = new DateTime(2002, 9, 7), yValue = 18, yValue1 = 41, yValue2 = 64 },
                new TrackballChartData { xValue = new DateTime(2003, 2, 11), yValue = 24, yValue1 = 45, yValue2 = 57 },
                new TrackballChartData { xValue = new DateTime(2003, 9, 14), yValue = 23, yValue1 = 48, yValue2 = 53 },
                new TrackballChartData { xValue = new DateTime(2004, 2, 6), yValue = 19, yValue1 = 54, yValue2 = 63 },
                new TrackballChartData { xValue = new DateTime(2004, 9, 6), yValue = 31, yValue1 = 55, yValue2 = 50 },
                new TrackballChartData { xValue = new DateTime(2005, 2, 11), yValue = 39, yValue1 = 57, yValue2 = 66 },
                new TrackballChartData { xValue = new DateTime(2005, 9, 11), yValue = 50, yValue1 = 60, yValue2 = 65 },
                new TrackballChartData { xValue = new DateTime(2006, 2, 11), yValue = 24, yValue1 = 60, yValue2 = 79 },
            };
            ViewBag.dataSource = chartData;
            return View();
        }

        public class TrackballChartData
        {
            public DateTime xValue;
            public double yValue;
            public double yValue1;
            public double yValue2;
        }