SQL 服务器的图表构建问题
Chart Building Issue With SQL Server
我正在尝试根据 SQL 服务器中 table 的数据构建饼图,这是我的代码,ASP.NET 和 c#:
ASP.NET
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
function drawChart() {
var options = {
title: 'My Bills'
};
$.ajax({
type: "POST",
url: "MyPage.aspx/GetChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert("Failure");
},
error: function (r) {
alert("Error");
}
});
}
C#
[WebMethod]
public static List<object> GetChartData()
{
string Chartquery = "SELECT a.[amount] AS 'Amount', DATENAME(Month,a.[myDate]) +' '+ convert(varchar(4),Year(a.[myDate])) As 'Month Year' "
+ "FROM [myTable] a "
+ "left join [mySecondTable] b on a.[ID] = b.[ID] "
+ "left join [myThirdTable] c on c.[code] = b.[code] "
+ "WHERE c.[myName] = John Doe";
String strConnString = ConfigurationManager.ConnectionStrings["myConnection"].ToString();
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Amount", "Month Year"
});
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand(Chartquery))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
chartData.Add(new object[]
{
sdr["Amount"].ToString(), sdr["Month Year"].ToString()
});
}
}
con.Close();
return chartData;
}
}
}
C#
代码 returns 完全正确,我已经对其进行了调试并且效果很好。我的程序编译、构建并运行,但是当它加载页面时,我在页面上收到错误 Pie chart should have a first column of type string
。所以我猜它与 ASP 代码有关。
我知道我发布了很多代码,但我想提供我必须提供的所有内容,以便更好地了解我在做什么,并让大家指出任何错误。
关于如何修复上述 Pie chart should have a first column of type string
错误的任何建议?
编辑:
我在 while 循环中添加了 .ToString()
,它解决了字符串的问题。现在页面加载没有错误,但图表似乎没有填充数据,即使我的 return chartData
正在返回数据....我页面上唯一显示的是图表标题
有什么建议吗?
你写的地方
chartData.Add(new object[]
{
sdr["Amount"].ToString(), sdr["Month Year"].ToString()
});
改为
chartData.Add(new object[]
{
sdr["Month Year"].ToString(), sdr["Amount"]
});
这应该可以解决问题。另外,更改 header 行!
我正在尝试根据 SQL 服务器中 table 的数据构建饼图,这是我的代码,ASP.NET 和 c#:
ASP.NET
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
function drawChart() {
var options = {
title: 'My Bills'
};
$.ajax({
type: "POST",
url: "MyPage.aspx/GetChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert("Failure");
},
error: function (r) {
alert("Error");
}
});
}
C#
[WebMethod]
public static List<object> GetChartData()
{
string Chartquery = "SELECT a.[amount] AS 'Amount', DATENAME(Month,a.[myDate]) +' '+ convert(varchar(4),Year(a.[myDate])) As 'Month Year' "
+ "FROM [myTable] a "
+ "left join [mySecondTable] b on a.[ID] = b.[ID] "
+ "left join [myThirdTable] c on c.[code] = b.[code] "
+ "WHERE c.[myName] = John Doe";
String strConnString = ConfigurationManager.ConnectionStrings["myConnection"].ToString();
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Amount", "Month Year"
});
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand(Chartquery))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
chartData.Add(new object[]
{
sdr["Amount"].ToString(), sdr["Month Year"].ToString()
});
}
}
con.Close();
return chartData;
}
}
}
C#
代码 returns 完全正确,我已经对其进行了调试并且效果很好。我的程序编译、构建并运行,但是当它加载页面时,我在页面上收到错误 Pie chart should have a first column of type string
。所以我猜它与 ASP 代码有关。
我知道我发布了很多代码,但我想提供我必须提供的所有内容,以便更好地了解我在做什么,并让大家指出任何错误。
关于如何修复上述 Pie chart should have a first column of type string
错误的任何建议?
编辑:
我在 while 循环中添加了 .ToString()
,它解决了字符串的问题。现在页面加载没有错误,但图表似乎没有填充数据,即使我的 return chartData
正在返回数据....我页面上唯一显示的是图表标题
有什么建议吗?
你写的地方
chartData.Add(new object[]
{
sdr["Amount"].ToString(), sdr["Month Year"].ToString()
});
改为
chartData.Add(new object[]
{
sdr["Month Year"].ToString(), sdr["Amount"]
});
这应该可以解决问题。另外,更改 header 行!