我在Excel 2013上绘制图表时遇到了一些问题。
我想绘制两个轴使用相同比例的图形(因此图形应为正方形)。此时x和y的步长相同,但x轴约为y轴长度的两倍。我知道可以移动图表的边界。但是,我希望我的图表完全是一个正方形,这很难手动实现。是否可以在Excel 2013上自动执行此操作?如果有,怎么样?如果没有,你能否推荐一些可以帮助我的其他程序?
我希望我的问题有道理。
非常感谢你的帮助!谢谢!
答案 0 :(得分:0)
这有助于使用VBA
:
Sub SizeChart()
Dim cht As ChartObject
Set cht = ActiveSheet.ChartObjects("Chart 1")
With cht
.Height = 300
.Width = 300
End With
End Sub
答案 1 :(得分:0)
这是对xy-charts中两个轴的相等缩放问题的更一般的解决方案。
Excel通常会对两个轴进行自动调整,并且不提供保持x和y标度相等的选项。
以下VBA模块准确地将缩放设置为最适合plotArea
(边界框)中图表的diagramArea
。
它首先将plotArea
向上吹到最大值然后看,哪个轴将被缩小为相同的比例。
Option Explicit
''
' Default borders between plot area and chart area
'
Const BORDER_LEFT = 4
Const BORDER_RIGHT = 4
Const BORDER_TOP = 29
Const BORDER_BOTTOM = 4
''
' test procedure for ScaleChart
Sub test1()
Dim ws As Worksheet
Dim oChart As ChartObject
Set ws = ActiveSheet
Set oChart = ws.ChartObjects("Diagramm 4")
If ScaleChart(oChart) Then
MsgBox "The axes are now equally scaled", vbInformation, "Success"
Else
MsgBox "An error occured", vbCritical, "Failed"
End If
End Sub
''
' ScaleChart - set equal scaling to both axes of a xy-chart
'
' oChart a chartObject
'
' borderLeft (optional) left border between plot area and chart ares
' borderRight (optional) right border between plot area and chart ares
' borderTop (optional) top border between plot area and chart ares
' borderBottom (optional) bottom border between plot area and chart ares
'
' returns true on success, false on any error
'
Function ScaleChart(oChart As ChartObject, _
Optional borderLeft As Double = BORDER_LEFT, Optional borderRight As Double = BORDER_RIGHT, _
Optional borderTop As Double = BORDER_TOP, Optional borderBottom As Double = BORDER_BOTTOM) As Boolean
'
' Variables
'
Dim xMin As Double
Dim xMax As Double
Dim yMin As Double
Dim yMax As Double
Dim xWidth As Double
Dim yHeight As Double
Dim scaleX As Double
Dim scaleY As Double
On Error GoTo mark_err
With oChart.Chart
' ''
' ' display currend border settings
' Debug.Print "Const BORDER_LEFT = "; .PlotArea.Left
' Debug.Print "Const BORDER_RIGHT = "; .ChartArea.Width - .PlotArea.Width - .PlotArea.Left
'
' Debug.Print "Const BORDER_TOP = "; .PlotArea.Top
' Debug.Print "Const BORDER_BOTTOM = "; .ChartArea.Height - .PlotArea.Height - .PlotArea.Top
'
' ''
''
' reset plot area to full size
'
.PlotArea.Left = BORDER_LEFT
.PlotArea.Width = .ChartArea.Width - .PlotArea.Left - BORDER_RIGHT
.PlotArea.Top = BORDER_TOP
.PlotArea.Height = .ChartArea.Height - .PlotArea.Top - BORDER_BOTTOM
'
''
''
' get axis min/max values and current display sizes
'
xMin = .axes(xlCategory).MinimumScale
xMax = .axes(xlCategory).MaximumScale
xWidth = .PlotArea.Width
yMin = .axes(xlValue).MinimumScale
yMax = .axes(xlValue).MaximumScale
yHeight = .PlotArea.Height
End With
scaleX = (xMax - xMin) / xWidth
scaleY = (yMax - yMin) / yHeight
' '
' ' scales information
' '
' Debug.Print "x-axis: M 1:"; 1 / scaleX, "y-axis: M 1:"; 1 / scaleY
If 1 / scaleX > 1 / scaleY Then
'
' the diagram has to be reduced in x-direction
'
oChart.Chart.PlotArea.Width = oChart.Chart.PlotArea.Width * (1 / scaleY) / (1 / scaleX)
Else
'
' the diagram has to be reduced in y-direction
'
oChart.Chart.PlotArea.Height = oChart.Chart.PlotArea.Height * (1 / scaleX) / (1 / scaleY)
End If
'
' no error - return true
'
ScaleChart = True
On Error GoTo 0
Exit Function
mark_err:
'
' error - return false
'
ScaleChart = False
On Error GoTo 0
End Function