我一直在尝试制作一个从数据库中提取数据的多线图。 为了做到这一点,我编写了以下代码来填充图表:
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = ConfigurationManager.ConnectionStrings["Removed"].ToString();
SqlCommand cmd1 = new SqlCommand("SELECT YEAR(Start_Date) AS Year, Month(Start_Date) As Month, Cost, Utility_Type FROM import_Utilities WHERE YEAR(Start_Date) = 2011 AND Utility_Type = 'Water'");
cmd1.Connection = con1;
con1.Open();
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
Chart1.DataSource = dt1;
Chart1.Series["Series2"].XValueMember = "Month";
Chart1.Series["Series2"].YValueMembers = "Cost";
Chart1.Legends.Add(new Legend("Default") { Docking = Docking.Right });
Chart1.DataBind();
con1.Close();
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Removed"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("SELECT YEAR(Start_Date) AS Year, Month(Start_Date) As Month, Cost, Utility_Type FROM import_Utilities WHERE YEAR(Start_Date) = 2012 AND Utility_Type = 'Water'");
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Chart1.DataSource = dt;
Chart1.Series["2010"].XValueMember = "Month";
Chart1.Series["2010"].YValueMembers = "Cost";
Chart1.DataBind();
con.Close();
此代码有效,但它有两个数据源,因此只有底部的sqlconnection填充到图表中。 我迷失了如何解决这个问题,我想他们这是一个更好的方法,但我不知所措。
更新:
使用下面的示例我最终使用pivot sql来制作表格(见图片)。 然后我将我的代码更新为以下内容:
SqlCommand cmd = new SqlCommand("SELECT * FROM ( SELECT YEAR(Start_Date), CASE MONTH(Start_Date) WHEN 1 THEN 'January' WHEN 2 THEN 'February' WHEN 3 THEN 'March' WHEN 4 THEN 'April' WHEN 5 THEN 'May' WHEN 6 THEN 'June' WHEN 7 THEN 'July' WHEN 8 THEN 'August' WHEN 9 THEN 'September'WHEN 10 THEN 'October' WHEN 11 THEN 'November' WHEN 12 THEN 'December' END as [Month], [Cost] FROM [HousingAccountingReports].[dbo].[import_Utilities] Where [Building] = 'Building B' AND [Utility_Type] = 'Electric') TableDate PIVOT (SUM([Cost]) FOR [Month] IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December] )) PivotTable");
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Chart3.DataSource = dt;
Chart3.Series["Series1"].XValueMember = "Month";
Chart3.Series["Series1"].YValueMembers = "2010";
Chart3.Series["Series2"].XValueMember = "Month";
Chart3.Series["Series2"].YValueMembers = "2011";
Chart3.Series["Series3"].XValueMember = "Month";
Chart3.Series["Series3"].YValueMembers = "2012";
Chart3.DataBind();`
当我运行页面时,它出错,无法找到列Month。如果我补充:
dt.Columns.Add("月&#34);
dt.Columns.Add("年份&#34);`
它找不到2010年,如何让我的图表正确驾驶;我的pivot sql有问题吗?
答案 0 :(得分:1)
您无法更改图表数据源而无需重置上述代码中的数据。
与昨天的问题一样,我建议您先创建一个数据表,然后将数据表中的列引用到您的特定系列
从昨天回答
因此,建议清理代码,我会执行以下操作
- 获取2个数据表
- 将数据表与“月”合并为主键
- 将每列添加到图表中作为新系列
醇>
或者您可以手动绘制系列
所以放弃
Chart1.DataSource = dt
Chart1.Series["2010"].XValueMember = "Month";
Chart1.Series["2010"].YValueMembers = "Cost";
并将其替换为
For Each row As DataRow In dt1.Rows
Chart1.Series("Series2").Points.AddXY(row.Item("Month"), row.Item("Cost"))
Next
For Each row As DataRow In dt.Rows
Chart1.Series("2010").Points.AddXY(row.Item("Month"), row.Item("Cost"))
Next
如果您设法将所有数据合并到一个Datatable
并创建了包含两个系列的图表,那么您可以执行以下操作
Dim dt As New DataTable
dt.Columns.Add("Month", GetType(String))
dt.Columns.Add("2010", GetType(Integer))
dt.Columns.Add("2011", GetType(Integer))
dt.Rows.Add("January", 15, 25)
dt.Rows.Add("February", 18, 32)
dt.Rows.Add("March", 12, 34)
dt.Rows.Add("April", 12, 34)
Chart1.DataSource = dt
Chart1.Series(0).XValueMember = "Month"
Chart1.Series(0).YValueMembers = "2010"
Chart1.Series(1).XValueMember = "Month"
Chart1.Series(1).YValueMembers = "2011"
Chart1.DataBind()
根据您的代码,这应符合我的理解:
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["removed"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("SELECT YEAR(Start_Date) AS Year, Month(Start_Date) As Month, Cost, Utility_Type, Building FROM import_Utilities WHERE YEAR(Start_Date) = 2012 AND YEAR(Start_Date) = 2011 AND Utility_Type = 'Water'");
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Chart1.DataSource = dt;
Chart1.Series(0).XValueMember = "Month";
Chart1.Series(0).YValueMembers = "2010";
Chart1.Series(1).XValueMember = "Month";
Chart1.Series(1).YValueMembers = "2011"
Chart1.DataBind()
数据表dt应该包含其中的所有数据{因为我正在考虑这两年)(如何将其绑定到表以便我可以验证)
现在它的抱怨系列不能用作方法。
我是一名非常新的程序员,只能在我的空闲时间这样做,所以请原谅我的简单错误。
我是否需要像在此处一样设置数据表: Chart1.DataSource = dt; Chart1.Series(0).XValueMember =“Month”; Chart1.Series(0).YValueMembers =“2010”;
Chart1.Series(1).XValueMember = "Month";
Chart1.Series(1).YValueMembers = "2011"
Chart1.DataBind()
或者它是基于sql表吗?