ASP.NET MS 图表控件:数据点插入错误。这个数据系列只能设置2个Y值。参数名称:数据源
ASP.NET MS chart control: Data points insertion error. Only 2 Y values can be set for this data series. Parameter name: dataSource
我的 MS 图表控件出现此错误:
Data points insertion error. Only 2 Y values can be set for this data series.
Parameter name: dataSource
它出现在我下面代码的 chartPriceHistory_STATIC.DataBind()
行。
我认为这与我添加点的方式有关(AddXY
),但无法弄清楚它是什么。
我尝试了这两个代码选项:
选项 1
chartPriceHistory_STATIC.Series(seriesName).Points.AddXY(get3LetterMonth(CDate(row("createdate")).Month) + "-" + CDate(row("createdate")).Year.ToString, {CType(row("price"), Integer), totalobjects})
选项 2
chartPriceHistory_STATIC.Series(seriesName).Points.AddXY(get3LetterMonth(CDate(row("createdate")).Month) + "-" + CDate(row("createdate" )).Year.ToString, CType(行("price"), 整数))<br>
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 方法尝试通过新 class 创建点并将它们添加到图表
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);
}
您还需要为 "Avg price" 系列设置 YValueMembers
。
添加这一行(使用您想要在 Y 轴上绘制的任何字符串):
chartPriceHistory_Static.Series(seriesName).YValueMembers = "totalobjects"
在这一行之前添加:
chartPriceHistory_Static.Series(seriesName).YValuesPerPoint = 2
此外,您的 date/createdate 列名称不一致 - 在更正之前您将看不到图表。
如果您只添加 1 个 YValue,您可以再次将 YValuesPerPoint 降低到 1,不会出错。
已测试。工作正常。干杯!
我的 MS 图表控件出现此错误:
Data points insertion error. Only 2 Y values can be set for this data series. Parameter name: dataSource
它出现在我下面代码的 chartPriceHistory_STATIC.DataBind()
行。
我认为这与我添加点的方式有关(AddXY
),但无法弄清楚它是什么。
我尝试了这两个代码选项:
选项 1
chartPriceHistory_STATIC.Series(seriesName).Points.AddXY(get3LetterMonth(CDate(row("createdate")).Month) + "-" + CDate(row("createdate")).Year.ToString, {CType(row("price"), Integer), totalobjects})
选项 2
chartPriceHistory_STATIC.Series(seriesName).Points.AddXY(get3LetterMonth(CDate(row("createdate")).Month) + "-" + CDate(row("createdate" )).Year.ToString, CType(行("price"), 整数))<br>
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 方法尝试通过新 class 创建点并将它们添加到图表
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);
}
您还需要为 "Avg price" 系列设置 YValueMembers
。
添加这一行(使用您想要在 Y 轴上绘制的任何字符串):
chartPriceHistory_Static.Series(seriesName).YValueMembers = "totalobjects"
在这一行之前添加:
chartPriceHistory_Static.Series(seriesName).YValuesPerPoint = 2
此外,您的 date/createdate 列名称不一致 - 在更正之前您将看不到图表。
如果您只添加 1 个 YValue,您可以再次将 YValuesPerPoint 降低到 1,不会出错。
已测试。工作正常。干杯!