I'm getting this error on my MS Chart control:
Data points insertion error. Only 2 Y values can be set for this data series. Parameter name: dataSource
It occurs on line chartPriceHistory_STATIC.DataBind()
in my code below.
I think it has got something to do with the way I'm adding points (AddXY
) but can't figure out what it is.
I tried these 2 code options:
Option 1
chartPriceHistory_STATIC.Series(seriesName).Points.AddXY(get3LetterMonth(CDate(row("createdate")).Month) + "-" + CDate(row("createdate")).Year.ToString, {CType(row("price"), Integer), totalobjects})
Option 2
chartPriceHistory_STATIC.Series(seriesName).Points.AddXY(get3LetterMonth(CDate(row("createdate")).Month) + "-" + CDate(row("createdate")).Year.ToString, CType(row("price"), Integer))
chartPriceHistory_STATIC.Series("totalobjects").Points.AddXY(get3LetterMonth(CDate(row("createdate")).Month) + "-" + CDate(row("createdate")).Year.ToString, totalobjects)
Both throw the same error...what am I missing?
Dim mycommand As New SqlCommand("SELECT avgprice, createdate, totalobjects FROM avgprices", myConnection)
Dim dtPrices As New System.Data.DataTable
dtPrices.Columns.Add("price", System.Type.GetType("System.Int32"))
dtPrices.Columns.Add("createdate", System.Type.GetType("System.DateTime"))
dtPrices.Columns.Add("totalobjects", System.Type.GetType("System.Int32"))
Dim dr As System.Data.DataRow
Try
Dim reader As SqlDataReader = mycommand.ExecuteReader()
While reader.Read
dr = dtPrices.NewRow()
dr("price") = reader("price")
dr("createdate") = reader("createdate")
dr("totalobjects") = reader("totalobjects")
dtPrices.Rows.Add(dr)
End While
Catch ex As Exception
End Try
' Initializes a New instance of the DataSet class
Dim myDataSet As DataSet = New DataSet()
'Adds rows in the DataSet
myDataSet.Tables.Add(dtPrices)
chartPriceHistory_STATIC.Series.Clear()
Dim seriesName As String = "Avg price"
chartPriceHistory_STATIC.Series.Add(seriesName)
chartPriceHistory_STATIC.Series(seriesName).XValueMember = "Date"
chartPriceHistory_STATIC.ChartAreas.Add("ChartArea1")
chartPriceHistory_STATIC.Series(seriesName).YValuesPerPoint = 2
chartPriceHistory_STATIC.ChartAreas(0).AxisY.MajorGrid.Enabled = True
chartPriceHistory_STATIC.ChartAreas(0).AxisY.Title = "Price"
Dim totalobjects As Integer = 1
chartPriceHistory_STATIC.Series.Add("Series2")
chartPriceHistory_STATIC.Series("Series2").YAxisType = AxisType.Secondary
chartPriceHistory_STATIC.Series("Series2").XValueMember = "Date"
chartPriceHistory_STATIC.Series("Series2").YValueMembers = "totalobjects"
chartPriceHistory_STATIC.Series("Series2").Name = "totalobjects"
chartPriceHistory_STATIC.Series("totalobjects").ChartType = SeriesChartType.Line
chartPriceHistory_STATIC.Series("totalobjects").ToolTip = "Total objects"
chartPriceHistory_STATIC.Series(0).YAxisType = AxisType.Primary
chartPriceHistory_STATIC.Series(1).YAxisType = AxisType.Secondary
For Each row As DataRow In myDataSet.Tables(0).Rows
totalobjects += 1
'I tried: these 2 options, both generate the same error
chartPriceHistory_STATIC.Series(seriesName).Points.AddXY(get3LetterMonth(CDate(row("createdate")).Month) + "-" + CDate(row("createdate")).Year.ToString, {CType(row("price"), Integer), totalobjects})
chartPriceHistory_STATIC.Series(seriesName).Points.AddXY(get3LetterMonth(CDate(row("createdate")).Month) + "-" + CDate(row("createdate")).Year.ToString, CType(row("price"), Integer))
chartPriceHistory_STATIC.Series("totalobjects").Points.AddXY(get3LetterMonth(CDate(row("createdate")).Month) + "-" + CDate(row("createdate")).Year.ToString, totalobjects)
Next
chartPriceHistory_STATIC.DataSource = myDataSet
chartPriceHistory_STATIC.DataBind()
You need to set the YValueMembers
for the "Avg price" series, too.
Add this line (use whatever string you want to plot on Y axis):
chartPriceHistory_Static.Series(seriesName).YValueMembers = "totalobjects"
Add it just before this line:
chartPriceHistory_Static.Series(seriesName).YValuesPerPoint = 2
Also, your name of date/createdate column is inconsistent - you won't see the plots until you correct that.
If you are only adding 1 YValue, you can reduce the YValuesPerPoint down to 1 again, without error.
Tested. Works fine. Cheers!