【问题标题】:Get custom label from clicked column of MS Chart control (WinForm)从 MS Chart 控件(WinForm)的单击列中获取自定义标签
【发布时间】:2014-10-04 05:42:51
【问题描述】:

我在我的 WinForm 项目中使用MS Chart 控件:

我想编写一些代码来获取点击列的标签,例如,如果用户点击 A 列,则返回“A [EAF/LF]”,如果用户点击 B 列,则返回“B [VD/VOD]”。 所以我编写了以下代码来查找图表中的点击列并显示自定义标签:

private void chart_MouseDown(object sender, MouseEventArgs e)
{
    var result = chart.HitTest(e.X, e.Y);

    if (result.ChartElementType == ChartElementType.DataPoint)
    {
       var selectedColumn = chart.Series[0].Points[result.PointIndex];
       var customLabel = selectedColumn.AxisLabel + "[ "
                         result.Axis.CustomLabels[result.PointIndex] + " ]";
       // other codes
     }
}   

但是当运行代码时,result.Axisnull。有谁知道问题出在哪里?

【问题讨论】:

    标签: c# winforms charts mschart


    【解决方案1】:

    只有当HitTest 返回的对象与轴相关联时,Axis 属性才有价值。例如,刻度线或带状线。对于DataPoint 对象,它返回null

    要获取CustomLabels,您可以使用以下代码。

    chart.ChartAreas[chart.Series[0].CharArea].AxisX.CustomLabels[result.PointIndex];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      相关资源
      最近更新 更多