Reputation: 11
I am using a loop to add series to a chart based on column name
With ds.Tables("MyTable")
Dim i As Integer = 0
For Each column As DataColumn In .Columns
If column.ColumnName Like "S1_*" Then
Chart2.Series.Add(column.ColumnName)
Chart2.Series(column.ColumnName).YValueMembers = column.ColumnName
Chart2.Series(column.ColumnName).ChartType = DataVisualization.Charting.SeriesChartType.Line
Chart2.Series(column.ColumnName).BorderWidth = 3
Chart2.Series(column.ColumnName).XValueMember = .Columns("DateTime").ToString
Chart2.Series(column.ColumnName).IsXValueIndexed = True
End If
Next
When changing the series names manually everything works fine
With Chart2
.Series(0).Name = "Series Name 1"
.Series(1).Name = "Series Name 2"
.Series(2).Name = "Series Name 3"
End With
The problem occurs when i change the way the series names are changed. If i change the series names within a loop
For i = 0 To ds.Tables("Qs").Rows.Count - 1
For x = 0 To Chart2.Series.Count - 1
If Chart2.Series(x).Name = ds.Tables("Qs").Rows(x).Item("Q_Name") Then
Chart2.Series(x).Name = ds.Tables("Qs").Rows(x).Item("Q_Text")
End If
Next
Next
I get the following error when viewing the chart
An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.DataVisualization.dll
Additional information: Cannot display indexed series (XValueIndexed = true) on the same axis if they are not aligned.A chart element with the name 'Series Name 1' could not be found in the 'SeriesCollection'.
The Series names are being updated correctly within in the loop and i can see teh chart if I remove the Chart2.Series(column.ColumnName).IsXValueIndexed = True. The only difference I can see is how the series names are being changed.
All help / comments are appreciated
Upvotes: 1
Views: 1365
Reputation: 19
Visual Basic starts indexing at 1. Try replacing
.Series(0).Name = "Series Name 1"
with
.Series(1).Name = "Series Name 1"
and likewise for the other series.
Upvotes: 0