ChartSheet.GetChartElement 方法

取得在指定的 X 和 Y 座標上的圖表項目相關資訊。

命名空間:  Microsoft.Office.Tools.Excel
組件:   Microsoft.Office.Tools.Excel.v4.0.Utilities (在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)
  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

None

xlPivotChartFieldButton

DropZoneType

PivotFieldIndex

xlDownBars

GroupIndex

None

xlDropLines

GroupIndex

None

xlHiLoLines

GroupIndex

None

xlRadarAxisLabels

GroupIndex

None

xlSeriesLines

GroupIndex

None

xlUpBars

GroupIndex

None

xlChartArea

None

None

xlChartTitle

None

None

xlCorners

None

None

xlDataTable

None

None

xlFloor

None

None

xlLeaderLines

None

None

xlLegend

None

None

xlNothing

None

None

xlPlotArea

None

None

xlWalls

None

None

xlDataLabel

SeriesIndex

PointIndex

xlErrorBars

SeriesIndex

None

xlLegendEntry

SeriesIndex

None

xlLegendKey

SeriesIndex

None

xlSeries

SeriesIndex

PointIndex

xlShape

ShapeIndex

None

xlTrendline

SeriesIndex

TrendLineIndex

xlXErrorBars

SeriesIndex

None

xlYErrorBars

SeriesIndex

None

下表說明方法傳回後,Arg1 和 Arg2 所代表的意義。

引數

描述

AxisIndex

指定座標軸為主座標軸或副座標軸。 可以是下列其中一個 XlAxisGroup 常數:xlPrimaryxlSecondary

AxisType

指定座標軸類型。 可以是下列其中一個 XlAxisType 常數:xlCategoryxlSeriesAxisxlValue

DropZoneType

指定下拉區域類型:欄、資料、頁面或列欄位。 可以是下列其中一個 XlPivotFieldOrientation 常數:xlColumnFieldxlDataFieldxlPageFieldxlRowField。 欄和列欄位常數會分別指定數列和類別欄位。

GroupIndex

指定特定圖表群組在 Microsoft.Office.Interop.Excel.ChartGroups 集合內的位移 (Offset)。

PivotFieldIndex

指定特定欄 (數列)、資料、頁面或列 (類別) 欄位在 T:Microsoft.Office.Interop.Excel.PivotFields 集合內的位移。 如果下拉區域類型為 xlDataField,則值為 -1。

PointIndex

指定數列中特定資料點在 Points 集合內的位移。 值為 -1 表示選取了所有資料點。

SeriesIndex

指定特定數列在 Series 集合內的位移。

ShapeIndex

指定特定圖案在 Shapes 集合內的位移。

TrendlineIndex

指定數列中特定趨勢線在 Trendlines 集合內的位移。

範例

下列程式碼範例使用 GetChartElement 方法顯示當使用者按一下圖表時會出現圖表項目。

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

    Me.SetSourceData(Globals.Sheet1.Range("A1", "B5"), _
        Excel.XlRowCol.xlColumns)
    Me.ChartType = Excel.XlChartType.xlColumnClustered
End Sub

Sub ChartSheet_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
    ByVal x As Integer, ByVal y As Integer) Handles Me.MouseDown

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

    Me.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 void DisplayChartElement()
{
    Globals.Sheet1.Range["A1", "A5"].Value2 = 22;
    Globals.Sheet1.Range["B1", "B5"].Value2 = 55;

    this.SetSourceData(Globals.Sheet1.Range["A1", "B5"],
        Excel.XlRowCol.xlColumns);
    this.ChartType = Excel.XlChartType.xlColumnClustered;

    this.MouseDown +=
        new Excel.ChartEvents_MouseDownEventHandler(ChartSheet_MouseDown);
}

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

    this.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 安全性

請參閱

參考

ChartSheet 介面

Microsoft.Office.Tools.Excel 命名空間