2

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()
Adam
  • 6,041
  • 36
  • 120
  • 208
  • probaly u have duplicate in X Value (Date) ? series. u can't type in chart like x:1/1/2000 y2 and x:1/1/12000 y:3 have no sense – Norbert Ziemniak Feb 28 '18 at 12:49
  • Double checked that, but no, I have 7 unique values: "jul-2017","sep-2017","oct-2017","nov-2017","dec-2017","jan-2018","feb-2018" – Adam Feb 28 '18 at 13:31

2 Answers2

2

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!

Chalky
  • 1,624
  • 18
  • 20
  • Thanks, it works indeed! I used the code in my post above and added your suggestions. btw: I also needed to remove this line from my code `chartPriceHistory_STATIC.DataSource = myDataSet` – Adam Mar 12 '18 at 09:27
  • Yeah re removal of source - you have duplicated functionality in there - you could alternatively get rid of the lines where you iterate the dataset and add the point. – Chalky Mar 12 '18 at 10:50
0

Instead of using Points.AddXY method try to create point through new class and add them to chart

foreach (var result in data)
                {
                    point = new DataPoint();
                    point.AxisLabel = result.XData;

                    point.YValues = new double[] { result.YData };

                    point.Color = result.Color;
                    seriesDetail.Points.Add(point);

                }
MCoder
  • 113
  • 5
  • I'm unsure what you're doing here..I want 2 Y data points...so even if I add 2 value to point.YValues, what is this `seriesDetail.Points.Add(point)`? Could you give an example based on my code sample? Thanks in advance! :-) – Adam Mar 04 '18 at 20:08
  • Have you try the solution I have suggested? – MCoder Mar 07 '18 at 15:54
  • Yes I have, however see my first comment, if you could provide a sample based on my requirements and code? – Adam Mar 09 '18 at 15:43
  • I may be able to do it tomorrow, busy today. – MCoder Mar 09 '18 at 16:06