样条图,如何获取点击的项目? [英] Spline chart, how to get the clicked item?
问题描述
我在c#上使用白图。
我想知道一种获得iten(样条曲线)点击图表来做类似更改颜色或隐藏的方法。
使用 HitTest
检查点击的项目:
private void chart1_MouseDown(object sender,MouseEventArgs e)
{
HitTestResult result = chart1.HitTest(eX,eY);
//现在做你想要的数据..
if(result.Series!= null&& result.PointIndex> = 0)
Text =X = + result.Series.Points [result.PointIndex] .XValue +
Y =+ result.Series.Points [result.PointIndex] .YValues [0];
}
为帮助用户指定可能的匹配:
private void chart1_MouseMove(object sender,MouseEventArgs e)
{
HitTestResult result = chart1.HitTest(eX,eY);
Cursor = result.Series!= null? Cursors.Hand:Cursors.Default;
}
通过使样条线更大一些来帮助用户: p>
eachOfYourSeries.BorderWidth = 4;
当然,您显示的图表使得受控点击无法放大。
现在如果你想隐藏一个系列
或者改变它的 Color
HitTestResult
足够好:
result.Series.Enabled = flase ;
或
result.Series.Color = Color.Fuchsia;
但是要访问点击的值,它可能不是..:
请注意,特别是 Spline
或 Line
几个 DataPoints
创建自己的行;所以来自 HitTest
的结果根本不够; 会告诉您哪些系列被点击,以及最接近的 DataPoint
,但要获得正确的值,您仍可能需要将像素转换为数据值。
而不是 HitTest
结果,你可能更喜欢使用方便的函数 PixelPositionToValue
:
float px =(float)yourChartArea.AxisX.PixelPositionToValue(eX);
这将获得实际 DataPoints
。当然你可以对Y值做同样的事情:
float py =(float)yourChartArea.AxisY.PixelPositionToValue );
注意,您只能在图表不忙时使用这些功能确定布局;正式地,这只是在任何三个 Paint
事件。但我发现在实践中一个鼠标事件也很好。当图表未准备就绪时,您将获得一个空值。
I'm working whit charts on c#.
I would like to know a way of get the iten (the spline) clicked on the chart to do something like change color or hide.
You can check the clicked item using HitTest
, as suggested..:
private void chart1_MouseDown(object sender, MouseEventArgs e)
{
HitTestResult result = chart1.HitTest(e.X, e.Y);
// now do what you want with the data..:
if (result.Series != null && result.PointIndex >= 0)
Text = "X = " + result.Series.Points[result.PointIndex].XValue +
" Y = " + result.Series.Points[result.PointIndex].YValues[0];
}
To help the user you can indicate a possible hit:
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
HitTestResult result = chart1.HitTest(e.X, e.Y);
Cursor = result.Series != null ? Cursors.Hand : Cursors.Default;
}
Do your users a favor by making the spline lines a little larger:
eachOfYourSeries.BorderWidth = 4;
Of course the chart you show makes a controlled clicking impossible without zooming in first..
Now if all you want is hiding a Series
or changing its Color
the HitTestResult
is good enough:
result.Series.Enabled = flase;
or
result.Series.Color = Color.Fuchsia;
But to access the clicked values, it may not be..:
Note that especially a Spline
or a Line
graph, need only very few DataPoints
to create their lines; so the result from HitTest
simply is not enough; it will tell you which series was clicked and also the closest DataPoint
but to get at the correct values you still may need to convert the pixels to data values.
So instead of the HitTest
result you may prefer to use the handy function PixelPositionToValue
:
float px = (float)yourChartArea.AxisX.PixelPositionToValue(e.X);
This will get all the values between the actual DataPoints
. Of course you can do the same for the Y-Values:
float py = (float)yourChartArea.AxisY.PixelPositionToValue(e.Y);
Note that you can only use these function when the chart is not busy with determining the layout; officially this is only during any of the three Paint
events. But I found that in practice a mouse event is also fine. You will get a null value when the chart is not ready..
这篇关于样条图,如何获取点击的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!