我正在尝试基于SQL Server中的表中的数据构建饼图,这是我的代码,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#代码完全返回了它应该的内容,我已经对其进行了调试,并且效果很好。我的程序可以编译,生成和运行,但是当它加载页面时,在显示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"]
});


那应该解决它。另外,更改标题行!

关于c# - SQL Server的图表构建问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28641842/

10-16 23:11