2012-10-17 30 views
0

如何檢索Excel圖表系列集合的最大值和最小值以及圖表上相同的位置?我想計算標籤在系列中每個點的位置並相應地移動它。如何檢索Excel圖表對象中的最大/最小值和位置

我正在這樣做,讓他們看起來不錯,萬一你想知道。如果我只是盲目地移動標籤,這些數字就會變得混亂起來。我確信我可以在圖表的底部有這個數字,但這不是我想要做的。 Chart對象很難與Object對象列表中的事物(即在命名對象名稱後碰到的時候彈出的對應列表)對齊。

編輯:我試圖實現的是檢索某一系列excel圖表的最高(最高)和最低(最低)值。我也想在圖表本身(從頂部或左側)獲得他們的位置。我知道哪種方式看,但如果你給代碼檢查哪種方式來衡量,這將是一個獎金。請參閱下圖。

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

[這個答案](http://stackoverflow.com/a/8773137/445425),而不是你是什麼之後可能有用。順便說一句,這是什麼ma-call它是_IntelliSense_ –

回答

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 
0

目前尚不清楚你想要達到的目標,但這裏的訪問SeriesPoints和圖表的Labels的演示。

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 
+0

那裏,這有幫助嗎? –

2

你根本不需要任何VBA。我知道你想要什麼,我已經創建了它。我會解釋你如何做到最大限度,就像你做到最低限度一樣。首先,我們需要找到最大的Y軸標籤,然後它變得容易。

讓我們把它分解到五個步驟:

  1. 在一個公式:確定規模。
  2. 公式中的B:比較該比例。
  3. 公式中的C:測量間隔。
  4. 公式中的D:將其舍入。
  5. E在公式中:計算最大軸標籤。

行,這裏有用於每個步驟的公式:

  1. = ROUNDDOWN(LOG(MAX(數據)); 1)其中,數據表示包含您的值的單元格。
  2. = MAX(數據)/ 10^A)* 1.05
  3. = 0.2 +(B> 2)* 0.3 +(B> 5)* 0.5
  4. = ROUNDDOWN(B; C)
  5. = (C + D)* 10^A

我自己使用荷蘭公式,所以我可能會做出輕微的翻譯錯誤。除此之外,我保證你的工作。其原因是,我已經認識到Excel如何用圖表思考。我會試着解釋這是如何工作的。

基本上有三個基本範圍Excel使用:從1到2,從2到5和從5到10 ..當一個數字高於10時,10將被視爲1,並且您回退在第一個範圍內。這就是爲什麼我們首先用LOG公式確定比例。如果您是新手,請在wiki中查找。

所以,當我們有規模時,我們確定它落入哪個範圍。對於這三個範圍中的每一個,y軸標籤間隔是不同的。所以我們在第三步中計算它們,並在第四步中使用它來將數字B縮小。第五步簡單地將它乘以它的原始比例。

你去那裏:找到最大的Y軸標籤。看看圖表,它應該是一樣的。如果你有這個想法,你也可以找到最小的Y軸標籤。現在很酷的東西,你可以很容易地計算標籤應該去的地方現在確切地知道網格的大小。

祝你好運,如果你還有問題,讓我知道。

帕特里克

相關問題