Chart - Legend Formatting

jbeaucaire

Well-known Member
Joined
May 8, 2002
Messages
6,012
This question embarrassingly demonstrates my lack of time spent in the "Charts" arena.

On the sample workbook is an X/Y Scatter plot chart. What I would like to do by hand or by VBA:

1) Reduce the legend to only unique values
* Data1
* Data2
* Data3
etc...

2) Have all the dots of the same data type in the chart be matching in color.
Data1 = red
Data2 = green
Data3 = yellow
etc...


Cross-posted here with the workbook sample for convenience of the issue:
Chart - Legend Formatting

If this issue is resolved I will certainly and immediately update both threads with the solution and closure.

Thanks in advance.
 
Hello Jerry

Can you post a public link to the workbook? I cannot download from Excel Forum.
 
Upvote 0
Hi Jerry

I see you got a solution at Excel Forum. Would you mind posting it here?
Only members can view it there.
 
Upvote 0
I'm still poking at this to understand how it causes the LEGEND to reduce the number of entries, but this wonder little macro from xlnitwit accomplishes both tasks quite neatly, merging the like items into a single colored item in a smaller legend.
Code:
Sub formatChart()

    Dim dic As Object
    Dim n As Long
    Dim theChart As Chart
    Dim theSeries As Series
    
    Set theChart = ActiveSheet.ChartObjects(1).Chart
    Set dic = CreateObject("Scripting.Dictionary")
    
    With theChart
        For n = .SeriesCollection.Count To 1 Step -1
            Set theSeries = .SeriesCollection(n)
            If dic.exists(theSeries.Name) Then
                .Legend.LegendEntries(n).Delete
            Else
                dic(theSeries.Name) = ActiveWorkbook.Colors(dic.Count + 1)
            End If
            With theSeries.Format
                With .Fill
                    .BackColor.RGB = dic(theSeries.Name)
                    .ForeColor.RGB = dic(theSeries.Name)
                End With
                With .Line
                    .BackColor.RGB = dic(theSeries.Name)
                    .ForeColor.RGB = dic(theSeries.Name)
                End With
            End With
        Next n
    End With
    Set dic = Nothing
End Sub
 
Upvote 0
I added a few comments:

Code:
Sub formatChart()
Dim dic As Object, n%, theChart As Chart, theSeries As Series
Set theChart = ActiveSheet.ChartObjects(1).Chart
Set dic = CreateObject("Scripting.Dictionary")                        ' could also be a collection
With theChart
    For n = .SeriesCollection.Count To 1 Step -1                       ' all series
        Set theSeries = .SeriesCollection(n)                            ' one series
        If dic.exists(theSeries.Name) Then
            .Legend.LegendEntries(n).Delete                             ' exclude duplicate entry
        Else
            dic(theSeries.Name) = ActiveWorkbook.Colors(dic.Count + 1)  ' add color to dictionary
        End If
        With theSeries.Format                                           ' one series
            With .Fill
                .BackColor.RGB = dic(theSeries.Name)                    ' apply color
                .ForeColor.RGB = dic(theSeries.Name)
            End With
            With .Line
                .BackColor.RGB = dic(theSeries.Name)                    ' apply color
                .ForeColor.RGB = dic(theSeries.Name)
            End With
        End With
    Next
End With
Set dic = Nothing
End Sub
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top