4

I have a chart on excel with a curve. The curve represents the price of a stock depending of the time (so basically I've a column A full of dates and Column B full of prices and a chart).

Now for each point in the chart there is a datalabel text as followed

Charts("CHART").SeriesCollection(1).Points(Numpoint).DataLabel.Text = numcomptagesetup & vbCrLf & numcomptagecountdown

"numpoint" is a variable that goes from point 1 to the last one. "numcomptagesetup" and "numcomptagecountdown" are values that goes from 1 to 6 for "numcomptagesetup" and from 1 to 15 for "numcomptagecountdown".

The code above is writing "numcomptagesetup" then jumping two lines then writing "numcomptagecountdown"

What I need is, depending on 2 signals, to change the color of "numcomptagesetup" in green or red and the color of "numcomptagecountdown" in green and red too. So in some cases, I'll need to have two different colors in the same datalabeltext.

I've written this loop and it works at least at the beginning (step by step method) then everything change and the colors aren't at the good place (red instead of green or the contrary) ... Really strange By default, all the points are green, then i change the color of the point to red when I want :

 For numpoint=1 to 100
With Charts("CHART").SeriesCollection(1).Points(Numpoint)
            .HasDataLabel = True
            .DataLabel.Text = numcomptagesetup & vbCrLf & numcomptagecountdown
            .DataLabel.Font.Size = 6
            .DataLabel.Font.Color = vbRed
            .MarkerSize = 5
End With
If Signal = 2 Then Charts("CHART").SeriesCollection(1).DataLabels.Select 
  With Charts("CHART").SeriesCollection(1).Points(Numpoint).DataLabel.Format.TextFrame2.TextRange.Characters(1, 3).Font.Fill 
    .Visible = msoTrue 
    .ForeColor
    .RGB = RGB(0, 176, 80) .Solid 
  End With 
End If

If Signal2 = 2 Then
    Charts("CHART").SeriesCollection(1).DataLabels.Select
    With Charts("CHART").SeriesCollection(1).Points(Numpoint).DataLabel.Format.TextFrame2.TextRange.Characters(4, 1).Font.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(0, 176, 80)
        .Solid
    End With
End If

I hope you understand my problem ! If not you can ask, i'll answer as quickly as I can

Thank you!!

4

2 回答 2

2

我被这件事难住了。甚至宏记录器也给了我类似于你的代码,但是当我运行它时,颜色会应用于Series.DataLabels 中的所有数据标签。

我有一个可能的解决方法,就是将 TextBox 形状添加到图表中,并将它们直接放置在数据标签的顶部。然后,删除最后的数据标签。

'## This is from your original code:
For numpoint=1 to 100
    With Charts("CHART").SeriesCollection(1).Points(Numpoint)
        .HasDataLabel = True
        .DataLabel.Text = numcomptagesetup & vbCrLf & numcomptagecountdown
        .DataLabel.Font.Size = 6
        .DataLabel.Font.Color = vbRed
        .MarkerSize = 5
    End With
Next

'## Now, I make some changes:
Dim srs As Series
Dim dl As DataLabel
Dim dlText As String

Set srs = Charts("CHART").SeriesCollection(1) '## I find it easier to work with objects, so I use variable "srs"

'## Delete any textboxes that might be leftover from previous.
For Each shp In cht.Shapes
    shp.Delete
Next

For Each dl In srs.DataLabels  '## object-oriented programming, use a DataLabel object variable :
    dlText = dl.Caption
    '## Add a textbox to overlay the datalabel
    Set tb = cht.Shapes.AddTextbox(msoTextOrientationHorizontal, dl.Left, dl.Top, dl.Width, dl.Height)
    With tb.TextFrame2
        .VerticalAnchor = msoAnchorMiddle
        .WordWrap = msoFalse
        .AutoSize = msoAutoSizeShapeToFitText
        With .TextRange
            .Text = dlText
            .Font.Size = dl.Format.TextFrame2.TextRange.Font.Size
            '## Apply character color format to the textbox:
            If Signal = 2 Then
                With .Characters(, 3).Font.Fill
                    .Visible = msoTrue
                    .ForeColor.RGB = RGB(0, 176, 80)
                    .Solid
                End With
            End If
            '## Apply character color format to the textbox:
            If Signal2 = 2 Then
                With .Characters(4, 1).Font.Fill
                    .Visible = msoTrue
                    .ForeColor.RGB = RGB(0, 176, 80)
                    .Solid
                End With
            End If
        End With
    End With
Next

'## Finally, turn off the datalabels that you don't need anymore.
srs.HasDataLabels = False
于 2013-06-13T15:22:25.313 回答
0

i've come up with something else :

 With ChartObj.Chart.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange.Characters.Font.fill                                    
         .TwoColorGradient msoGradientDiagonalUp, 1
         .GradientAngle = 45
         .GradientStops(1).Position = 0 '.2
         .GradientStops(2).Position = 1 '0.8
         .ForeColor.RGB = RGB(0, 0, 0)
         .BackColor.RGB = RGB(255, 255, 255)
 End With

wich gives me a gradient color for the datalabels.

于 2017-06-30T23:18:07.287 回答