0

我正在尝试使用理解查询语法从我的数据库中提取数据,然后将其作为 Json 对象传递,以便 Google 图表可以显示数据,但它说数据不可用。我的查询语法看起来不错,我什至在 Mysql 工作台中尝试过,它返回了我想要的结果。但是,它没有返回我在 C# MVC 项目中想要的结果。也许我没有正确返回数据对象。它适用于虚拟数据,但不适用于数据库中的数据。

[HttpPost]
    public JsonResult GetChartData()
    {
        //var siteData = new[] { new { time = 2345, tdate = "2012, 3, 1" },
        //                       new { time = 4568, tdate = "2012, 3, 2" },
        //                       new { time = 1237, tdate = "2012, 3, 3" },
        //                       new { time = 9874, tdate = "2012, 3, 4" },
        //};

        var test = from p in _db.page_loads
                    join f in _db.sites
                    on p.site_id equals f.id
                    where f.thirdParty_id == siteId //(siteId is private int declared in my class above
                    select new { time = p.time, created_at = p.created_at };


        return Json(test);           
    }

这就是我的脚本在视图部分的样子

<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript'>
    google.load('visualization', '1', { 'packages': ['annotatedtimeline'] });

    google.setOnLoadCallback(drawChart);

    function drawChart() {

        $.post('/metrics/GetChartData', {},
        function (data) {
            var tdata = new google.visualization.DataTable();

            tdata.addColumn('datetime', 'Date');
            tdata.addColumn('number', 'Times');

            for (var i = 0; i < data.length; i++) {
                                    var date = new Date(parseInt(data[i].created_at.substr(6)));
                                    var load_time = data[i].time;
                                    tdata.addRow([date, load_time]);
            }

            var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
            chart.draw(tdata, { displayAnnotations: true });

        });

    }

4

0 回答 0