Page 1 of 1

Xvalue of cursor does not correspond

Posted: Tue Jul 20, 2010 9:03 am
by 13056035
Hi,

In my code the Xvalue of cursor does not correspond of the Label. It very strange.

Code: Select all


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.Windows.Threading;
using System.IO;

using Steema.TeeChart.WPF.Styles;
using Steema.TeeChart.WPF.Tools;
using Steema.TeeChart.WPF.Themes;
using Steema.TeeChart.WPF;
using Steema.TeeChart.WPF.Drawing;
using Steema.TeeChart;
using Steema.TeeChart.WPF.Editors;
using Steema.TeeChart.WPF.Export;
using Steema.TeeChart.WPF.Data;
using Steema.TeeChart.WPF.Functions;
using System.Drawing;
using System.Data;

namespace CSV_real_time
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    /// 
    public partial class Window1 : Window
    {
        // Declaration of global variable
        public Steema.TeeChart.WPF.Styles.FastLine fast1;
        //private System.Windows.Threading.DispatcherTimer TimerClock;

        // Declaration of global variables
        private Steema.TeeChart.WPF.Tools.CursorTool Cursor_left;
        private Steema.TeeChart.WPF.Tools.CursorTool Cursor_right;
        private double val_cursor_left, val_cursor_right;
        int x_left, x_right, y_left, y_right;
        // use the DispatcherTimer class for timer application
        private DispatcherTimer timer;

        public Window1()
        {
            InitializeComponent();        
            InitializeChart();
        }
           private void InitializeChart()
        {
            /*
            * local variables
            */
            double init_cursor_right = 0;
            double init_cursor_left = 100;

            // new instance of DispatcherTimer 
            timer = new DispatcherTimer();
            // set interval of timer --> 100 ms
            timer.Interval = TimeSpan.FromSeconds(0.100);
            // Each tick check datsource of my CSV file
            timer.Tick += timer1_Tick;
            //Timer timer = new System.Timers.Timer(1000);
            timer.Start();

            // disable of view in 3D
            this.tChart1.Aspect.View3D = false;
            //display or not the legend of chart
            tChart1.Legend.LegendStyle = Steema.TeeChart.WPF.LegendStyles.LastValues;
            tChart1.Legend.Visible = true;
            //tChart1.AutoRepaint = true;

            // title of chart
            this.tChart1.Header.Text = " CSV file in real time ";
            // new fastline of steema library
            //FastLine fast1;
            // add fastline of chart
            fast1 = new Steema.TeeChart.WPF.Styles.FastLine(tChart1.Chart);
            // Path of CSV file
            TextSource textsource1 = new TextSource("C:\\Users\\ljoly\\testone.csv");
            // Decimale Separator of each data
            textsource1.DecimalSeparator = ',';
            // Number of header ( here it is X and Y )
            textsource1.HeaderLines = 2;
            // Separator of each data
            textsource1.Separator = Convert.ToChar("\t");
            // link fastline and data on CSV file
            fast1.DataSource = textsource1;
            // X field data of CSV file
            textsource1.Fields.Add(0, "X");
            // Y field data of CSV file
            textsource1.Fields.Add(1, "Y");
            // Active fastlien serie which come from CSV file
            textsource1.Series.Active = true;
            // link serie of fastline of CSV file 
            textsource1.Series = fast1;
            // make a check of datasource
            fast1.CheckDataSource();        

            
            /*
            * Cursors part
            */

            // Left cursor 
            // Create a new cursor on chart
            Cursor_left = new Steema.TeeChart.WPF.Tools.CursorTool(tChart1.Chart);

            // this.tChart1.Tools.Add(fast1);

            // Use only the vertical cursor
            Cursor_left.Style = Steema.TeeChart.WPF.Tools.CursorToolStyles.Vertical;
            // Color of cursor
            Cursor_left.Pen.Color = System.Windows.Media.Colors.Blue;

            // this.Cursor_left.Pen.Color = Colors.Yellow;

            // Make something when cursor change of position
            Cursor_left.Change += new Steema.TeeChart.WPF.Tools.CursorChangeEventHandler(Cursor_left_Change);
            // Initial position of cursor left            
            Cursor_left.XValue = init_cursor_left;
            Cursor_left.YValue = init_cursor_left;
            Cursor_left.XValue = init_cursor_left;

            // Right cursor 
            // create a new cursor on chart
            Cursor_right = new Steema.TeeChart.WPF.Tools.CursorTool(tChart1.Chart);
            // use only the vertical cursor
            Cursor_right.Style = Steema.TeeChart.WPF.Tools.CursorToolStyles.Vertical;
            //color of cursor
            Cursor_right.Pen.Color = System.Windows.Media.Colors.Orange;
            // make something when cursor change of position
            Cursor_right.Change += new Steema.TeeChart.WPF.Tools.CursorChangeEventHandler(Cursor_right_Change);
            // initial position            
            Cursor_right.XValue = init_cursor_right;
            Cursor_right.YValue = init_cursor_right;
            Cursor_right.XValue = init_cursor_right;
            // Active function afterdraw the chart
            tChart1.AfterDraw += new Steema.TeeChart.WPF.PaintChartEventHandler(tChart1_AfterDraw);

        }

           //function of cursors

           void Cursor_left_Change(object sender, Steema.TeeChart.WPF.Tools.CursorChangeEventArgs e)
           {
               val_cursor_left = e.XValue;
               recalc();
           }
           void Cursor_right_Change(object sender, Steema.TeeChart.WPF.Tools.CursorChangeEventArgs e)
           {
               val_cursor_right = e.XValue;
               recalc();
           }
           private void recalc()
           {
               x_left = (int)tChart1.Axes.Bottom.CalcXPosValue(val_cursor_left);
               x_right = (int)tChart1.Axes.Bottom.CalcXPosValue(val_cursor_right);
               y_left = (int)tChart1[0].GetVertAxis.CalcYPosValue(InterpolateLineSeries(tChart1[0] as Steema.TeeChart.WPF.Styles.BaseLine, val_cursor_left));
               y_right = (int)tChart1[0].GetVertAxis.CalcYPosValue(InterpolateLineSeries(tChart1[0] as Steema.TeeChart.WPF.Styles.BaseLine, val_cursor_right));
           }
           void tChart1_AfterDraw(object sender, Steema.TeeChart.WPF.Drawing.Graphics3D g)
           {
               g.Brush.Visible = true;
               g.Brush.Solid = true;
               g.Brush.Color = Cursor_left.Pen.Color;
               g.Ellipse(new Rect(x_left - 4, y_left - 4, 8, 8));
               g.Brush.Color = Cursor_right.Pen.Color;
               g.Ellipse(new Rect(x_right - 4, y_right - 4, 8, 8));

               label_result_cursor_blue.Content=x_left;
               label_result_cursor_yellow.Content=x_right;
           }

           private double InterpolateLineSeries(Steema.TeeChart.WPF.Styles.BaseLine 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;

               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(Steema.TeeChart.WPF.Styles.BaseLine series, double xvalue)
           {
               return InterpolateLineSeries(series, series.FirstVisibleIndex, series.LastVisibleIndex, xvalue);
           }

           //end of function of cursor


           void timer1_Tick(object sender, EventArgs e)
           {

               fast1.CheckDataSource();
           }
    }
}
The aim of my project is to find the Xvalue of each cursor and make some calculation on this part.

Cheers,
ljoli

Re: Xvalue of cursor does not correspond

Posted: Tue Jul 20, 2010 2:06 pm
by yeray
Hi ljoli,

I've made some changes to your code to simplify it. As you add data with a timer, I've added a call to repaint the chart after adding the points. Then, you need to redo the cursor calculations and then draw the ellipses in the new positions:

Code: Select all

        public Window1()
        {
            InitializeComponent();
            InitializeChart();
        }

        public Steema.TeeChart.WPF.Styles.FastLine fast1;
        private Steema.TeeChart.WPF.Tools.CursorTool Cursor_left;
        private Steema.TeeChart.WPF.Tools.CursorTool Cursor_right;
        private System.Windows.Threading.DispatcherTimer timer;
        private Random r = new Random();

        private void InitializeChart()
        {
            double init_cursor_right = 5;
            double init_cursor_left = 10;

            timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer1_Tick;
            timer.Start();

            tChart1.Aspect.View3D = false;
            tChart1.Legend.LegendStyle = Steema.TeeChart.WPF.LegendStyles.LastValues;
            tChart1.Legend.Visible = true;

            fast1 = new Steema.TeeChart.WPF.Styles.FastLine(tChart1.Chart);
            fast1.FillSampleValues();

            Cursor_left = new Steema.TeeChart.WPF.Tools.CursorTool(tChart1.Chart);
            Cursor_left.Style = Steema.TeeChart.WPF.Tools.CursorToolStyles.Vertical;
            Cursor_left.Pen.Color = System.Windows.Media.Colors.Blue;
            Cursor_left.XValue = init_cursor_left;
            Cursor_left.YValue = init_cursor_left;
            Cursor_left.XValue = init_cursor_left;

            Cursor_right = new Steema.TeeChart.WPF.Tools.CursorTool(tChart1.Chart);
            Cursor_right.Style = Steema.TeeChart.WPF.Tools.CursorToolStyles.Vertical;
            Cursor_right.Pen.Color = System.Windows.Media.Colors.Orange;
            Cursor_right.XValue = init_cursor_right;
            Cursor_right.YValue = init_cursor_right;
            Cursor_right.XValue = init_cursor_right;

            tChart1.AfterDraw += new Steema.TeeChart.WPF.PaintChartEventHandler(tChart1_AfterDraw);
        }

        void tChart1_AfterDraw(object sender, Steema.TeeChart.WPF.Drawing.Graphics3D g)
        {
            int x_left = (int)tChart1.Axes.Bottom.CalcXPosValue(Cursor_left.XValue);
            int x_right = (int)tChart1.Axes.Bottom.CalcXPosValue(Cursor_right.XValue);
            int y_left = (int)tChart1[0].GetVertAxis.CalcYPosValue(InterpolateLineSeries(tChart1[0] as Steema.TeeChart.WPF.Styles.BaseLine, Cursor_left.XValue));
            int y_right = (int)tChart1[0].GetVertAxis.CalcYPosValue(InterpolateLineSeries(tChart1[0] as Steema.TeeChart.WPF.Styles.BaseLine, Cursor_right.XValue));

            g.Brush.Visible = true;
            g.Brush.Solid = true;
            g.Brush.Color = Cursor_left.Pen.Color;
            g.Ellipse(new Rect(x_left - 4, y_left - 4, 8, 8));
            g.Brush.Color = Cursor_right.Pen.Color;
            g.Ellipse(new Rect(x_right - 4, y_right - 4, 8, 8));

            tChart1.Header.Text = "x_left: " + x_left.ToString() + "\nx_right: " + x_right.ToString();
        }

        private double InterpolateLineSeries(Steema.TeeChart.WPF.Styles.BaseLine 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;

            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(Steema.TeeChart.WPF.Styles.BaseLine series, double xvalue)
        {
            return InterpolateLineSeries(series, series.FirstVisibleIndex, series.LastVisibleIndex, xvalue);
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            double tmp = (fast1.YValues.Maximum - fast1.YValues.Minimum) / 2;
            fast1.Add(fast1.YValues[fast1.Count-1] + r.NextDouble()*tmp - tmp/2);

            tChart1.Invalidate();
        }

Re: Xvalue of cursor does not correspond

Posted: Tue Jul 20, 2010 2:33 pm
by 13056035
hello, Yeray,

The code is a little more simple but there is still the same problem.

When I put the cursor for example at x=5 => cursor_left=126 !?

Re: Xvalue of cursor does not correspond

Posted: Tue Jul 20, 2010 3:44 pm
by yeray
Hi ljoli,

Note that in the header (or your labels) we are showing the x_left integer and that's the value in pixels we calculated for the given Cursor_left.XValue, that is the cursor position in the bottom axis scale.

Code: Select all

tChart1.Header.Text = "LeftCursor XValue: " + Cursor_left.XValue.ToString() + ", in pixels: " + x_left.ToString() +
                                  "\nRightCursor XValue: " + Cursor_right.XValue.ToString() + ", in pixels: " + x_right.ToString();
We calculated the position of the cursors in pixels to draw ellipses manually at the same place.

Re: Xvalue of cursor does not correspond

Posted: Wed Jul 21, 2010 8:20 am
by 13056035
Ok I saw now the difference between the two value.
It's clear now. :) The value what I want it's the cursor.Xvalue

Thank for all this information :wink:

Re: Xvalue of cursor does not correspond

Posted: Wed Jul 21, 2010 8:52 am
by yeray
Hi ljoli,

I'm glad to see you satisfied! :D