首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从海图轴中提取最大值

从海图轴中提取最大值
EN

Stack Overflow用户
提问于 2021-09-24 10:37:55
回答 3查看 134关注 0票数 0

我有一个2轴的图表表,我需要提取主轴的最大值来精确地设置第二轴。我似乎不能让函数简单地读取主轴最大值,并将其放在一个单元格中,有什么想法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-09-24 11:25:41

这将返回左轴的最大值,通常是原始数据的<>最大值。

代码语言:javascript
运行
复制
Public Function getAxisMaxScale(cht As Chart) As Single

Dim ax As Axis
Set ax = cht.Axes(xlValue)

getAxisMaxScale= ax.MaximumScale


End Function

若要调整可使用的二级轴,请执行以下操作

代码语言:javascript
运行
复制
Public sub adjustAxisForChartXYZ()

Dim cht As Chart
Set cht = ThisWorkbook.Sheets("XYZ")   'insert sheetname of your chart

adjustSecondaryAxis cht

End Sub


Public Sub adjustSecondaryAxis(cht As Chart)

Dim axPrimary As Axis, axSecondary As Axis
Set axPrimary = cht.Axes(xlValue)

Set axSecondary = cht.Axes(xlValue, xlSecondary)

With axSecondary
    .MajorUnit = axPrimary.MajorUnit
    .MaximumScale = axPrimary.MaximumScale
End With

End Sub

我也调整了MajorUnit .也要对齐

票数 0
EN

Stack Overflow用户

发布于 2021-09-24 11:23:07

欢迎来到董事会。阅读ASKMCVE

在手动更改轴时记录宏将为您提供:

代码语言:javascript
运行
复制
Sub Macro4()
'
' Macro4 Macro
'

'
    ActiveSheet.ChartObjects("Chart 2").Activate
    ActiveChart.Axes(xlValue).Select
    ActiveChart.Axes(xlValue).MaximumScale = 60
    ActiveChart.Axes(xlValue, xlSecondary).Select
    ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = 90
    Range("L2").Select
End Sub  

代码显示,您需要在工作表中的图表对象中引用图表,才能到达轴。

代码语言:javascript
运行
复制
Sub Test()
    
    Sheet1.ChartObjects("Chart 2").Chart.Axes(xlValue, xlSecondary).MaximumScale = _
        Sheet1.ChartObjects("Chart 2").Chart.Axes(xlValue).MaximumScale

End Sub

注意-可以使用变量和With.End With块缩短这段代码。

票数 0
EN

Stack Overflow用户

发布于 2021-09-24 12:31:46

这就是如何在图表中得到这两个系列的最小/最大值:

选择图表,然后运行以下命令

代码语言:javascript
运行
复制
Dim ser as SeriesCollection
' `ChartArea.Parent` = `Chart` object
Set ser = Selection.Parent.SeriesCollection()

Dim temp1() As Variant, temp2() As Variant
temp1 = ser(1).values
temp2 = ser(2).values
    
' Get the data limits
Dim a_1 As Double, a_2 As Double, b_1 As Double, b_2 As Double
a_1 = WorksheetFunction.Min(temp1): a_2 = WorksheetFunction.Min(temp2)
b_1 = WorksheetFunction.Max(temp1): b_2 = WorksheetFunction.Max(temp2)

这是我开发的宏的一部分,用于缩放图表的第二轴,例如滴答落在第一轴的网格上。

先于

请注意,在第二轴上,10到60的网格不是在所示的网格线上。这使得你很难读懂图表。

然后,调整第二轴上的网格间距以匹配主轴。此外,限制调整,以填补更好的面积。

代码

下面是我用来做这件事的代码。

代码语言:javascript
运行
复制
Public Sub DualYAxisChart(ByRef ch As Chart)
    Dim x_axis_1 As Axis, x_axis_2 As Axis, n As Long
    Dim ser As SeriesCollection
    Set ser = ch.SeriesCollection()
    If ser.Count <> 2 Then
        MsgBox "Please select a chart with two series", vbOKOnly, "Dual y-axis Chart"
        Exit Sub
    End If
    
    If UBound(ser(1).values) <> UBound(ser(2).values) Then
        MsgBox "Both series must share the same number of points", vbOKOnly, "Dual y-axis Chart"
        Exit Sub
    End If
    n = UBound(ser(1).values)
    
    ser(1).AxisGroup = xlPrimary
    ser(2).AxisGroup = xlSecondary
    
    ' Enable Both Y-axes
    ch.SetElement msoElementPrimaryValueAxisShow
    ch.SetElement msoElementSecondaryValueAxisShow
    ' Enable Both X-axes
    ch.SetElement msoElementPrimaryCategoryAxisShow
    ch.SetElement msoElementSecondaryCategoryAxisShow
       
    ' Delete Secondary x-axis
    ch.SetElement msoElementSecondaryCategoryAxisNone
    
    Set x_axis_1 = ch.Axes(xlValue, xlPrimary)
    Set x_axis_2 = ch.Axes(xlValue, xlSecondary)
    
    x_axis_1.format.Line.ForeColor.RGB = ser(1).format.Line.ForeColor.RGB
    x_axis_1.format.Line.EndArrowheadStyle = msoArrowheadTriangle
    x_axis_2.format.Line.ForeColor.RGB = ser(2).format.Line.ForeColor.RGB
    x_axis_2.format.Line.EndArrowheadStyle = msoArrowheadTriangle

    
    ' Auto Scale All
    x_axis_1.MajorUnitIsAuto = True
    x_axis_1.MaximumScaleIsAuto = True
    x_axis_1.MinimumScaleIsAuto = True
    x_axis_2.MajorUnitIsAuto = True
    x_axis_2.MaximumScaleIsAuto = True
    x_axis_2.MinimumScaleIsAuto = True
    
    Dim a_1 As Double, a_2 As Double, b_1 As Double, b_2 As Double
    Dim x_1 As Double, s_1 As Double, x_2 As Double, s_2 As Double, g_1 As Double, g_2 As Double
    Dim n_1 As Long, n_2 As Long, sense As Long
    
    ' Get the axis limits
    s_1 = x_axis_1.MinimumScale
    x_1 = x_axis_1.MaximumScale
    g_1 = x_axis_1.MajorUnit
    n_1 = CLng((x_1 - s_1) / g_1)
    
    s_2 = x_axis_2.MinimumScale
    x_2 = x_axis_2.MaximumScale
    g_2 = x_axis_2.MajorUnit
    n_2 = CLng((x_2 - s_2) / g_2)
    
    Dim temp1() As Variant, temp2() As Variant
    temp1 = ser(1).values
    temp2 = ser(2).values
        
    ' Get the data limits
    a_1 = WorksheetFunction.Min(temp1): a_2 = WorksheetFunction.Min(temp2)
    b_1 = WorksheetFunction.Max(temp1): b_2 = WorksheetFunction.Max(temp2)
    
    sense = Sgn((b_2 - a_2) / (b_1 - a_1))
    
    If sense < 0 Then
        x_axis_2.ReversePlotOrder = True
        Swap a_2, b_2
        x_axis_2.format.Line.EndArrowheadStyle = msoArrowheadNone
        x_axis_2.format.Line.BeginArrowheadStyle = msoArrowheadTriangle
    End If
    
    x_axis_1.MinimumScale = a_1: x_axis_1.MaximumScale = b_1
    g_1 = x_axis_1.MajorUnit
    n_1 = CLng((x_1 - s_1) / g_1)
    x_axis_2.MinimumScale = a_2: x_axis_2.MaximumScale = b_2
    g_2 = x_axis_2.MajorUnit
    n_2 = CLng((x_2 - s_2) / g_2)
    
    g_2 = (x_2 - s_2) / n_1
    x_axis_2.MajorUnit = g_2
        
    Dim i_1 As Long, i_2 As Long
    i_1 = WorksheetFunction.Floor_Math(a_1 / g_1)
    s_1 = i_1 * g_1
    i_2 = WorksheetFunction.Floor_Math(a_2 / g_2)
    s_2 = i_2 * g_2
    
    Dim j_1 As Long, j_2 As Long
    j_1 = WorksheetFunction.Ceiling_Math(b_1 / g_1)
    x_1 = j_1 * g_1
    j_2 = WorksheetFunction.Ceiling_Math(b_2 / g_2)
    x_2 = j_2 * g_2
    
    x_axis_1.MinimumScale = s_1: x_axis_1.MaximumScale = x_1
    x_axis_2.MinimumScale = s_2: x_axis_2.MaximumScale = x_2
    
    x_axis_1.MajorUnitIsAuto = True
    g_1 = x_axis_1.MajorUnit
    n_1 = CLng((x_1 - s_1) / g_1)
        
    
    g_2 = (x_2 - s_2) / n_1
    x_axis_2.MajorUnit = g_2

End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69313611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档