You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按示例格式修改VBA EMA图表的背景色与侧边内边距?

如何修改VBA EMA图表的背景色与侧边内边距

一、修改背景色

你可以分别设置图表区和绘图区的背景色:

  • 图表区(整个图表的外围区域):通过.ChartArea.Format.Fill设置填充色
  • 绘图区(折线所在的核心区域):通过.PlotArea.Format.Fill设置填充色

可以用RGB(r,g,b)指定精准颜色,也能用ColorIndex调用内置色号,前者更可控。

二、调整侧边内边距

通过设置绘图区(PlotArea)的Left、Right、Top、Bottom属性,控制它和图表区的间距,实现侧边内边距的调整。


修改后的完整代码

Set EMAChart = Sheets("Parameters").ChartObjects.Add(Left:=Range("a18").Left, Width:=700, Top:=Range("a18").Top, Height:=500)
With EMAChart.Chart
   'EMA_A Grafik
    .Parent.Name = "EMA Chart"

    ' --------------------------
    ' 新增:修改图表区背景色(浅灰色示例)
    .ChartArea.Format.Fill.ForeColor.RGB = RGB(240, 240, 240)
    ' 新增:修改绘图区背景色(白色示例)
    .PlotArea.Format.Fill.ForeColor.RGB = RGB(255, 255, 255)
    ' 新增:调整侧边内边距(数值可按需修改)
    .PlotArea.Left = 50    ' 左侧内边距
    .PlotArea.Right = 20   ' 右侧内边距
    .PlotArea.Top = 30     ' 顶部内边距
    .PlotArea.Bottom = 40  ' 底部内边距
    ' --------------------------

    With .SeriesCollection.NewSeries
        .ChartType = xlLine
        .Values = Sheets("data").Range("e2:e" & numRows)
        .XValues = Sheets("data").Range("a2:a" & numRows)
        .Format.Line.Weight = 1
        .Border.ColorIndex = 1
        .Name = "Price"
    End With

    With .SeriesCollection.NewSeries
        .ChartType = xlLine
        .AxisGroup = xlPrimary
        .Values = Sheets("data").Range("h2:h" & numRows)
        .Name = "EMA_A"
        .Border.ColorIndex = 44
        .Format.Line.Weight = 1

    End With
    'EMA_B Grafik
    With .SeriesCollection.NewSeries
        .ChartType = xlLine
        .AxisGroup = xlPrimary
        .Values = Sheets("data").Range("ı2:ı" & numRows)
        .Name = "EMA_B"
        .Border.ColorIndex = 26
        .Format.Line.Weight = 1

    End With
    'EMA_c Grafik
    With .SeriesCollection.NewSeries
        .ChartType = xlLine
        .AxisGroup = xlPrimary
        .Values = Sheets("data").Range("j2:j" & numRows)
        .Name = "EMA_C"
        .Border.ColorIndex = 17
        .Format.Line.Weight = 1

    End With
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Price"
    .Axes(xlValue, xlPrimary).MaximumScale = WorksheetFunction.Max(Sheets("Data").Range("e2:e" & numRows))
    .Axes(xlValue, xlPrimary).MinimumScale = Int(WorksheetFunction.Min(Sheets("Data").Range("e2:e" & numRows)))
    .Legend.Position = xlLegendPositionBottom
    .SetElement (msoElementChartTitleAboveChart)
    .ChartTitle.Text = " "

End With

说明

  • 背景色的RGB值可以根据你的附图示例自行调整,比如需要深蓝色背景就改成RGB(0, 51, 102)
  • 内边距的数值(Left/Right/Top/Bottom)可根据图表大小和视觉需求微调,数值越大,绘图区和图表边缘的间距就越大

内容的提问来源于stack exchange,提问作者ozgn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 12:18:13