如何使用 VB 在 Excel 中的图表上绘制大括号

How to Draw a Curly Brace on a Chart in Excel Using VB

我是 VBA 的新手,使用 Excel 并且一直在寻找这个问题的答案几个小时但没有成功,所以希望有人能帮助我。

我在 Excel 2016 年使用 VB 绘制了一张图表。这张图表有一个 XY 散点图和 2 条水平虚线来向查看者指示一个范围,但我想画一个花括号在水平线之间标记此范围的含义。

这是我的代码示例:

Sub Create_Chart()

Dim co As ChartObject
Dim ct As Chart
Dim sc1 As SeriesCollection
Dim ser1 As Series
Dim intMaxX As Integer
Dim intMinX As Integer
Dim intMaxY As Integer
Dim intMinY As Integer

intMaxX = 20000
intMinX = 0
intMaxY = Range("Sheet1!C25").Value
intMinY = Range("Sheet1!C26").Value

Set co = Sheet2.ChartObjects.Add(Range("K1").Left, Range("K1").Top, 1000, 600)

Set ct = co.Chart

With ct
 'Code for my main XYScatterplot chart is here. This all works fine so I am not including it
End With

'This code draws one of my horizontal dashed lines
With ct.SeriesCollection.NewSeries
    .Name = ""
    .XValues = Array(intMinX, intMaxX)
    .Values = Array(intMinY, intMinY)
    .Format.Line.ForeColor.RGB = RGB(255, 0, 0)
    .Format.Line.DashStyle = msoLineDash
    .MarkerStyle = xlMarkerStyleNone
End With

'This code draws my other horizontal dashed line
With ct.SeriesCollection.NewSeries
    .Name = ""
    .XValues = Array(intMinX, intMaxX)
    .Values = Array(intMaxY, intMaxY)
    .Format.Line.ForeColor.RGB = RGB(255, 0, 0)
    .Format.Line.DashStyle = msoLineDash
    .MarkerStyle = xlMarkerStyleNone
End With

'Then I tried to draw my curly brace. I want it to be placed between the two dashed lines
ct.Shapes.AddShape(msoShapeRightBrace, desired x-coordinate on my graph, intMaxY, 10, (intMaxY - intMinY)).TextFrame.Characters.Text = ""

End Sub

现在,这确实绘制了一个大括号,但它很小并且在右上角结束。它甚至似乎根本不属于我的图表。我如何将它与我的图表相关联,特别是我的水平虚线的确切位置,您可以看到它取决于我的一张工作表中的值。

非常感谢您的帮助。

更新: 感谢回复和我在其他地方阅读的几篇文章,这就是我想出的:

Set curlyBrace = ct.Shapes.AddShape(msoShapeRightBrace, ct.PlotArea.InsideLeft, ct.PlotArea.InsideTop, 15, ct.PlotArea.InsideHeight)
curlyBrace.IncrementLeft ct.PlotArea.Left + ct.PlotArea.InsideWidth * 0.96
curlyBrace.IncrementTop ct.PlotArea.InsideHeight - ct.PlotArea.Position
With curlyBrace.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
    .Transparency = 0
End With

基于这段代码,我可以生成如下图(我只展示了水平虚线和花括号)

enter image description here

现在如何使花括号的垂直起点和终点与两条水平线的位置相匹配?

非常感谢。

以下是(例如)如何绘制覆盖所有绘图区域的大括号:

Dim b As Shape

Set b = ct.Shapes.AddShape(msoShapeRightBrace, _
                ct.PlotArea.InsideLeft, _
                ct.PlotArea.InsideTop, _
                ct.PlotArea.InsideWidth, _
                ct.PlotArea.InsideHeight)

您只需要根据您的 XY 坐标系应用一点数学,根据需要调整位置和大小...