Page 1 of 1
Reference a Series by Name?
Posted: Mon Mar 12, 2012 6:03 am
by 13049545
I need to add an unknown number of series dynamically --
Then I need to add data to this series (preferably by series name) --
Is there an elegant way to do this without creating a bunch of line objects and referencing those?
Example:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objLine As Steema.TeeChart.Styles.Line
Dim intNumberOfSeries As Integer
Randomize()
'Between 1 and 5 random
intNumberOfSeries = CInt(Math.Floor((5 - 1 + 1) * Rnd())) + 1
'Assume there's already a bunch of series on the chart plus the ones being added below
'Add Series
For IntCnt = 0 To Rnd(5)
objLine = New Steema.TeeChart.Styles.Line
objLine.Title = "Series " + IntCnt.ToString()
Me.TChart1.Chart.Series.Add(objLine)
Next
'Now Populate Series At Random
Me.TChart1.Chart.Series("Series 3").Add(0, 0)
End Sub
Thanks,
Joseph
Re: Reference a Series by Name?
Posted: Tue Mar 13, 2012 11:41 am
by 10050769
Hello jzarech,
I have made a simple code that I think can help you to achieve as you want:
Code: Select all
Private Sub InitializeChart()
tChart1.Series.Clear()
Dim rnd As New Random()
Dim CountRandom As Integer = rnd.[Next](10)
For i As Integer = 0 To CountRandom
TChart1.Series.Add(New Steema.TeeChart.Styles.Line)
Next
For Each s As Steema.TeeChart.Styles.Line In TChart1.Series
If s.Count > 1 Then
If s.Title = "line2" OrElse s.Title = "line3" Then
s.FillSampleValues()
Else
s.FillSampleValues(3)
End If
Else
s.FillSampleValues()
End If
Next
End Sub
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
InitializeChart()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
InitializeChart()
End Sub
Can you tell us if previous code works as you expect?
I hope will helps.
Thanks,
Re: Reference a Series by Name?
Posted: Sun Mar 18, 2012 1:44 pm
by 15661493
Hope you don't mind a suggestion from a forum user...
A hashtable is a very handy place to store a list of series. Also, the Series.Tag property is a great place to save a name so you can keep track of things. Here are a few simple routines that will let you add the series and remove them. If you happen to put tools, lines, etc. on the series and want to remove them too, set their tag properties to begin with the series name, too.
Imports Steema.TeeChart
Public Class Form1
Private SeriesHT As New Hashtable
Private tChart1 As Steema.TeeChart.Chart
Private Function SetSeriesInHashtable(S As Steema.TeeChart.Styles.Series, SeriesName As String) As Boolean
Dim bResult As Boolean = True
Try
S.Tag = SeriesName 'Tag Property is a great place to store the name if you need to.
'If you will be adding tools, lines or whatever to the series charts, consider setting the Tools.Tag to begin with the series name.
'Then when you are removing the series, you can also loop through all the tools you have created
'and remove them at the same time.
SeriesHT.Add(SeriesName, S)
Catch ex As Exception
bResult = False
End Try
Return bResult
End Function
Private Function GetSeriesFromHashTable(SeriesName As String) As Steema.TeeChart.Styles.Series
'This makes it very easy to grab the series you want.
Dim S As Steema.TeeChart.Styles.Series = Nothing
Try
If SeriesHT.Contains(SeriesName) Then
S = SeriesHT(SeriesName)
End If
Catch ex As Exception
End Try
Return S
End Function
Private Function RemoveSeries(SeriesName As String) As Boolean
'When you are all done, this will remove tools and series from the chart.
'It also clears the series from the hashtable.
Dim bResult As Boolean = True
Dim S As Steema.TeeChart.Styles.Series = Nothing
Try
If SeriesHT.Contains(SeriesName) Then
S = SeriesHT(SeriesName)
'if you saved any tools with the SeriesName as the tag, remove them.
'Step backwards through the enumrated tools while doing so.
For x As Integer = tChart1.Tools.Count - 1 To 0 Step -1
Dim tTag As String = T.Tag
If tTag.StartsWith(SeriesName) Then
tChart1.Tools.RemoveAt(x)
End If
Next
For Each tSeries As Steema.TeeChart.Styles.Series In tChart1.Series
If tSeries.Tag = SeriesName Then
tChart1.Series.Remove(tSeries)
End If
Exit For
Next
SeriesHT.Remove(S)
End If
Catch ex As Exception
End Try
Return bResult
End Function
End Class
Re: Reference a Series by Name?
Posted: Mon Mar 19, 2012 12:26 pm
by 10050769
Hello DaveR,
Thanks for your suggestion. We have added in in wish-list to be consider its inclusion in future versions TeeChart.Net
Thanks,