我有一个Chart Control,我试图用sqldatasource中的一些系列动态填充,这是我的图表控件:
<asp:Chart ID="chartMonthly" runat="server" Width="920px" ImageLocation="~/Images/Temp/ChartPic_#SEQ(300,3)" Palette="Berry">
<ChartAreas>
<asp:ChartArea Name="line"></asp:ChartArea>
</ChartAreas>
</asp:Chart>
以下是我动态添加系列和点数的方法:
Private Sub chartMonthly_DataBinding(sender As Object, e As System.EventArgs) Handles chartMonthly.DataBinding
Dim conStr As String = ConfigurationManager.ConnectionStrings("GenshenPOS").ConnectionString
Dim conn As New SqlConnection(conStr)
conn.Open()
Dim sqlPoints As New SqlCommand("SELECT [m] AS [月], [Store] AS [門市], SUM([amount]) AS [NTD] FROM vwMonthly WHERE [y] = " & dropYear.SelectedValue & " GROUP BY [m], [Store]", conn)
Dim reader As SqlDataReader = sqlPoints.ExecuteReader()
While reader.Read()
If chartMonthly.Series.IndexOf(reader.Item("門市")) = -1 Then
chartMonthly.Series.Add(reader.Item("門市"))
chartMonthly.Legends.Add(reader.Item("門市"))
chartMonthly.Series(reader.Item("門市")).ChartType = SeriesChartType.Line
chartMonthly.Series(reader.Item("門市")).ChartArea = "line"
End If
chartMonthly.Series(reader.Item("門市")).Points.AddXY("月", "NTD")
End While
conn.Close()
End Sub
图表正在正确读取商店的名称并根据它们创建系列,但我的图表没有数据 - 没有轴有值,系列的行在图表的底部作为0 /空值。
我已单独运行基础查询,数据就在那里,我假设它看到了数据,否则系列和图例就不会被创建,所以我想我错过了一些东西,我添加点到系列?!
Image of chart control with series but displaying no datapoints
答案 0 :(得分:0)
您可以尝试通过显式转换为datetime(月初)来添加Points.AddXY
chartMonthly.Series(reader.Item("門市")).Points.AddXY(New DateTime(Integer.Parse(dropYear.SelectedValue), Integer.Parse(reader("月").ToString()), 1), "NTD")