使用MouseEvents更改C#Winforms中的图表系列外观
我的表格中有一个动态创建的点图,因此图表上系列的编号/名称可以在每次运行表格时更改。我希望能够使用图例突出显示一个系列,以便更好地查看仅属于该系列的点。我已经能够使用较厚的标记边框高亮显示该系列,但我无法使其在另一次单击或移动图例项目时删除该高亮显示。以下是我如何突出选定系列中的要点:使用MouseEvents更改C#Winforms中的图表系列外观,c#,visual-studio,winforms,charts,legend,C#,Visual Studio,Winforms,Charts,Legend,我的表格中有一个动态创建的点图,因此图表上系列的编号/名称可以在每次运行表格时更改。我希望能够使用图例突出显示一个系列,以便更好地查看仅属于该系列的点。我已经能够使用较厚的标记边框高亮显示该系列,但我无法使其在另一次单击或移动图例项目时删除该高亮显示。以下是我如何突出选定系列中的要点: private void plot.MouseMove (object sender, MouseEventArgs e) {
private void plot.MouseMove (object sender, MouseEventArgs e)
{
HitTestResult result = plot.HitTest(e.X, e.Y);
if (result != null && result.Object != null)
{
if (result.ChartElementType == ChartElementType.LegendItem)
{
string selseries = result.Series.Name;
plot.Series[selseries].MarkerBorderWidth = 3;
plot.Series[selseries].MarkerSize = 11;
plot.Series[selseries].MarkerBorderColor = Color.Black;
}
}
};
高亮显示后,如何取消选择?与其他系列相比,有没有更好的方法来选择性地突出一个系列?理想情况下,我希望将所选系列以外的所有系列更改为较暗的颜色,从而突出显示相关系列,但我会满足于能够选择/取消选择相关系列。要“记住”突出显示的系列,您需要在此方法之外存储一个引用,以便以后可以访问它。然后,无论何时要清除更改,只要查找先前保存的内容并重置属性即可。下面是一些示例代码:
string selectedSeries = ""; // store a class-scoped reference
private void plot.MouseMove(object sender, MouseEventArgs e)
{
HitTestResult result = plot.HitTest(e.X, e.Y);
if (result != null && result.Object != null && result.ChartElementType == ChartElementType.LegendItem)
{
string selseries = result.Series.Name;
// store a reference to what we are changing:
selectedSeries = selseries;
plot.Series[selseries].MarkerBorderWidth = 3;
plot.Series[selseries].MarkerSize = 11;
plot.Series[selseries].MarkerBorderColor = Color.Black;
}
else
{
// if we clear the selection here, then we are clearing the selection
// whenever we move off the legend item... that was one of your use cases
// you could also do something similar in a mouse click event to cover your other use case.
if (selectedSeries != "")
{
plot.Series[selectedSeries].MarkerBorderWidth = 1; // set these to default value
plot.Series[selectedseries].MarkerSize = 5;
plot.Series[selectedseries].MarkerBorderColor = Color.Green;
selectedSeries = ""; // reset selection
}
}
}
此
plot.MouseMove
compile是否编译?如果要还原更改,需要a)记住或b)通过查看所有系列的当前值找出更改的系列。你需要知道旧的价值观。两者都不难做到。除此之外,你做得很好。。最好编写一个函数void-hilightSeries(Series s,bool-hilite)
来打开/关闭它。string-selseries
在您的场景中是不必要的。最好确保selectSeries键在plot.Series集合中有效。@LarsTech-这是真的。我试图对现有代码进行最小的更改,但是可以直接使用selectedSeries
代替selseries
,并且一些额外的验证将是明智的。谢谢大家!我将把答案标记为正确,就像我做了一些非常相似的事情一样。我读了TaW的回复,并且已经在努力寻找改变了的系列。我不确定出了什么问题,但现在在我调出selseries
的地方,我添加了一个基于所选序列标记的if/else检查。我还将其设置为MouseClick事件,而不是MouseMove,因为我认为图例项目之间的小空间导致它在不需要时选择/取消选择。