How can I help you?
Add Crosshair
23 Mar 202624 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.
<script src="~/financial-data.js"></script>
@(Html.EJS().StockChart("container").Title("AAPL Stock Price").ExportType(new List<Object>() { })
.Series(sr =>
{
sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Add();
})
.Crosshair(cr=>cr.Enable(true))
.Render())
<script>
var data = window.chartData;
</script>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
public partial class StockChartController : Controller
{
public IActionResult Default()
{
return View();
}
}
}Tooltip for axis
Tooltip label for an axis can be enabled by using Enable property of CrosshairTooltip in the corresponding axis.
<script src="~/financial-data.js"></script>
@(Html.EJS().StockChart("container").Load("stockload").Title("AAPL Stock Price").ExportType(new List<Object>() { })
.PrimaryXAxis(xaxis =>xaxis.MajorTickLines(mg=>mg.Width(0).Color("transparent")).CrosshairTooltip(ct => ct.Enable(true)).LineStyle(ls=>ls.Color("transparent")
).ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).CrosshairTooltip(ct=>ct.Enable(true)))
.PrimaryYAxis(yaxis =>yaxis.MajorTickLines(mt=>mt.Color("transparent")).CrosshairTooltip(ct=> ct.Enable(true))
.Series(sr =>
{
sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Add();
})
.Render())
<script>
var data = window.chartData;
</script>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
public partial class StockChartController : Controller
{
public IActionResult Default()
{
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.
<script src="~/financial-data.js"></script>
@(Html.EJS().StockChart("container").Load("stockload").Title("AAPL Stock Price").ExportType(new List<Object>() { })
.Series(sr =>
{
sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Add();
})
.Render())
<script>
var data = window.chartData;
function stockload(args) {
args.stockChart.crosshair = { enable: true, line: { width: 2, color: 'green' } };
args.stockChart.primaryYAxis = {
lineStyle: { color: 'transparent' },
majorTickLines: { color: 'transparent', width: 0 },
crosshairTooltip: { enable: true, fill: 'green' }
};
args.stockChart.primaryXAxis = {
majorGridLines: { color: 'transparent' },
crosshairTooltip: { enable: true, fill: 'green' }
};
}
</script>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
public partial class StockChartController : Controller
{
public IActionResult Default()
{
return View();
}
}
}Crosshair label customization
The crosshairLabelRender event is triggered before each crosshair axis label is rendered in the stock chart. This event allows you to customize the appearance and content of the crosshair labels or prevent specific labels from being displayed.
The event arguments include:
-
text– The default text displayed in the crosshair label. You can modify this value to show 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 (HorizontalorVertical). -
textStyle– Defines the font properties of the label text such as color, size, and font family. -
fill– Specifies the background color of the crosshair label. -
cancel– Set this property to true to prevent the label from being rendered.
@using Syncfusion.EJ2.Charts
@Html.EJS().StockChart("container")
.PrimaryXAxis(px => px
.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime)
.MajorGridLines(mg => mg.Width(0))
.CrosshairTooltip(ct => ct.Enable(true))
)
.PrimaryYAxis(py => py
.LineStyle(ls => ls.Color("transparent"))
.MajorTickLines(mt => mt.Color("transparent").Height(0))
.CrosshairTooltip(ct => ct.Enable(true))
)
.ChartArea(area => area.Border(b => b.Width(0)))
.Crosshair(cr => cr.Enable(true).LineType(Syncfusion.EJ2.Charts.LineType.Both))
.CrosshairLabelRender("crosshairLabelRender")
.Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.SplineArea)
.XName("x")
.YName("high")
.Opacity(0.5)
.DataSource(ViewBag.dataSource)
.Add();
})
.Render()
<script>
function crosshairLabelRender(args) {
if (args.axisName === 'primaryXAxis') {
var date = new Date(args.value);
args.text = date.toLocaleDateString('en-IN', {
day: '2-digit',
month: 'short',
year: 'numeric'
});
}
if (args.axisName === 'primaryYAxis') {
var price = Number(args.value);
args.text = '₹' + price.toLocaleString('en-IN', {
maximumFractionDigits: 0
});
if (price > 310) {
args.textStyle = args.textStyle || {};
args.textStyle.color = '#d32f2f';
args.fill = '#ffebee';
}
}
}
</script>using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
public partial class StockChartController : Controller
{
public IActionResult CrosshairLabel()
{
List<StockData> chartData = new List<StockData>
{
new StockData { x = new DateTime(2012,4,2), open = 320.70, high = 324.07, low = 317.73, close = 323.78, volume = 45638000 },
new StockData { x = new DateTime(2012,4,3), open = 323.02, high = 324.29, low = 319.63, close = 321.63, volume = 40857000 },
new StockData { x = new DateTime(2012,4,4), open = 319.54, high = 319.81, low = 315.86, close = 317.89, volume = 32519000 },
new StockData { x = new DateTime(2012,4,5), open = 316.43, high = 318.53, low = 314.59, close = 316.47, volume = 46327000 },
new StockData { x = new DateTime(2012,4,9), open = 314.55, high = 317.98, low = 312.95, close = 315.73, volume = 43610000 },
new StockData { x = new DateTime(2012,4,10), open = 317.07, high = 317.56, low = 312.58, close = 313.74, volume = 49590000 },
new StockData { x = new DateTime(2012,4,11), open = 317.30, high = 318.31, low = 315.96, close = 318.29, volume = 43936000 },
new StockData { x = new DateTime(2012,4,12), open = 321.49, high = 326.89, low = 320.45, close = 325.83, volume = 11501600 },
new StockData { x = new DateTime(2012,4,13), open = 324.09, high = 324.81, low = 312.08, close = 312.61, volume = 16302200 },
new StockData { x = new DateTime(2012,4,16), open = 311.81, high = 312.21, low = 301.13, close = 303.33, volume = 11372800 }
};
ViewBag.dataSource = chartData;
return View();
}
public class StockData
{
public DateTime x;
public double open;
public double high;
public double low;
public double close;
public double volume;
}
}
}Snap to data
Enabling the SnapToData property in the crosshair aligns it with the nearest data point instead of following the exact mouse position.
<script src="~/financial-data.js"></script>
@(Html.EJS().StockChart("container").Load("stockload").Title("AAPL Stock Price").ExportType(new List<Object>() { })
.Series(sr =>
{
sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Add();
})
.Render())
<script>
var data = window.chartData;
function stockload(args) {
args.stockChart.crosshair = { enable: true, lineType: "Horizontal", snapToData: true };
args.stockChart.primaryYAxis = {
lineStyle: { color: 'transparent' },
majorTickLines: { color: 'transparent', width: 0 },
crosshairTooltip: { enable: true, fill: 'green' }
};
args.stockChart.primaryXAxis = {
majorGridLines: { color: 'transparent' },
crosshairTooltip: { enable: true, fill: 'green' }
};
}
</script>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
public partial class StockChartController : Controller
{
public IActionResult Default()
{
return View();
}
}
}Add 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.
@Html.EJS().Chart("container-vertical").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).XName("x").YName("yValue").Marker(ViewBag.marker)
.DataSource(ViewBag.dataSource ).Name("John").Width(2).Add();
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).XName("x").YName("yValue1").Marker(ViewBag.marker)
.DataSource(ViewBag.dataSource ).Name("Andrew").Width(2).Add();
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).XName("x").YName("yValue2").Marker(ViewBag.marker)
.DataSource(ViewBag.dataSource).Name("Thomas").Width(2).Add();})\
.PrimaryXAxis(px => px.EdgeLabelPlacement(Syncfusion.EJ2.Charts.EdgeLabelPlacement.Shift)
.MajorGridLines(ViewBag.majorGridLines).LineStyle(ViewBag.lineStyle)
.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime)
.Skeleton("y"))
.PrimaryYAxis(py => py.Title("Revenue")
.MajorTickLines(ViewBag.majorTickLines)
.LineStyle(ViewBag.lineStyle)
.Minimum(10)
.Maximum(80)
.LabelFormat("{value}M"))
.ChartArea(area => area.Border(ViewBag.ChartBorder))
.Tooltip(tl =>tl.Enable(true).Shared(true))
.Crosshair(cr => cr.Enable(true).LineType(Syncfusion.EJ2.Charts.LineType.Vertical))
.Title("Average Sales per Person").Render()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;
}