2012-03-02 18 views
2

基本上我有一個從Java動態創建的圖表(使用POI),並且通過允許特定點在圖表中着色的值。Excel-VBA-顏色X軸點如果LIKE *值*

爲此,我需要訪問點值標籤,以便我可以測試條件屬性是否適用於每個點值。

比如我有一個seriesPointObject設置變量

  1. 系列名稱
  2. 點值名稱
  3. 條件
  4. 顏色

我的僞代碼如下

For every seriesPointObject in the list 
     Get all Values from Obj 
     For Each series in the series collection 
      Get Every point 
        For every point label 
         Check condition with point value 
          if condition test is true 
           series point change colour 

但我無法訪問每個系列的點值標籤。點值標籤和系列之間必須有連接,但我無法找到它。

有沒有什麼辦法可以從系列對象中獲取點標籤文本?

回答

2

在上面的示例中,它完美地工作,但是,如果我想測試a,b,c和d,該怎麼辦。說:if(pointLabel ==「a」){編輯點的顏色}我認爲在我的問題點標籤和刻度標籤之間有點混亂,因爲我想訪問相關的x軸上的標籤到系列的點。

您好科林

要訪問datavalue或數據點的點標籤,您通過每個數據點必須先循環,然後檢索值。

戴夫已經給你一個方法來檢索Y值。這是另一種可以同時獲得X值和Y值的方法。

Sub FormatPoints() 
    Dim chr As ChartObject 
    Dim chrSeries As Series 
    Dim X() As String 
    Dim lngCnt As Long 
    Dim pnt As Point 
    Set chr = ActiveSheet.ChartObjects(1) 


    For Each chrSeries In chr.Chart.SeriesCollection 
     For Each pnt In chrSeries.Points 
      pnt.DataLabel.ShowCategoryName = True 
      X = Split(pnt.DataLabel.Caption, ",") 

      '---- X Value --------- 
      '~~> This will give you "A" for the above example 
      '~~> which you can use for comparision 
      Debug.Print X(0) 

      '---- Y Value --------- 
      '~~> This will give you 1 
      Debug.Print X(1) ' OR 

      pnt.DataLabel.ShowCategoryName = False 
     Next 
    Next 
End Sub 

編輯

如果數據點是不可見上面的代碼將失敗。您也可以使用此代碼。

Sub FormatPoints() 
    Dim chr As ChartObject 
    Dim chrSeries As Series 
    Dim X() As String 
    Dim lngCnt As Long 
    Dim pnt As Point 
    Set chr = ActiveSheet.ChartObjects(1) 


    For Each chrSeries In chr.Chart.SeriesCollection 
     For Each pnt In chrSeries.Points 
      '~~> You need this line else the code will fail 
      pnt.DataLabel.ShowValue = True 

      pnt.DataLabel.ShowCategoryName = True 
      X = Split(pnt.DataLabel.Caption, ",") 
      pnt.DataLabel.ShowCategoryName = False 

      MsgBox "X Value :" & X(0) & vbNewLine & "Y Value :" & X(1) 
     Next 
    Next 
End Sub 

快照

enter image description here

現在,如果你有X軸值 「希德,潰敗」 那麼上面將無法正常工作。對於這些場景,我創建了一個額外的功能。請參閱下面的代碼。

Sub FormatPoints() 
    Dim chr As ChartObject 
    Dim chrSeries As Series 
    Dim X As String, Y As String 
    Dim lngCnt As Long 
    Dim pnt As Point 
    Set chr = ActiveSheet.ChartObjects(1) 


    For Each chrSeries In chr.Chart.SeriesCollection 
     For Each pnt In chrSeries.Points 
      '~~> You need this line else the code will fail 
      pnt.DataLabel.ShowValue = True 

      pnt.DataLabel.ShowCategoryName = True 

      X = GetVal(pnt.DataLabel.Caption, "X") 
      Y = GetVal(pnt.DataLabel.Caption, "Y") 

      pnt.DataLabel.ShowCategoryName = False 

      MsgBox "X Value :" & X & vbNewLine & "Y Value :" & Y 
     Next 
    Next 
End Sub 

Function GetVal(DataPointCaption As String, strAxis As String) As String 
    Dim TempAr() As String 

    TempAr = Split(DataPointCaption, ",") 

    If strAxis = "Y" Then GetVal = TempAr(UBound(TempAr)) 
    If strAxis = "X" Then 
     For i = LBound(TempAr) To (UBound(TempAr) - 1) 
      GetVal = GetVal & "," & TempAr(i) 
     Next i 
     GetVal = Mid(GetVal, 2) 
    End If 
End Function 

快照

enter image description here

HTH

希德

+0

優秀的回覆。正是我在找什麼。謝謝! – 2012-03-09 09:28:36

3

像這樣的事情會做的伎倆

我有點驚訝,我可以通過VBA訪問每個圖表系列的每個Point,但該Point沒有直接的價值。解決方法是對整個圖表系列轉儲到一個變型中,該陣列爲超測試的條件在測試每個值,則使用格式化PointchrSeries.Points(lngCnt)

Sub FormatPoints() 
    Dim chr As ChartObject 
    Dim chrSeries As Series 
    Dim X As Variant 
    Dim lngCnt As Long 
    Set chr = ActiveSheet.ChartObjects(1) 
    For Each chrSeries In chr.Chart.SeriesCollection 
     X = chrSeries.Values 
     For lngCnt = 1 To UBound(X) 
      If X(lngCnt) > 10 Then 
       With chrSeries.Points(lngCnt) 
        .MarkerBackgroundColor = vbRed 
        .MarkerForegroundColor = vbBlue 
       End With 
      End If 
     Next 
    Next 
End Sub 

sample

+0

在你上面的例子,它完美的作品,但是,如果我想測試對A,B ,c和d。說: 如果(pointLabel ==「A」) 我覺得有一點標籤之間混亂的一點點,我的問題打勾標籤,因爲我想在訪問標籤{點 的 編輯顏色}與該系列上的點相關的x軸。 – 2012-03-02 14:10:31

+0

@ColinDawson因此,如果圖表上的數據標籤(例如,紅線第一個點上的我的值爲11)與x軸匹配,則需要格式化數據標籤(而不是點) - 或實際的x系列標籤爲根據您的評論以上?你的照片可能會幫助 – brettdj 2012-03-03 00:27:19

+1

而不是'X = chrSeries.Values'使用'X = chrSeries.XValues'和'If X(lngCnt)=「a」Then'。 – 2012-03-03 08:32:05