简体   繁体   中英

Excel-VBA - Colour X-axis point if LIKE *Value*

Basically I have a chart that is created dynamically from Java (using POI) and I am passing through values that will allow specific points to be coloured in the chart.

To do this I need to access the point value label so that I can test if the condition attribute is applicable per point value.

For example I have variables set for a seriesPointObject

  1. Series Name
  2. Point Value Name
  3. Condition
  4. Colour

My pseudo-code is as follows

   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

But I cannot access the point value label for each series. There must be linkage somewhere between point value label and series, but I just cant find it.

Is there any way I can get the point label text from the series object?

Something like this will do the trick

I was a little surprised that I could access each Point of each chart series via VBA, but that the Point did not have a direct value. The workaround was to dump the entire chart series into a variant array, test each value in the array for a condition exceeding test, then format that Point using chrSeries.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

样品

In your example above, it works perfectly, however, what if I want to test against the a,b,c and d. to say: if( pointLabel == "a" ){ Edit colour of point } I think there is a little bit of confusion between point label and tick label in my question as I want to access the label on the x axis that is related to the point on the series.

Hello Colin

To access the datavalue or the point label of a data point, you have to first loop through each data points and then retrieve the values.

Dave has already given you a method to retrieve the Y values. Here is another method with which you can get both X Values and Y values.

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

EDIT

The above code will fail if the data points are not visible. You can use this code as well.

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

Snapshot

在此处输入图片说明

Now If you have X Axis values as " Sid, Rout " then the above will not work. For these scenarios, I created an extra function. See the code below.

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

Snapshot

在此处输入图片说明

HTH

Sid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM