EirikDaude
05-08-2013, 12:16 AM
Hi! I've come up with the following code to label the datapoints on a chart:
Sub test()
Call merkSistePunkt(Worksheets("Utvikling").ChartObjects("Kortsone3").Chart, "Opptak/botnsuging")
Call merkSistePunkt(Worksheets("Utvikling").ChartObjects("Kortsone3").Chart, "Nedsuging")
Call merkSistePunkt(Worksheets("Utvikling").ChartObjects("Kortsone3").Chart, "Toppsuging")
End Sub
Sub merkSistePunkt(graf As Chart, handling As String)
Dim iPts As Integer, mySrs As Series, dataOffset As Integer
Set mySrs = graf.SeriesCollection(handling)
Select Case graf.Name
Case "Kortsone3": dataOffset = 0
Case "Langsone3": dataOffset = 7
Case "Kortsone4": dataOffset = 14
Case "Langsone4": dataOffset = 21
End Select
Select Case handling
Case "Toppsuging": dataOffset = dataOffset
Case "Nedsuging": dataOffset = dataOffset + 2
Case "Opptak/botnsuging": dataOffset = dataOffset + 4
End Select
With mySrs
.HasDataLabels = True
For iPts = 1 To .DataLabels.Count
.DataLabels(iPts).Text = Worksheets("Grafverdiar").Range("B4").Offset(iPts - 1, dataOffset).Value
Next
End With
End Sub Now, as far as I can see this code works quite well - that is I do get the datapoints labelled. However, I get a bit more info in my chart than I strictly want. If it is possible I only want the last point in each dataseries to be labelled. To complicate things further, the series don't necessarily have the same amount of "real" values. See this picture for how the graph and datavalues typically will look:
http://i.imgur.com/EpEjLfh.png
So to sum up, what I am curious to know is if there's any way to find and label just the last point of the series?
Sub test()
Call merkSistePunkt(Worksheets("Utvikling").ChartObjects("Kortsone3").Chart, "Opptak/botnsuging")
Call merkSistePunkt(Worksheets("Utvikling").ChartObjects("Kortsone3").Chart, "Nedsuging")
Call merkSistePunkt(Worksheets("Utvikling").ChartObjects("Kortsone3").Chart, "Toppsuging")
End Sub
Sub merkSistePunkt(graf As Chart, handling As String)
Dim iPts As Integer, mySrs As Series, dataOffset As Integer
Set mySrs = graf.SeriesCollection(handling)
Select Case graf.Name
Case "Kortsone3": dataOffset = 0
Case "Langsone3": dataOffset = 7
Case "Kortsone4": dataOffset = 14
Case "Langsone4": dataOffset = 21
End Select
Select Case handling
Case "Toppsuging": dataOffset = dataOffset
Case "Nedsuging": dataOffset = dataOffset + 2
Case "Opptak/botnsuging": dataOffset = dataOffset + 4
End Select
With mySrs
.HasDataLabels = True
For iPts = 1 To .DataLabels.Count
.DataLabels(iPts).Text = Worksheets("Grafverdiar").Range("B4").Offset(iPts - 1, dataOffset).Value
Next
End With
End Sub Now, as far as I can see this code works quite well - that is I do get the datapoints labelled. However, I get a bit more info in my chart than I strictly want. If it is possible I only want the last point in each dataseries to be labelled. To complicate things further, the series don't necessarily have the same amount of "real" values. See this picture for how the graph and datavalues typically will look:
http://i.imgur.com/EpEjLfh.png
So to sum up, what I am curious to know is if there's any way to find and label just the last point of the series?