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();
}
}
}
Cheers,
ljoli