Posted 12 April 2022, 1:29 am EST
Hi Chris,
Thank you for sharing the code snippets.
You need to manually identify ChartGroup based on the distance from the nearest point. I have written an extension method for Hit testing C1Chart as follows:
public static ChartHitTestInfo HitTest(this C1Chart chart, Point mouseCoordinates, int maxDistance = 3)
{
var hitTestInfo = new ChartHitTestInfo();
int seriesIndex = -1, pointIndex = -1, distance = -1;
// Hit testing with group 1
var group0 = chart.ChartGroups.Group0;
if (group0.CoordToDataIndex(mouseCoordinates.X, mouseCoordinates.Y, CoordinateFocusEnum.XandYCoord, ref seriesIndex, ref pointIndex, ref distance))
{
hitTestInfo.SeriesIndex = seriesIndex;
hitTestInfo.PointIndex = pointIndex;
hitTestInfo.Distance = distance;
hitTestInfo.Group = group0;
}
seriesIndex = -1;
pointIndex = -1;
distance = -1;
// Hit testing with group 2
var group1 = chart.ChartGroups.Group1;
if (group1.CoordToDataIndex(mouseCoordinates.X, mouseCoordinates.Y, CoordinateFocusEnum.XandYCoord, ref seriesIndex, ref pointIndex, ref distance)
&& distance < hitTestInfo.Distance)
{
// Group2 is hit if its series dataitem distance is nearest to mouse coordinates
hitTestInfo.SeriesIndex = seriesIndex;
hitTestInfo.PointIndex = pointIndex;
hitTestInfo.Distance = distance;
hitTestInfo.Group = group1;
}
// return null if any of the below criteria matches
if (hitTestInfo.Group == null || hitTestInfo.SeriesIndex == -1 ||
hitTestInfo.Distance < 0 || hitTestInfo.Distance > maxDistance || hitTestInfo.PointIndex == -1)
return null;
// hit test is valid
return hitTestInfo;
}
And then you can use it inside C1Chart’s MouseDown event as follows:
private void OnChartMouseDown(object sender, MouseEventArgs e)
{
var hitTest = c1Chart1.HitTest(e.Location);
if(hitTest != null)
MessageBox.Show($"Series Index: {hitTest.SeriesIndex}\nPoint Index: {hitTest.PointIndex}\nDistance: {hitTest.Distance}\nChart Group: {hitTest.Group.Name}");
}
And for getting the series index, you need to use ChartDataSeriesCollection’s IndexOf method as follows:
var series = c1Chart1.ChartGroups.Group0.ChartData.SeriesList.AddNewSeries();
int seriesIndex = c1Chart1.ChartGroups.Group0.ChartData.SeriesList.IndexOf(series);
Please refer to the same from the attached sample. (see C1ChartSample.zip)
Additionally, I would also like to inform you that C1Chart is a legacy control now and hence you won’t receive any fixes/updates in future. So, I would recommend you to take a look at our latest charting control i.e. FlexChart which is more feature-rich, highly-customizable & has top-notch performance as compared to C1Chart.
https://www.grapecity.com/componentone/docs/win/online-flexchart/key-features.html
Best Regards,
Kartik
C1ChartSample.zip