从图表组件中单击弹出图表

时间:2017-06-23 08:39:54

标签: excel excel-vba charts excel-charts vba

这是一个可能性问题,所以请不要指望任何代码,尽管我已尽力将其可视化。 example chart正如你在这里可以看到不同的区域,现在我的问题是,当我点击“亚洲”栏/栏时,我是否可以弹出一个图表,图表将显示所有亚洲国家的集中度。

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。一个非常基本的实现来捕获这些"点击系列中的数据点"事件如下:

课程模块MouseDownChart代码:

Option Explicit
Private WithEvents p_Chart As Chart
Private Sub Class_Terminate()
    Set p_Chart = Nothing
End Sub
Public Sub setChart(ByRef Chart As Chart)
    Set p_Chart = Chart
End Sub
Private Sub p_Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long) 'Capture the event
    Dim sertmp As Series
    Dim IDNum As Long
    Dim a As Long
    Dim B As Long
    p_Chart.GetChartElement x, y, IDNum, a, B
    If IDNum = 3 Then 'Clicked on a point in a chart series
        MsgBox "a is the clicked series: " & a
        MsgBox "B is the clicked datapoint: " & B
        'Write code to pop-up the right stuff here.
    End If
End Sub

在常规模块中:

Option Explicit
Public mdCht As MouseDownChart

Sub Init()
Dim ws As Worksheet
Dim co As ChartObject
Dim ch As Chart

Set ws = Worksheets("Sheet1")
Set co = Worksheets("Sheet1").ChartObjects(1)
Set ch = co.Chart
Set mdCht = New MouseDownChart

mdCht.setChart ch

End Sub

请记住,WorksheetChart可以被引用为" Chart",但是嵌入在工作表中的任何图表(如示例图片中)将始终包含在ChartObject中。