+ Reply to Thread
Results 1 to 11 of 11

Chart - Legend Formatting

Hybrid View

  1. #1
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Chart - Legend Formatting

    This question embarrassingly demonstrated 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...

    Thanks in advance.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Chart - Legend Formatting

    I think you need to rearrange the date so that Data1 through Data12 are separate series. That would make each series name appear once, and all points within each series the same.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Chart - Legend Formatting

    Ok, that very simple answer should be enough, and I don't even know what I don't know. Totally Chart clueless. Trying to avoid hours and hours of study to learn something I doubt I'll use again for another year or two after I finish this ask.

    Hopeful...if it's any help, the names of the data1, data2, etc labels is a known quantity, so VBA could be told what values to look for and what color to apply...
    Last edited by JBeaucaire; 11-13-2016 at 07:41 PM.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Chart - Legend Formatting

    If you're clueless, I'm a scant 1/4 step ahead of you.

    You might ask on MrE; Jon Peltier has been posting there frequently of late.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Chart - Legend Formatting

    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.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,907

    Re: Chart - Legend Formatting

    What do you want to do where there are multiple entries for the same x value and the same data group? For example, B2 and B3 are both 6, corresponding x is 1 and the data group is data1.

    Here's what I did to start:

    A116 enter data1. continue down with each unique data group (I am expecting that you already have a utility that will extract unique entries from a list).
    B116=SUMIFS(B$2:B$114,$A$2:$A$114,$A116) -- assuming you want to sum the values for each data group. This could just as easily be AVERAGE() or MAX() or MIN() or something more complicated.
    copy B116 down and across. This gives a single row for each data group with the desired y values for each x value.
    Use this range as the source for the chart. Row 1 can still be the x_values for each data series. Each row in the rows below 116 becomes its own data range.

    I'm sure the same thing could be done in VBA, One way or another, the basic idea is like that -- figure out how to get the unique data groups in their own single row so that each data group has one data series.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Chart - Legend Formatting

    Hmm, not sure this is what they want. The XY scatter plot is already the exact plot display they want, so I presume the duplicate values in the table simply plot over the top of each other. They don't want the display to change, only the legend trimmed down to unique values and the like values colored the same. My lack of familiarity with charting and charting VBA puts me at the "ask" table.

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Chart - Legend Formatting

    Hi,

    Perhaps something like the attached code- for some reason I can not post the code inline?

    One might also use an array of specific colour values rather than the default workbook colours.

    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
    Attached Files Attached Files
    Last edited by JBeaucaire; 11-14-2016 at 11:22 AM.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Chart - Legend Formatting

    Quote Originally Posted by xlnitwit View Post
    Perhaps something like the attached code
    Nice work!

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Chart - Legend Formatting

    Yes, yes, yes! That did both things quite neatly. And I can tweak this from there to give them the code-coloring they want. Thanks so much.

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Chart - Legend Formatting

    Thank you both for the rep points and, perhaps more importantly, praise from MVPs! A fine day for me!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Use of subscript in chart legend
    By Awalgaonkar in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 02-06-2015, 05:39 PM
  2. Formatting a Chart Legend Key Excel 2003
    By Mogadeet in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 06-11-2013, 04:25 AM
  3. [SOLVED] Exclude one data series from scatter chart legend (but not from the chart)
    By JayUSA in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 05-29-2012, 05:24 PM
  4. Chart Legend
    By PCLIVE in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 05-04-2006, 09:10 PM
  5. Set Legend of a Chart from VBA?
    By Mike B in VT in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-30-2006, 11:45 AM
  6. pie chart legend
    By CM in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 10-17-2005, 07:05 PM
  7. Chart Legend
    By Larry in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 04-11-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1