Label Each Series in a Chart
by Jon Peltier
Peltier Technical Services, Inc., Copyright © 2009.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
In 9 Steps to Simpler Chart Formatting I suggested using data labels to identify each series rather than using a legend. I have a small VBA procedure that I use for this. It labels the last point of each series, and removes other labels. It also has an error trap that skips points that are not plotted because of blank cells or #N/A errors.
Sub LabelLastPoint()
Dim mySrs As Series
Dim iPts As Long
Dim bLabeled As Boolean
If ActiveChart Is Nothing Then
MsgBox "Select a chart and try again.", vbExclamation
Else
For Each mySrs In ActiveChart.SeriesCollection
bLabeled = False
With mySrs
For iPts = .Points.count To 1 Step -1
If bLabeled Then
' handle error if point isn't plotted
On Error Resume Next
' remove existing label if it's not the last point
mySrs.Points(iPts).HasDataLabel = False
On Error GoTo 0
Else
' handle error if point isn't plotted
On Error Resume Next
' add label
mySrs.Points(iPts).ApplyDataLabels _
ShowSeriesName:=True, _
AutoText:=True, LegendKey:=False
bLabeled = (Err.Number = 0)
On Error GoTo 0
End If
Next
End With
Next
ActiveChart.HasLegend = False
End If
End Sub
To implement this procedure, follow the steps in How To: Use Someone Else’s Macro.
Possibly Related Posts:
- Connect Two XY Series with Arrows (VBA)
- Order of Points in XY and Line Charts
- VBA to Split Data Range into Multiple Chart Series
- Extract Chart Data
- Enhanced Export Chart Procedure
- Quick VBA Routine: XY Chart with Axis Titles
- Chart Event to Highlight a Series
- Chart Event Class Module to Highlight a Series
Posted: Wednesday, October 15th, 2008 under VBA.
Comments: 8
Comments
I welcome comments from my readers. If you have an opinion on this post, if you have a question or if there is anything to add, I want to hear from you. Whether you agree or disagree, please join the discussion.
Read the PTS Blog Comment Policy.
Comment from Darlene
Time: Thursday, October 16, 2008, 3:49 pm
Hi Jon, me again…I’m sure this is probably a really easy answer but I cannot figure out how to chart the following. I have one series that is a total of 11,175 and the smallest series is a total of 79. I cannot get the 79 to show up because the Y axis is from 0 to 11,175. Any suggestions?
Darlene
Comment from Jon Peltier
Time: Thursday, October 16, 2008, 4:04 pm
Do you mean you can’t see the data because it’s too close to zero? What kind of chart is it? are the curves plotting the same kind of information?
To get around this problem you could use a logarithmic scale, put a break in the axis, or plot the curves in two different panels of a panel chart.
Comment from Jaanus
Time: Tuesday, October 21, 2008, 8:19 am
I would then add
ActiveChart.HasLegend = False
to the very end to remove the now redundant chart legend.
Comment from Jon Peltier
Time: Tuesday, October 21, 2008, 3:09 pm
Jaanus - Good idea. I missed it because I probably allready deleted the legend, but I’ve added it to the code above.
Comment from Petr
Time: Wednesday, October 22, 2008, 5:06 am
Very nice, Jon.
Still, have you ever pondered on a greater luxury: automatic connecting line between (the last) point and its label? What is your tentative opinion - would it be even soluble by VBA means?
Petr
Comment from Jon Peltier
Time: Wednesday, October 22, 2008, 7:45 am
Petr -
IMO, leader lines do not add to a chart, they seem to add clutter. If you need leader lines to clarify which label corresponds to which series, you may be dealing with excessive clutter. When I am almost cluttered enough to use leader lines, I try to rely instead on coloring the label text to match the series format.
That said, you could in fact use VBA to simplify the task of adding leader lines. I would extend the series by one point, remove the last marker and change the last line segment to a leader line kind of format (thin line, different format from the series lines) and center the label on the added point. You could do this manually, and see if you like it, if so, automate it.
Comment from Stružák
Time: Friday, October 31, 2008, 5:44 pm
What about adding Application.ScreenUpdating = False at the beggining of the macro and Application.ScreenUpdating = True at the end? Not a crucial thing, but it might speed up the procedure.
Comment from Jon Peltier
Time: Friday, October 31, 2008, 8:07 pm
Stružák -
Good point. I usually remember. In this case it probably wouldn’t make too much difference, but it’s a good habit to get into.















Write a comment