答案1
如果不使用 VBA,这是不可能的,但使用 VBA 可以自动触发任何图表更改。此代码假设图表是嵌入图表(嵌入在工作表中),而不是图表表。
- 选择选项
Developer
卡 (如果没有显示说明)。 - 选择
Editor
。 - 在项目窗口中的任意位置单击鼠标右键,然后选择
Insert
,Module
。 - 选择新的模块窗口后按F6(或单击
View
然后Properties Window
)。 - 将名称更改为
ChartEventModule
,然后关闭属性窗口。 - 将以下代码粘贴到
ChartEventModule
窗口中:
ChartEventModule 代码:
Option Explicit
Dim chartEventClassModule As New chartEventClassModule
Private Const chartSheet = "Sheet1"
Private Const chartNumber = 1
Sub RecalculateXAxisTitlePosition()
Dim chart As chart, plot As PlotArea, axis As axis, title As AxisTitle, titleXPos As Double, titleYPos As Double
Set chart = Worksheets(chartSheet).ChartObjects(chartNumber).chart
Set plot = chart.PlotArea
Set axis = chart.Axes(xlCategory) ' xlCategory is X axis, xlValue is Y axis
' If the X axis doesn't have a title, exit out
If Not axis.HasTitle Then Exit Sub
Set title = axis.AxisTitle
' Not necessary to set font size and text each time
title.Text = "Verknadsgrad"
title.Font.Size = 12
' .Position can be xlChartElementPositionAutomatic (-4105) or xlChartElementPositionCustom (-4114)
title.Position = xlChartElementPositionCustom
' Without a title the plotarea top is 9
' With a title font size 12 the title height is 17.4, and the plotarea top is 26.4
plot.Top = 9
' The PlotArea includes the axes. The Y axis throws off our centering if we center based on the PlotArea.
titleXPos = axis.Left + (axis.Width / 2) - (title.Width / 2)
titleYPos = plot.Top + plot.Height
title.Left = titleXPos
title.Top = titleYPos
' OPTIONAL CODE
' This code sets the background fill to DiagonalDown, which ensures it goes exactly from corner to corner
With plot.Format.Fill
.Visible = msoTrue
.TwoColorGradient Style:=msoGradientDiagonalDown, variant:=1
' Green color
.ForeColor.RGB = RGB(0, 176, 0)
' Red color
.BackColor.RGB = RGB(255, 0, 0)
' Percentage to be fully red (<=15%)
.GradientStops(1).Position = 0.15
' Percentage to be fully green (>=85%)
.GradientStops(2).Position = 0.85
' Middle color (yellow), center point (50%), Transparency (0%), Point to insert into list of gradients (1)
.GradientStops.Insert RGB(255, 255, 0), 0.5, 0, 1
End With
End Sub
Sub Initialise()
Set chartEventClassModule.myChart = Worksheets(chartSheet).ChartObjects(chartNumber).chart
End Sub
我不喜欢在设置渐变时必须设置静态角度,所以我在代码中设置了它。它被设置为DiagonalDown
根据图表的纵横比自动计算角度。
- 确保在代码顶部适当地
chartSheet
设置2 个常量。chartNumber
- 返回项目窗口,右键单击并创建一个
Class Module
。 - 使用相同的属性窗口(F6),将名称设置为
ChartEventClassModule
。 - 将以下代码粘贴到
ChartEventClassModule
窗口中:
ChartEventClassModule 代码:
Option Explicit
' https://msdn.microsoft.com/VBA/Excel-VBA/articles/using-events-with-embedded-charts
Public WithEvents myChart As chart
Private Sub myChart_Calculate()
Call ChartEventModule.RecalculateXAxisTitlePosition
End Sub
Private Sub myChart_Resize()
Call ChartEventModule.RecalculateXAxisTitlePosition
End Sub
Private Sub myChart_SeriesChange(ByVal SeriesIndex As Long, ByVal PointIndex As Long)
Call ChartEventModule.RecalculateXAxisTitlePosition
End Sub
此代码检测图表的变化并重新运行该函数以设置轴标题。不幸的是,我们图表中的可用事件有限。
- 在项目窗口中双击
ThisWorkbook
,然后粘贴以下代码:
本工作簿代码:
Private Sub Workbook_Open()
ChartEventModule.Initialise
End Sub
- 将工作簿另存为宏启用工作簿(.xlsm)。关闭并重新打开工作簿。