0

如何检索excel图表系列集合的最大值和最小值以及图表上相同的位置?我想计算系列中每个点的标签应该在哪里并相应地移动它。

我这样做是为了让它们看起来不错,以防你想知道。如果我只是盲目地移动标签,数字往往会变得一团糟。我确信我可以在图表底部找到数字,但这不是我想要做的。Chart 对象很难与 Object Listing thing-a-ma-jig (当您在对象名称后点击句点时弹出的 whatcha-ma-call-it )一起工作并没有多大帮助。

编辑:我想要实现的是检索某个系列的 excel 图表的最高(最大值)和最低(最小值)值。我还想获得他们在图表本身上的位置(从顶部或左侧)。我知道看哪种方式,但是如果您提供代码来检查哪种方式来衡量,那将是一个奖励。见下图。

   -------------------Left------------------------------------- >
  |        ________________________________________
  |       |                                        |
  |   225 |----------------------------------------|max (highest)
  |   200 |                                        |
  |   175 |                                        |
  |   150 |            -----                       |
|Top| 125 |           /     \                      |
  |   100 |          /       \                     |
  |    75 |   -------         \                    |
  |    50 |  /                 --------------\     |
  |    25 | /                                 \    |
 \|/    0 |----------------------------------------|min (lowest)
  V       |________________________________________|
4

3 回答 3

2

你根本不需要任何 VBA。我知道你想要什么,我已经创建了它。我将解释你如何为最大位置做这件事,就像你在最小位置做类似的事情一样。首先,我们需要找到最大 Y 轴标签,然后就很容易了。

让我们将其分解为五个步骤:

  1. 公式中的 A:确定比例。
  2. 公式中的 B:按该比例进行比较。
  3. 公式中的 C:测量间隔。
  4. 公式中的 D:四舍五入。
  5. 公式中的 E:计算最大轴标签。

好的,这是每个步骤的公式:

  1. =ROUNDDOWN(LOG(MAX(data));1) 其中 data 代表包含您的值的单元格。
  2. =MAX(数据)/10^A)*1.05
  3. =0.2+(B>2)*0.3+(B>5)*0.5
  4. =舍入(B;C)
  5. =(C+D)*10^A

我自己使用荷兰语公式,所以我可能会出现轻微的翻译错误。除此之外,我向你保证这是可行的。其原因是,我已经弄清楚 Excel 如何使用图表“思考”。我将尝试解释这是如何工作的。

基本上,Excel 使用三个基本范围:从 1 到 2、从 2 到 5 和从 5 到 10.. 当一个数字高于 10 时,10 将再次被视为 1,您将退回到第一个范围. 这就是为什么我们首先使用 LOG 公式确定比例。如果您是新手,请在 wiki 中查找。

因此,当我们有了刻度时,我们确定它属于哪个范围。对于这三个范围中的每一个,y 轴标签间隔都是不同的。所以我们在第三步计算它们,并在第四步使用它来向下舍入数字 B。第五步只是将它乘回到原来的规模。

就这样:找到了最大 Y 轴标签。看图表,应该是一样的。如果你明白了,你也会找到最小的 Y 轴标签。现在很酷的事情是,您可以非常轻松地计算出标签的确切位置,因为您现在知道网格的大小。

祝你好运,如果你还有问题,请告诉我。

帕特里克

于 2012-11-16T05:37:11.400 回答
0

好吧,经过更多的实验(这是 VBA 的目的之一)我明白了。我的具体案例有两个相互重叠的条形系列。代码相当长,但速度很快。我想我在这个过程中学到了一些关于如何加速 vba 的知识!

Sub AdjustReportChart()
    'main report chart
    Dim mrc As Chart
    Dim sercol As SeriesCollection
    Dim axe As Axis, ser As series
    Dim poi1 As Point, poi2 As Point
    Dim i As Integer, j As Integer
    'select the chart
    If hwb Is Nothing Then Call SetGlobals
    Set mrc = mws.ChartObjects("Chart 1").Chart
    'delave all the needed vars
    Dim poi1pos As Double, poi1val As Double
    Dim min1 As Integer, max1 As Integer
    Dim poi2pos As Double, poi2val As Double
    Dim min2 As Integer, max2 As Integer
    'get the chart width params
    Dim width As Integer
    width = Int(mrc.PlotArea.InsideWidth)
    Set sercol = mrc.SeriesCollection
    Set axe = mrc.Axes(2, sercol(1).AxisGroup)
    min1 = axe.MinimumScale
    max1 = axe.MaximumScale
    Set axe = mrc.Axes(2, sercol(2).AxisGroup)
    min2 = axe.MinimumScale
    max2 = axe.MaximumScale
    'start adjusting. 
    For j = 1 To sercol(1).points.Count
        Set poi1 = sercol(1).points(j)
        Set poi2 = sercol(2).points(j)
        poi1pos = poi1.DataLabel.Left
        poi2pos = poi2.DataLabel.Left
        poi1val = poi1.DataLabel.Text
        poi2val = poi2.DataLabel.Text
        poi1pos = (poi1val / (max1 - min1) * width - 6) + 142
        poi2pos = (poi2val / (max2 - min2) * width - 6) + 142
        If poi2pos < poi1pos + (Len(Str(poi1val)) * 6) And _
            poi1pos < poi2pos + (Len(Str(poi2val)) * 6) Then
            poi2pos = poi1pos + (Len(Str(poi1val)) * 6)
        End If
        poi1.DataLabel.Left = poi1pos
        poi2.DataLabel.Left = poi2pos
    Next j
End Sub
于 2012-10-18T15:53:49.283 回答
0

目前尚不清楚您想要实现什么,但这里有一个访问Series,PointsLabels图表的演示。

Sub MoveLabels()
    Dim sh As Worksheet
    Dim oCh As Chart
    Dim oSers As SeriesCollection
    Dim oSer As Series
    Dim oPts As Points
    Dim oPt As Point
    Dim oLbls As DataLabels
    Dim oLbl As DataLabel
    Dim i As Long, pt As Long


    Set sh = ActiveSheet
    Set oCh = sh.ChartObjects("Chart 8").Chart

    ' Series Collection of a chart
    Set oSers = ch.SeriesCollection
    For Each oSer In oSers
        'Labels collection of a series
        Set oLbls = oSer.DataLabels
        For Each oLbl In oLbls
            ' Label Object

        Next

        'Points collection of a series
        Set oPts = oSers.Points
        For Each oPt In oPts
            ' Label Object
            Set oLbl = oPt.DataLabel
        Next
    Next
End Sub
于 2012-10-17T08:18:54.087 回答