CursorTool intersection with multiple series

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
asupriya
Advanced
Posts: 179
Joined: Mon Dec 01, 2008 12:00 am

CursorTool intersection with multiple series

Post by asupriya » Wed Feb 25, 2009 5:58 am

I have a chart that displays one or more fastline series. I also have a MarksTip tool and cursorTool attached to this chart. Now, i can move the vertical cursor and when mouse is on the series display, it shows the series values in tooltip.

I like to merge both the functionalities. When the cursor tool moves from point A to point B, i like to display the point values (the info. on the markstip tool's tooltip) as a note to the user such that if there are multiple series displayed, the note will list the series and the cursortool's intersecting point with each of the series. (Very similar to Interpolating line series demo shown in the TeeChart Demo app, but it is not the interpolation i am interested, but in actual point itself as the data is too close to worry about inerpolation).

Please suggest on how i can do it?

Thanks.

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Post by Yeray » Wed Feb 25, 2009 12:08 pm

Hi asupriya,

To customize your text box to show I recommend you to use an annotation tool instead of mark tips tool.

Here there is an example of how you could show some info of the nearest point when the cursor tool is moved. If I understand well, you are trying to do something similar to this.

Code: Select all

        public Form1()
        {
            InitializeComponent();

            InitializeChart();
        }

        Steema.TeeChart.Tools.CursorTool cursor1;
        Steema.TeeChart.Tools.NearestPoint nearestPoint1;
        Steema.TeeChart.Tools.Annotation annotation1;

        private void InitializeChart()
        {
            chartController1.Chart = tChart1;
            tChart1.Aspect.View3D = false;

            cursor1 = new Steema.TeeChart.Tools.CursorTool(tChart1.Chart);
            nearestPoint1 = new Steema.TeeChart.Tools.NearestPoint(tChart1.Chart);
            annotation1 = new Steema.TeeChart.Tools.Annotation(tChart1.Chart);

            Random rnd = new Random();
            double yval;

            for (int i = 0; i < 10; i++)
            {
                new Steema.TeeChart.Styles.Points(tChart1.Chart);

                for (int j = 0; j < 50; j++)
                {
                    yval = rnd.NextDouble()*1000;
                    tChart1[i].Add(yval,yval.ToString());
                }
                
            }

            cursor1.Change += new Steema.TeeChart.Tools.CursorChangeEventHandler(cursor1_Change);
        }

        void cursor1_Change(object sender, Steema.TeeChart.Tools.CursorChangeEventArgs e)
        {
            int[] NearestPoints;
            int SeriesIndex, ValueIndex;
            double Dist, tmp;
            Point P1, P2;

            NearestPoints = new int[tChart1.Series.Count];
            P1 = new Point(0, 0);
            P2 = new Point(tChart1.Width, tChart1.Height);
            SeriesIndex = 0;
            ValueIndex = 0;

            for (int i = 0; i < NearestPoints.Length; i++)
            {
                nearestPoint1.Series = tChart1[i];
                NearestPoints[i] = nearestPoint1.GetNearestPoint(new Point(e.x, e.y));
            }

            nearestPoint1.Series = null;

            Dist = Distance(P1, P2);

            for (int i = 0; i < NearestPoints.Length; i++)
            {
                P1.X = tChart1[i].CalcXPos(NearestPoints[i]);
                P1.Y = tChart1[i].CalcYPos(NearestPoints[i]);
                P2.X = e.x;
                P2.Y = e.y;
                tmp = Distance(P1, P2);

                if ((i == 0) || (tmp < Dist))
                {
                    Dist = tmp;
                    SeriesIndex = i;
                    ValueIndex = NearestPoints[i];
                }
            }

            annotation1.Text = "Series: " + SeriesIndex + " - ValueIndex: " + ValueIndex + " - Label: " + tChart1[SeriesIndex].Labels[ValueIndex];
            annotation1.Left = e.x + 5;
            annotation1.Top = e.y - 25;
        }

        private double Distance(Point Pt1, Point Pt2)
        {
            int dx, dy;

            dx = Pt1.X - Pt2.X;
            dy = Pt1.Y - Pt2.Y;

            return Math.Sqrt((dx * dx) + (dy * dy));
        } 
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

pw
Newbie
Newbie
Posts: 57
Joined: Fri Nov 15, 2002 12:00 am

Re: CursorTool intersection with multiple series

Post by pw » Wed Aug 05, 2009 1:18 am

Hi Yeray,

I tried the code you suggested but I got an error "Steema.TeeChart.Tools.NearestPoint' does not contain a definition for 'GetNearestPoint'" for the following part:

Code: Select all

NearestPoints[i] = nearestPoint1.GetNearestPoint(new Point(e.x, e.y));
Please advise, thanks.

asupriya
Advanced
Posts: 179
Joined: Mon Dec 01, 2008 12:00 am

Re: CursorTool intersection with multiple series

Post by asupriya » Wed Aug 05, 2009 4:30 am

I finally got some time to try the nearest point idea you gave; but the values of Y-axis obtained from the nearest value are not correct (even by visual inspection of chart). Please see the following code.

Please suggest a way that can provide exact values of cursorTool intersecting points with multiple series

Thanks

Code: Select all

Imports Steema.TeeChart.Styles
Public Class Form1
    Private WithEvents tchart1 As New Steema.TeeChart.TChart
    Private subChartTool1 As Steema.TeeChart.Tools.SubChartTool
    Private WithEvents cursorTool1 As Steema.TeeChart.Tools.CursorTool
    Dim line1 As Steema.TeeChart.Styles.FastLine
    Dim line2 As Steema.TeeChart.Styles.FastLine
    Dim line3 As Steema.TeeChart.Styles.FastLine

    Dim np1 As Steema.TeeChart.Tools.NearestPoint
    Dim xval As Double

    Dim startTime As System.DateTime = New System.DateTime(1970, 1, 1, 0, 0, 0, 0)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim rand As New Random
            np1 = New Steema.TeeChart.Tools.NearestPoint(tchart1.Chart)

            Controls.Add(tchart1)
            tchart1.Dock = DockStyle.Fill

            tchart1.Aspect.View3D = False
            tchart1.Legend.Visible = False
            line1 = New Steema.TeeChart.Styles.FastLine()
            line2 = New Steema.TeeChart.Styles.FastLine()
            line3 = New Steema.TeeChart.Styles.FastLine()

            line1.Color = Color.Blue
            line2.Color = Color.Green
            line3.Color = Color.Red

            line1.FillSampleValues(200)
            line2.FillSampleValues(200)
            line3.FillSampleValues(200)

            cursorTool1 = New Steema.TeeChart.Tools.CursorTool()
            AddHandler cursorTool1.Change, AddressOf cursorTool1_Change

            cursorTool1.FollowMouse = True
            cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical

            tchart1.Legend.Visible = True
            tchart1.Series.Add(line1)
            tchart1.Series.Add(line2)
            tchart1.Series.Add(line3)
            tchart1.Tools.Add(cursorTool1)
        Catch ex As Exception

        End Try
        
    End Sub
    Private Sub cursorTool1_Change(ByVal sender As Object, ByVal e As Steema.TeeChart.Tools.CursorChangeEventArgs)
        Try
            xval = e.XValue
            tchart1.Header.Text = ""
            Dim p1, p2 As Point
            p1 = New Point(0, 0)
            p2 = New Point(tchart1.Width, tchart1.Height)
            Dim NearestPoints As Integer()
            ReDim NearestPoints(tchart1.Series.Count - 1)

            For i As Integer = 0 To tchart1.Series.Count - 1
                If TypeOf tchart1.Series(i) Is Steema.TeeChart.Styles.FastLine Then
                    np1.Series = tchart1.Series(i)
                    NearestPoints(i) = np1.GetNearestPoint(New Point(e.x, e.y))
                End If
            Next
            'np1.Series = Nothing

            For i As Integer = 0 To tchart1.Series.Count - 1
                If TypeOf tchart1.Series(i) Is Steema.TeeChart.Styles.FastLine Then
                    tchart1.Header.Text += tchart1.Series(i).Title + ": Y(" + e.XValue.ToString("0.00") + ")= "
                    tchart1.Header.Text += tchart1.Series(i).YValues(NearestPoints(i)).ToString("0.00")
                End If
            Next
            'Exit Sub
        Catch ex As InvalidOperationException
            MsgBox(ex.Message)
        End Try

    End Sub
End Class

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: CursorTool intersection with multiple series

Post by Yeray » Wed Aug 05, 2009 9:53 am

Hi pw,
pw wrote:Hi Yeray,

I tried the code you suggested but I got an error "Steema.TeeChart.Tools.NearestPoint' does not contain a definition for 'GetNearestPoint'" for the following part:

Code: Select all

NearestPoints[i] = nearestPoint1.GetNearestPoint(new Point(e.x, e.y));
Please advise, thanks.
Could you please tell us what TeeChart version are you using?
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: CursorTool intersection with multiple series

Post by Yeray » Wed Aug 05, 2009 10:56 am

Hi asupriya,

If I understood well you are trying to do the same that is done in the interpolation example but changing the points that are drawn for annotations. Take a look at this example:

Code: Select all

public Form1()
        {
            InitializeComponent();

            InitializeChart();
        }

        Steema.TeeChart.Tools.CursorTool cursor1;

        private double InterpolateLineSeries(Custom series, int firstindex, int lastindex, double xvalue)
        {
            int index;
            for (index = firstindex; index <= lastindex; index++)
            {
                if (index == -1 || series.XValues.Value[index] > xvalue) break;
            }
            // safeguard
            if (index < 1) index = 1;
            else if (index >= series.Count) index = series.Count - 1;
            // y=(y2-y1)/(x2-x1)*(x-x1)+y1
            double dx = series.XValues[index] - series.XValues[index - 1];
            double dy = series.YValues[index] - series.YValues[index - 1];
            if (dx != 0.0) return dy * (xvalue - series.XValues[index - 1]) / dx + series.YValues[index - 1];
            else return 0.0;
        }

        private double InterpolateLineSeries(Custom series, double xvalue)
        {
            return InterpolateLineSeries(series, series.FirstVisibleIndex, series.LastVisibleIndex, xvalue);
        }


        private void InitializeChart()
        {
            chartController1.Chart = tChart1;
            tChart1.Aspect.View3D = false;

            for (int i = 0; i < 3; i++)
            {
                new Steema.TeeChart.Styles.Line(tChart1.Chart);
                tChart1[i].FillSampleValues(50);

                new Steema.TeeChart.Tools.Annotation(tChart1.Chart);
            }

            cursor1 = new Steema.TeeChart.Tools.CursorTool(tChart1.Chart);

            cursor1.Change += new Steema.TeeChart.Tools.CursorChangeEventHandler(cursor1_Change);
        }

        void cursor1_Change(object sender, Steema.TeeChart.Tools.CursorChangeEventArgs e)
        {
            int ys;
            double YValue;
            int xs = tChart1.Axes.Bottom.CalcXPosValue(e.XValue);

            for (int i = 0; i < tChart1.Series.Count; i++)
                if (tChart1.Series[i] is Custom)
                {
                    YValue = InterpolateLineSeries(tChart1.Series[i] as Steema.TeeChart.Styles.Custom, e.XValue);
                    ys = tChart1.Series[i].GetVertAxis.CalcYPosValue(YValue);
                    ((Steema.TeeChart.Tools.Annotation)tChart1.Tools[i]).Left = xs;
                    ((Steema.TeeChart.Tools.Annotation)tChart1.Tools[i]).Top = ys;
                    ((Steema.TeeChart.Tools.Annotation)tChart1.Tools[i]).Text = tChart1.Series[i].Title + ": Y(" + e.XValue.ToString("0.00") + ")= ";
                    ((Steema.TeeChart.Tools.Annotation)tChart1.Tools[i]).Text += YValue.ToString("0.00") + "\r\n";
                }

        }
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

asupriya
Advanced
Posts: 179
Joined: Mon Dec 01, 2008 12:00 am

Re: CursorTool intersection with multiple series

Post by asupriya » Wed Aug 05, 2009 1:53 pm

This is exactly what I needed. Thanks alot.

pw
Newbie
Newbie
Posts: 57
Joined: Fri Nov 15, 2002 12:00 am

Re: CursorTool intersection with multiple series

Post by pw » Wed Aug 05, 2009 9:54 pm

Yeray wrote:Hi pw,
Could you please tell us what TeeChart version are you using?
Hi Yeray,

I'm using version 3.5.3187.15584. I assume that I should update to a newer version?

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: CursorTool intersection with multiple series

Post by Sandra » Thu Aug 06, 2009 11:04 am

Hello pw,

I'm using version 3.5.3187.15584. I assume that I should update to a newer version?
Yes, Please update version with last version if 30 Jul 2009 and check that in this version your application works fine.

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply