Can I create a horizontal LineCursor

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Arction_LasseP

GeoffDavis
Posts: 5
Joined: Fri Nov 20, 2020 12:12 am

Can I create a horizontal LineCursor

Post by GeoffDavis » Mon Nov 23, 2020 4:24 am

The "Cursor Tracking Chart" example in the Lightning Chart .Net INteractive examples offers a very nice cursor option

The example operates in a vertical fashion i.e
  • The LineSeriesCursor appears vertically
    The LineSeriesCursor will only move in horizontal direction across the X axis
    The initialize option only presents X axis options i.e. LineSeriesCursor cursor = new LineSeriesCursor(_chartXX.ViewXY, _chartXX.ViewXY.XAxes[0]);
    The cursor only permits X Axis starting points i.e. cursor.ValueAtXAxis = 10;
Is there anyway we can have a horizontal cursor that moves horizontally, i.e. up and down, the Y axis in the same was as the Verical cursor

Thank you

Arction_LasseP
Posts: 105
Joined: Wed Mar 27, 2019 1:05 pm

Re: Can I create a horizontal LineCursor

Post by Arction_LasseP » Mon Nov 23, 2020 8:20 am

Hello Geoff,

Currently there isn't a Horizontal Line Cursor with the same properties and functionalities as in Vertical Cursor. Though LineSeriesCursor's style can be changed to HairCrossTracking which draws both horizontal and vertical lines. However, the horizontal line cannot be dragged.

There are still many ways to draw a movable horizontal line across the chart. A line collection, another PointLineSeries or even an X-axis could be used. However, the best option most likely is ConstantLine, which draws a line at given Y-value. The line can also be dragged up or down. The following draws a red line at Y-value 5:

Code: Select all

            ConstantLine cl = new ConstantLine(_chart.ViewXY, _chart.ViewXY.XAxes[0], _chart.ViewXY.YAxes[0]);
            cl.LineStyle.Width = 2;
            cl.LineStyle.Color = Color.Red;
            cl.Value = 5;
            _chart.ViewXY.ConstantLines.Add(cl);
The only downside of this is that ConstantLine doesn't have series tracking options (SnapToPoints, solving intersection points etc.). If you need to for example solve intersection points between the ConstantLine and some line series, you need solve them in code. This can be done with help of ConstantLine's ValueChanged -event (triggers every time when Y-value has been changed) and LineIntersection method in MathRoutines class. The following adds a marker at every intersection point:

Code: Select all

cl.ValueChanged += Cl_ValueChanged;

private void Cl_ValueChanged(object sender, ValueChangedEventArgs e)
{
    _chart.BeginUpdate();
    PointLineSeries pls = _chart.ViewXY.PointLineSeries[0];
    pls.SeriesEventMarkers.Clear();
    for (int i = 0; i < pls.PointCount - 1; i++)
    {
        PointFloat inter;
        if (MathRoutines.LineIntersection(new PointFloat((float)_chart.ViewXY.XAxes[0].Minimum, (float)e.NewValue), new PointFloat((float)_chart.ViewXY.XAxes[0].Maximum, (float)e.NewValue),
            new PointFloat((float)pls.Points[i].X, (float)pls.Points[i].Y), new PointFloat((float)pls.Points[i + 1].X, (float)pls.Points[i + 1].Y), out inter))
        {
            SeriesEventMarker marker = new SeriesEventMarker();
            marker.AllowUserInteraction = false;
            marker.HorizontalPosition = SeriesEventMarkerHorizontalPosition.AtXValue;
            marker.VerticalPosition = SeriesEventMarkerVerticalPosition.AtYValue;
            marker.XValue = inter.X;
            marker.YValue = inter.Y;
            marker.Label.Text = "X: " + inter.X.ToString("0.0");
            pls.SeriesEventMarkers.Add(marker);
        }
    }
    _chart.EndUpdate();
}
Hope this helps.
Best regards,
Lasse

GeoffDavis
Posts: 5
Joined: Fri Nov 20, 2020 12:12 am

Re: Can I create a horizontal LineCursor

Post by GeoffDavis » Mon Nov 23, 2020 10:22 am

Thanks Lasse - I will give those options a try