[英]finding the value of the points in a chart
我在表格上制作了一張圖表。
我希望用戶通過單擊該部分來查看氣球中每個部件的value
, x_value
和y_value
。
當用戶移動鼠標時,氣球會消失。
我怎樣才能做到這一點?
你可以這樣做:
ToolTip tooltip = new ToolTip();
Point? clickPosition = null;
void chart1_MouseMove(object sender, MouseEventArgs e)
{
if (clickPosition.HasValue && e.Location != clickPosition)
{
tooltip.RemoveAll();
clickPosition = null;
}
}
void chart1_MouseClick(object sender, MouseEventArgs e)
{
var pos = e.Location;
clickPosition = pos;
var results = chart1.HitTest(pos.X, pos.Y, false,
ChartElementType.PlottingArea);
foreach (var result in results)
{
if (result.ChartElementType == ChartElementType.PlottingArea)
{
var xVal = result.ChartArea.AxisX.PixelPositionToValue(pos.X);
var yVal = result.ChartArea.AxisY.PixelPositionToValue(pos.Y);
tooltip.Show("X=" + xVal + ", Y=" + yVal,
this.chart1, e.Location.X,e.Location.Y - 15);
}
}
}
結果:
編輯:
要在鼠標移動時顯示工具提示,您可以使用以下代碼:
Point? prevPosition = null;
ToolTip tooltip = new ToolTip();
void chart1_MouseMove(object sender, MouseEventArgs e)
{
var pos = e.Location;
if (prevPosition.HasValue && pos == prevPosition.Value)
return;
tooltip.RemoveAll();
prevPosition = pos;
var results = chart1.HitTest(pos.X, pos.Y, false,
ChartElementType.PlottingArea);
foreach (var result in results)
{
if (result.ChartElementType == ChartElementType.PlottingArea)
{
var xVal = result.ChartArea.AxisX.PixelPositionToValue(pos.X);
var yVal = result.ChartArea.AxisY.PixelPositionToValue(pos.Y);
tooltip.Show("X=" + xVal + ", Y=" + yVal, this.chart1,
pos.X, pos.Y - 15);
}
}
}
請注意,這會在圖表的任何位置顯示工具提示。 如果只想在鼠標接近連續點時顯示它,可以使用mschart功能,例如:
yourSeries.ToolTip = "X=#VALX, Y=#VALY";
( 這里有更多例子)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.