C#图表:关于图表区域的多个问题。Cusor和读取X/Y值
我有一个在服务器上运行的程序,每5秒联系一次在线API获取数据。数据成对保存在数据库中。一对由UNIX时间戳和双精度值组成。 在我的C#应用程序中,我想同时显示两个API的值。X轴包含时间戳,Y轴包含双值保证两个系列具有相同的X值-只有Y值不同。看起来是这样的:C#图表:关于图表区域的多个问题。Cusor和读取X/Y值,c#,charts,C#,Charts,我有一个在服务器上运行的程序,每5秒联系一次在线API获取数据。数据成对保存在数据库中。一对由UNIX时间戳和双精度值组成。 在我的C#应用程序中,我想同时显示两个API的值。X轴包含时间戳,Y轴包含双值保证两个系列具有相同的X值-只有Y值不同。看起来是这样的: private void chart1_MouseMove(object sender, MouseEventArgs e) { Point p = new Point(e.X, e.Y);
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(p, true);
DateTime t = DateTime.FromOADate(chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X));
graph_time.Text = t.ToLongTimeString();
}
您可以看到,我已经将ChartArea[0].CursorX添加到图表中(绿线)。它得到如下更新:
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(p, true);
DateTime t = DateTime.FromOADate(chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X));
graph_time.Text = t.ToLongTimeString();
}
因此,绿色光标X被设置为鼠标的X位置。但是,我将其间隔设置为5秒
- 第一个问题
DateTime t = DateTime.FromOADate(chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X));
graph_time.Text = t.ToLongTimeString();
问题是,它显示的是实际光标位置的X值(我正在隐藏该光标),而不是绿线。如何获得绿线X轴上的值
- 第二个问题
- 第三个也是最后一个问题
谢谢 让我试着不用测试就回答 1-用于获取光标所在的x值 2-找到光标前后的两个数据点,并插值其Y值。您可以使用LINQ查找这两点。。有关如何在“点”集合中搜索的示例,请参见
3-见2!选择第一个、第二个或最近的点;您需要为此将间隔设置为0。让我在不进行测试的情况下尝试回答..: 1-用于获取光标所在的x值 2-找到光标前后的两个数据点,并插值其Y值。您可以使用LINQ查找这两点。。有关如何在“点”集合中搜索的示例,请参见
3-见2!选择第一个、第二个或最近的点;为此,您需要将间隔设置为0。我昨天晚上尝试了一下,我的问题的解决方法实际上比我想象的要简单得多。它的工作原理与TaW的答案非常相似
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X < 0 || e.Y < 0 || e.Location == prevPos)
return;
prevPos = e.Location;
if (this.graphShowingData == false)
return;
Point p = new Point(e.X, e.Y);
double searchVal = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
foreach (DataPoint dp in chart1.Series["Series1"].Points)
{
if(dp.XValue >= searchVal)
{
chart1.ChartAreas[0].CursorX.SetCursorPosition(dp.XValue);
foreach(double yD in dp.YValues)
{
val_series1.Text = Math.Round(yD, 4).ToString();
}
break;
}
}
foreach (DataPoint dp in chart1.Series["Series2"].Points)
{
if (dp.XValue >= searchVal)
{
foreach (double yD in dp.YValues)
{
val_series2.Text = Math.Round(yD, 4).ToString();
}
break;
}
}
DateTime t = DateTime.FromOADate(chart1.ChartAreas[0].CursorX.Position);
graph_time.Text = t.ToLongTimeString();
}
private void chart1\u MouseMove(对象发送方,MouseEventArgs e)
{
如果(e.X<0 | | e.Y<0 | | e.Location==prevPos)
返回;
prevPos=e.位置;
if(this.graphShowingData==false)
返回;
点p=新点(e.X,e.Y);
double searchVal=chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
foreach(图表1.系列[“系列1”]中的数据点dp.点)
{
如果(dp.XValue>=searchVal)
{
chart1.ChartAreas[0].CursorX.SetCursorPosition(dp.XValue);
foreach(dp.Y值中的双码)
{
val_series1.Text=Math.Round(yD,4).ToString();
}
打破
}
}
foreach(图表1.系列[“系列2”]中的数据点dp.点)
{
如果(dp.XValue>=searchVal)
{
foreach(dp.Y值中的双码)
{
val_series2.Text=Math.Round(yD,4).ToString();
}
打破
}
}
DateTime t=DateTime.FromOADate(chart1.ChartAreas[0].CursorX.Position);
graph_time.Text=t.ToLongTimeString();
}
这段代码解决了我上面的三个问题。我昨天晚上试了一下,我的问题的解决方法实际上比我想象的要简单得多。它的工作原理与TaW的答案非常相似
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X < 0 || e.Y < 0 || e.Location == prevPos)
return;
prevPos = e.Location;
if (this.graphShowingData == false)
return;
Point p = new Point(e.X, e.Y);
double searchVal = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
foreach (DataPoint dp in chart1.Series["Series1"].Points)
{
if(dp.XValue >= searchVal)
{
chart1.ChartAreas[0].CursorX.SetCursorPosition(dp.XValue);
foreach(double yD in dp.YValues)
{
val_series1.Text = Math.Round(yD, 4).ToString();
}
break;
}
}
foreach (DataPoint dp in chart1.Series["Series2"].Points)
{
if (dp.XValue >= searchVal)
{
foreach (double yD in dp.YValues)
{
val_series2.Text = Math.Round(yD, 4).ToString();
}
break;
}
}
DateTime t = DateTime.FromOADate(chart1.ChartAreas[0].CursorX.Position);
graph_time.Text = t.ToLongTimeString();
}
private void chart1\u MouseMove(对象发送方,MouseEventArgs e)
{
如果(e.X<0 | | e.Y<0 | | e.Location==prevPos)
返回;
prevPos=e.位置;
if(this.graphShowingData==false)
返回;
点p=新点(e.X,e.Y);
double searchVal=chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
foreach(图表1.系列[“系列1”]中的数据点dp.点)
{
如果(dp.XValue>=searchVal)
{
chart1.ChartAreas[0].CursorX.SetCursorPosition(dp.XValue);
foreach(dp.Y值中的双码)
{
val_series1.Text=Math.Round(yD,4).ToString();
}
打破
}
}
foreach(图表1.系列[“系列2”]中的数据点dp.点)
{
如果(dp.XValue>=searchVal)
{
foreach(双码dp)。