c# - 在 C# Winforms 中使用 MouseEvents 更改图表系列的外观
问题描述
我的表格中有一个点图,它是动态创建的,因此图表上系列的编号/名称可以在表格的每次运行中发生变化。我希望能够使用图例突出显示一个系列,以便更好地查看仅属于该系列的点。我已经能够使用较厚的标记边框突出显示该系列,但我无法做到这一点,以便在再次单击或移出图例项时删除此突出显示。以下是我如何突出选定系列中的要点:
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
}
}
}
推荐阅读
- python - AttributeError:“应用程序”对象没有属性“tunes_list”
- flutter - 参数类型“String”不能分配给参数类型“Uri”。请问有人可以帮我吗?
- java - 调用 @AfterThrowing 处理程序方法后如何避免记录异常
- javascript - 如何使用浏览器的主水平滚动条在 DIV 部分中滚动
- c# - AutoMapper:使用多个 IValueResolver 时 Resolve() 的调用顺序
- python - Apache Airflow - 连接到 AWS S3 错误
- java - 我如何获得在 main 中使用的方法的值
- firebase - 项目之间的 Firebase 迁移 - 导入命令时目标 Fire Store 数据库不会更改(未显示错误)
- hyperledger-fabric - 是否可以在 levelDB 超级账本结构、Javascript 上添加表
- ros - 我在设置 Ros 主从时遇到问题