Change the Legend Box Color in PowerPoint
-
Friday, February 18, 2011 10:51 AM
Change the Legend Box Colour in PowerPoint
I'm developing an office automation application in which, I'm creating chart using VB.NET into PowerPoint.
I have four (4) Bars on the chart. I'm using ChartType = 26 (just for reference)
It's giving me three (3) Bars on the chart and using some excel data, I'm adding one more Bar into it.
The default color for charts are as,
Blue, Green, White, Blank
But for some reason, I'm changing the colors of the 3rd and 4th Bar, as below,
Blue, Green, Red, Yellow
This is working fine. The 3rd and 4th Bar is changing it's color.
But I also have legend below mentioning the description of the Bars.
The color of 3rd and 4th Legend Box is not changing.
As I have already change the color of Bars, the legend box's color also should get changed.
How can I change it? Any idea please?
Thanks
Naimish
All Replies
-
Friday, February 18, 2011 8:07 PM
Hi
You haven't given us any clues on how your changing the format of the bars, could you include a sample?
There are two ways to change the format of the bars
the whole data series
Activechart.SeriesCollection(x).Format.fill
or as data points
Activechart.SeriesCollection(x).Point(z).Format.Fill
If you use the second method the legend will not change colour even if all the points are formatted to the same colour.
Hope this helps
G North MMI- Marked As Answer by Naimish Pandya Monday, February 21, 2011 10:18 AM
-
Monday, February 21, 2011 6:42 AM
Hi,
I have 4 Bar on my chart (added 2 extra).
I'm formatting the 3rd and 4th Bar as below:
Dim colors As New Dictionary(Of Int16, Integer)(10)
colors.Add(1, RGB(0, 89, 196))
colors.Add(2, RGB(0, 255, 0))
colors.Add(3, RGB(255, 0, 0))
colors.Add(4, RGB(255, 215, 45))
colors.Add(5, RGB(0, 176, 240))
colors.Add(6, RGB(93, 182, 44))
colors.Add(7, RGB(0, 176, 240))
colors.Add(8, RGB(234, 234, 234))
Dim points As PowerPoint.Points = Nothing
points = CType(Me.ChartsOnDeck(GraphID).Item1.SeriesCollection, PowerPoint.SeriesCollection).Item(3).Points
Dim idx1 As Integer
idx1 = 0
If points IsNot Nothing Then
For idx1 = 1 To points.Count
points.Item(idx1).Format.Fill.BackColor.SchemeColor = PowerPoint.PpColorSchemeIndex.ppBackground
points.Item(idx1).Format.Fill.BackColor.RGB = colors(3)
Next
End If
points = CType(Me.ChartsOnDeck(GraphID).Item1.SeriesCollection, PowerPoint.SeriesCollection).Item(4).Points
idx1 = 0
If points IsNot Nothing Then
For idx1 = 1 To points.Count
points.Item(idx1).Format.Fill.BackColor.SchemeColor = PowerPoint.PpColorSchemeIndex.ppBackground
points.Item(idx1).Format.Fill.BackColor.RGB = colors(4)
Next
End If
But once I have formated my 3rd Bar and 4th Bar, the color of the 3rd legend and 4th legend is not changing. It should change depends on this right?
Any help?
Thanks
-
Monday, February 21, 2011 9:10 AM
Hi
No. You are changing the points within the series. The legend colour is controlled SeriesCollection itself.
Even if you set all the points to the same colour the legend will always display the series colour defined by something like this (not sure if I have everything in the right place)
Me.ChartsOnDeck(GraphId).Item(1).SeriesCollection(x).Format.Fill.BackColor.rgb = colors(3)
The easiest way to see it is to try formatting the bars manually on a chart. If you click a series all the bars are selected and changing the colour changes the legend.
However if you click one of the bars in the series again(a data point) only the clicked bar is selected. Changing it's colour has no impact on the legend. If you click all the bars in turn and format their colour, when you have finished the legend will still show the original colour.
Depending on which version of PowerPoint you are in, some of the older versions did allow you to select the legend colour box and set it's colour, but this was interpreted as formatting the series colour.
Hope this helps
G North MMI- Marked As Answer by Naimish Pandya Monday, February 21, 2011 10:18 AM
-
Monday, February 21, 2011 10:19 AM
The solution code:
If GrphSegShr1_Rows.Count > 3 Then
Dim objSeriesCollection = DirectCast(objChart.SeriesCollection, PowerPoint.SeriesCollection)
Dim dataLables As PowerPoint.DataLabels
For index = 1 To objSeriesCollection.Count
objSeriesCollection.Item(index).ApplyDataLabels(PowerPoint.XlDataLabelsType.xlDataLabelsShowValue)
dataLables = DirectCast(objSeriesCollection.Item(index).DataLabels, PowerPoint.DataLabels)
dataLables.Format.TextFrame2.TextRange.Font.Size = 6
dataLables.Format.TextFrame2.TextRange.Font.Bold = MsoTriState.msoTrue
dataLables.NumberFormat = "#,##0.00"
objSeriesCollection.Item(index).Format.Fill.BackColor.SchemeColor = PowerPoint.PpColorSchemeIndex.ppBackground
objSeriesCollection.Item(index).Format.Fill.BackColor.RGB = colors(index)
Next
dataLables = Nothing
objSeriesCollection = Nothing
End If
'Thanks for your help G North :)

