Specify the coordinates of the timeline at the introduction
-
There's a schedule for a function like chart. We need to write a code to make a mouse at the point of schedule, show the location.
Implementation of the timetable:
int x = 0; for (double i = -Period; i <= Period; i += step) { chart.Visible = true; chart.Series[0].ChartType = SeriesChartType.Spline; mas_MyGraphic5[x] = FuncForGraphic5(i); chart5.Series[0].Points.AddXY(i, FuncForGraphic5(i)); x++; }
-
I suggest that the following value should be established.
Series.ToolTip
:chart.Series[0].ToolTip = "X = #VALX, Y = #VALY";
In this case, no additional code will have to be written.
We can still sign for the event. https://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chart.gettooltiptext.aspx and using a class-transmitted copy to the processor https://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.tooltipeventargs.aspx I'm gonna figure out what to do with ToolTip.
chart.GetToolTipText += chart_GetToolTipText;
Processor:
private void chart_GetToolTipText(object sender, ToolTipEventArgs e) { // Если текст в подсказке уже есть, то ничего не меняем. if (!String.IsNullOrWhiteSpace(e.Text)) return;
Console.WriteLine(e.HitTestResult.ChartElementType); switch (e.HitTestResult.ChartElementType) { case ChartElementType.DataPoint: case ChartElementType.DataPointLabel: case ChartElementType.Gridlines: case ChartElementType.Axis: case ChartElementType.TickMarks: case ChartElementType.PlottingArea: // Первый ChartArea var area = chart.ChartAreas[0]; // Его относительные координаты (в процентах от размеров Chart) var areaPosition = area.Position; // Переводим в абсолютные var areaRect = new RectangleF(areaPosition.X * chart.Width / 100, areaPosition.Y * chart.Height / 100, areaPosition.Width * chart.Width / 100, areaPosition.Height * chart.Height / 100); // Область построения (в процентах от размеров area) var innerPlot = area.InnerPlotPosition; double x = area.AxisX.Minimum + (area.AxisX.Maximum - area.AxisX.Minimum) * (e.X - areaRect.Left - innerPlot.X * areaRect.Width / 100) / (innerPlot.Width * areaRect.Width / 100); double y = area.AxisY.Maximum - (area.AxisY.Maximum - area.AxisY.Minimum) * (e.Y - areaRect.Top - innerPlot.Y * areaRect.Height / 100) / (innerPlot.Height * areaRect.Height / 100); Console.WriteLine("{0:F2} {1:F2}", x, y); e.Text = String.Format("{0:F2} {1:F2}", x, y); break; }
}
Despite the fact that the coordinates are computed correctly and sent to
e.Text
I don't always have ToolTip. That's why I'm printing them to the console for loyalty. Instead, you can display them in some control.