Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Get yaxis DateTime value to a str from OnPlotAreaMouseMove?

I'm using a scattergraph and the yaxis is DateTime with current time and a span of 5 minutes or so.

How do I get the DateTime value of the axis and display it in a string.

I tried MapDataPoint but it only returns a PointF which a 4byte float is not big enough to cover the time value.

ScottyO
0 Kudos
Message 1 of 8
(4,258 Views)
Are you trying to represent the minimum and/or the maximum values of the yaxis range as a string? If so, you can get the DateTime representation of the minimum and/or maximum values from the Range class and call ToString on the returned DateTime object. For example, the following code shows how to convert the minimum value of the yaxis range to a string:


private string ConvertDateTimeMinimumToString()
{
DateTime minimum = yaxis.Range.MinimumDateTime;
return minimum.ToString(yaxis.MajorDivisions.LabelFormat.Format);
}


Let me know if I understood your question correctly or if you were trying to do something else.
Abhishek Ghuwalewala | Measurement Studio | National Instruments
0 Kudos
Message 2 of 8
(4,247 Views)
Thanks for the reply.

I want to display the time when the user moves the mouse in the plotarea.

My yaxis goes from say, 15:10:00 to 15:12:00. If the user puts the mouse on say, 15:11:35, I want to display that from my OnPlotAreaMouseMove which gives me e.Y as a mouse value.

private void OnPlotAreaMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
DateTime dt; // somehow put the actual value in here so I can display it below.
string str;
str.Format("{0} Time", dt);
_CursorToolTip.SetToolTip(_NIGraph,str);

}

Scott

Message Edited by scottyoo on 03-03-2005 06:40 PM

0 Kudos
Message 3 of 8
(4,247 Views)
If you want to format the value to a string when the mouse is over a point, you can get the point via the graph's GetPlotAt method, then format the Y value with your configured label format. For example, assuming that you have a form with a graph and a label and all controls and sub-objects have their default names, add an event to the graph's PlotAreaMouseMove event and the following code will update the label with the formatted DateTime Y value:


private void OnPlotAreaMouseMove(object sender, MouseEventArgs e)
{
double xData, yData;
XYPlot plot = scatterGraph1.GetPlotAt(e.X, e.Y, out xData, out yData);

if (plot == null)
label1.Text = String.Empty;
else
label1.Text = yAxis1.MajorDivisions.LabelFormat.FormatDouble(yData);
}


If you want to format the string regardless of where in the plot area the mouse is, it gets a little trickier. This code will do it with the same UI configuration as specified above:


private void OnPlotAreaMouseMove(object sender, MouseEventArgs e)
{
Rectangle bounds = scatterGraph1.PlotAreaBounds;
float factor = ((float)(bounds.Bottom - e.Y)) / ((float)(bounds.Bottom - bounds.Top));

Range range = yAxis1.Range;
double yValue = (range.Interval * factor) + range.Minimum;

label1.Text = yAxis1.MajorDivisions.LabelFormat.FormatDouble(yValue);
}


This looks easy enough, but note that this only works with a linear, non-inverted scale. It gets more complicated if you have to handle things like logarithmic scales. Also note that you're going from screen coordinates (ints) to data coordinates (doubles), so there is a loss of precision. This loss of precision is not an issue in the previous solution above since the returned data points are actually from the plot and not simply inverse mapped from the screen coordinates.

- Elton
0 Kudos
Message 4 of 8
(4,239 Views)
Thanks a million Elton.

while I was waiting, I did do exactly what you did in your second example with the exception of:

label1.Text = yAxis1.MajorDivisions.LabelFormat.FormatDouble(yValue);

That's a nice utility I did not know about!

I thought maybe there might have been a simpler way.

Thanks again,
ScottyO
0 Kudos
Message 5 of 8
(4,233 Views)
Thanks a million Elton.

while I was waiting, I did do exactly what you did in your second example with the exception of:

label1.Text = yAxis1.MajorDivisions.LabelFormat.FormatDouble(yValue);

That's a nice utility I did not know about!

I thought maybe there might have been a simpler way.

Thanks again,
ScottyO

P.S. Just figured out how to reply to a reply! 🙂
0 Kudos
Message 6 of 8
(4,230 Views)
range.Interval

this property would be nice 🙂 what is your MS version ?
0 Kudos
Message 7 of 8
(4,220 Views)
Oops ... sorry. I have a development build of the next version of Measurement Studio on my machine. I usually write examples for forum posts on a test machine that has Measurement Studio 7.1 to avoid mistakes like this, but this was a simple example and I thought we added the Interval property in Measurement Studio 7.1. Anyway, there's the first sneak peek at the next version of Measurement Studio - Range will have an Interval property ;-).

- Elton
0 Kudos
Message 8 of 8
(4,208 Views)