Chart.GetChartElement 方法

获取有关指定 X 和 Y 坐标上的图表元素的信息。

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel(在 Microsoft.Office.Tools.Excel.dll 中)

语法

声明
Sub GetChartElement ( _
    x As Integer, _
    y As Integer, _
    ByRef ElementID As Integer, _
    ByRef Arg1 As Integer, _
    ByRef Arg2 As Integer _
)
void GetChartElement(
    int x,
    int y,
    ref int ElementID,
    ref int Arg1,
    ref int Arg2
)

参数

  • ElementID
    类型:System.Int32%
    该方法返回时,此参数包含指定坐标上的图表元素的 XlChartItem 值。有关更多信息,请参见“备注”部分。
  • Arg1
    类型:System.Int32%
    该方法返回时,此参数包含与图表元素有关的信息。有关更多信息,请参见“备注”部分。
  • Arg2
    类型:System.Int32%
    该方法返回时,此参数包含与图表元素有关的信息。有关更多信息,请参见“备注”部分。

备注

此方法比较特殊,因为只需指定前两个参数的值。 Microsoft Office Excel 会填充其他参数,您的代码应在该方法返回时检查这些值。

该方法返回后的 ElementID 值确定 Arg1 和 Arg2 是否包含如下表中所示的信息。

ElementID

Arg1

Arg2

xlAxis

AxisIndex

AxisType

xlAxisTitle

AxisIndex

AxisType

xlDisplayUnitLabel

AxisIndex

AxisType

xlMajorGridlines

AxisIndex

AxisType

xlMinorGridlines

AxisIndex

AxisType

xlPivotChartDropZone

DropZoneType

xlPivotChartFieldButton

DropZoneType

PivotFieldIndex

xlDownBars

GroupIndex

xlDropLines

GroupIndex

xlHiLoLines

GroupIndex

xlRadarAxisLabels

GroupIndex

xlSeriesLines

GroupIndex

xlUpBars

GroupIndex

xlChartArea

xlChartTitle

xlCorners

xlDataTable

xlFloor

xlLeaderLines

xlLegend

xlNothing

xlPlotArea

xlWalls

xlDataLabel

SeriesIndex

PointIndex

xlErrorBars

SeriesIndex

xlLegendEntry

SeriesIndex

xlLegendKey

SeriesIndex

xlSeries

SeriesIndex

PointIndex

xlShape

ShapeIndex

xlTrendline

SeriesIndex

TrendLineIndex

xlXErrorBars

SeriesIndex

xlYErrorBars

SeriesIndex

下表说明了该方法返回后的 Arg1 和 Arg2 的含义。

参数

描述

AxisIndex

指定轴是主轴或还是辅助轴。 可以是下列 XlAxisGroup 常数之一:xlPrimaryxlSecondary

AxisType

指定轴类型。 可以为以下 XlAxisType 常数之一:xlCategoryxlSeriesAxisxlValue

DropZoneType

指定拖放区域类型:列、数据、页或行字段。 可以为以下 XlPivotFieldOrientation 常数之一:xlColumnFieldxlDataFieldxlPageFieldxlRowField。 列和行字段常数分别指定系列和类别字段。

GroupIndex

指定特定图表组在 ChartGroups 集合中的偏移量。

PivotFieldIndex

指定特定列(系列)、数据、页或行(类别)字段在 PivotFields 集合中的偏移量。 如果拖放区域类型为 xlDataField,则为 -1。

PointIndex

指定系列中的特定点在 Points 集合中的偏移量。 该值为 – 1 时指示选择了所有数据点。

SeriesIndex

指定特定系列在 Series 集合中的偏移量。

ShapeIndex

指定特定形状在 Shapes 集合中的偏移量。

TrendlineIndex

指定系列中的特定趋势线在 Trendlines 集合中的偏移量。

示例

下面的代码示例创建 Microsoft.Office.Tools.Excel.Chart,然后使用 GetChartElement 方法在用户单击图表时显示图表元素。

WithEvents elementChart As Microsoft.Office.Tools.Excel.Chart

Private Sub DisplayChartElement()
    Globals.Sheet1.Range("A1", "A5").Value2 = 22
    Globals.Sheet1.Range("B1", "B5").Value2 = 55

    elementChart = Me.Controls.AddChart(Me.Range("D2", "H12"), _
        "elementChart")
    elementChart.SetSourceData(Globals.Sheet1.Range("A1", "B5"), _
        Excel.XlRowCol.xlColumns)
    elementChart.ChartType = Excel.XlChartType.xl3DColumn
End Sub

Sub elementChart_MouseDownHandler(ByVal Button As Integer, ByVal Shift As Integer, _
    ByVal x As Integer, ByVal y As Integer) Handles elementChart.MouseDown

    Dim elementID As Integer = 0
    Dim arg1 As Integer = 0
    Dim arg2 As Integer = 0

    elementChart.GetChartElement(x, y, elementID, arg1, arg2)

    MsgBox("Chart element is: " & CType(elementID, Excel.XlChartItem).ToString() _
            & Constants.vbNewLine & "arg1 is: " & arg1.ToString() _
            & Constants.vbNewLine & "arg2 is: " & arg2.ToString())
End Sub
private Microsoft.Office.Tools.Excel.Chart elementChart;

private void DisplayChartElement()
{
    this.Range["A1", "A5"].Value2 = 22;
    this.Range["B1", "B5"].Value2 = 55;

    elementChart = this.Controls.AddChart(this.Range["D2", "H12"],
        "elementChart");

    elementChart.SetSourceData(this.Range["A1", "B5"],
        Excel.XlRowCol.xlColumns);
    elementChart.ChartType = Excel.XlChartType.xl3DColumn;
    elementChart.MouseDown +=
        new Excel.ChartEvents_MouseDownEventHandler(elementChart_MouseDown);
}

void elementChart_MouseDown(int Button, int Shift, int x, int y)
{
    Int32 elementID = 0;
    Int32 arg1 = 0;
    Int32 arg2 = 0;

    elementChart.GetChartElement(x, y, ref elementID, ref arg1, ref arg2);

    MessageBox.Show("Chart element is: " + ((Excel.XlChartItem)elementID).ToString()
        + "\n arg1 is: " + arg1.ToString() + "\n arg2 is: " + arg2.ToString());
}

.NET Framework 安全性

请参见

参考

Chart 接口

Microsoft.Office.Tools.Excel 命名空间