ASP.NET MS图表控件:数据点插入错误。此数据系列只能设置2个Y值。参数名称:数据源

ASP.NET MS图表控件:数据点插入错误。此数据系列只能设置2个Y值。参数名称:数据源,asp.net,vb.net,data-visualization,microsoft-chart-controls,Asp.net,Vb.net,Data Visualization,Microsoft Chart Controls,我的MS图表控件出现以下错误: 数据点插入错误。此数据系列只能设置2个Y值。 参数名称:数据源 它出现在下面我的代码中的第行chartPriceHistory\u STATIC.DataBind() 我认为这与我添加点的方式有关(AddXY),但我不知道这是什么 我尝试了以下两种代码选项: 选择1 chartPriceHistory\u STATIC.Series(seriesName).Points.AddXY(get3LetterMonth(CDate(row(“createdate”)).

我的MS图表控件出现以下错误:

数据点插入错误。此数据系列只能设置2个Y值。 参数名称:数据源

它出现在下面我的代码中的第行
chartPriceHistory\u STATIC.DataBind()

我认为这与我添加点的方式有关(
AddXY
),但我不知道这是什么

我尝试了以下两种代码选项:

选择1
chartPriceHistory\u STATIC.Series(seriesName).Points.AddXY(get3LetterMonth(CDate(row(“createdate”)).Month)+“-”+CDate(row(“createdate”).Year.ToString,{CType(row(“price”),Integer,totalobjects})

选择2
chartPriceHistory\u 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)

两个都抛出相同的错误…我遗漏了什么

    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()

尝试通过新类创建点并将其添加到图表中,而不是使用Points.AddXY方法

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);

                }

您还需要为“平均价格”系列设置
YValueMembers

添加此行(使用要在Y轴上绘制的任何字符串):

将其添加到此行之前:

chartPriceHistory_Static.Series(seriesName).YValuesPerPoint = 2
此外,日期/createdate列的名称不一致-在更正之前,您将无法看到绘图

如果只添加1个YValue,则可以将YValuesPerPoint再次减少为1,而不会出现错误


测试。很好。干杯

您是否有重复的X值(日期)?系列你不能输入像x:1/1/2000 y2和x:1/1/12000 y:3这样的图表,但没有,我有7个唯一的值:“2017年7月”、“2017年9月”、“2017年10月”、“2017年11月”、“2017年12月”、“2018年1月”、“2018年2月”我不确定你在这里做什么……我想要2个y数据点……所以即使我给点添加2个值。y值,这是什么
seriesDetail.points.add(要点)
?你能根据我的代码示例给出一个示例吗?提前谢谢!:-)你尝试过我建议的解决方案吗?是的,但是我看到了我的第一条评论,如果你能根据我的要求和代码提供一个示例的话?我可能明天就可以做了,今天很忙。谢谢,它确实有效!我在上面的帖子中使用了代码,并添加了您的建议。顺便说一句:我还需要从我的代码中删除这一行
chartPriceHistory\u STATIC.DataSource=myDataSet
Yeah重新删除源代码-您在其中有重复的功能-您也可以删除迭代数据集并添加点的行。
chartPriceHistory_Static.Series(seriesName).YValuesPerPoint = 2