jqplot不显示ajax数据

发布于 2024-11-29 04:09:26 字数 1642 浏览 1 评论 0原文

我有以下 jQuery 代码:

$(document).ready(function () {
        var group = 'test';
        $.ajax({
            type: "POST",
            async: false,
            url: "/validate.asmx/GetGraphData",
            contentType: "application/json;",
            data: "{'groupBy': '" + group + "'}",
            dataType: 'json',
            success: function (data) {
                Plot(data.d);
            }
        });
    });

    function Plot(dataIn) {
        alert(dataIn);
        $.jqplot('chartcontainer', [[[ 'test1', 1 ], [ 'test2', 5]]],
        {
            seriesDefaults: {
                renderer: $.jqplot.PieRenderer,
                rendererOptions: {
                    showDataLabels: true
                }
            },
            legend: { show: true, location: 'e' }
        }
            );
    }

webmethod (在剪切它进行测试之后)如下所示:

[WebMethod]
    public string GetGraphData(string groupBy)
    {
        PaymentModelDataContext db = new PaymentModelDataContext();
        var q = from Event in db.TrackingEvents
                group Event by Event.campaignID;

        //string returnJSON;
        //string returnJSON = "[";
        //foreach (var grp in q)
        //{
        //    returnJSON += "['" + grp.Key + "'," + grp.Count() + "],";
        //}

        //returnJSON += "]";


        //var ser = new JavaScriptSerializer();
        //returnJSON = ser.Serialize(q);
        //return returnJSON;
        return "[['test1' , 1],['test2' , 5]]";
    }

如果我采用在此处返回的相同字符串并将其作为文本放入 jquery 代码中,则会显示该图。我在绘图函数中放置了一个警报,数据是我发送的。 有什么想法吗?

谢谢你!

I have the following jQuery code:

$(document).ready(function () {
        var group = 'test';
        $.ajax({
            type: "POST",
            async: false,
            url: "/validate.asmx/GetGraphData",
            contentType: "application/json;",
            data: "{'groupBy': '" + group + "'}",
            dataType: 'json',
            success: function (data) {
                Plot(data.d);
            }
        });
    });

    function Plot(dataIn) {
        alert(dataIn);
        $.jqplot('chartcontainer', [[[ 'test1', 1 ], [ 'test2', 5]]],
        {
            seriesDefaults: {
                renderer: $.jqplot.PieRenderer,
                rendererOptions: {
                    showDataLabels: true
                }
            },
            legend: { show: true, location: 'e' }
        }
            );
    }

The webmethod (after cutting it for testing) looks like this:

[WebMethod]
    public string GetGraphData(string groupBy)
    {
        PaymentModelDataContext db = new PaymentModelDataContext();
        var q = from Event in db.TrackingEvents
                group Event by Event.campaignID;

        //string returnJSON;
        //string returnJSON = "[";
        //foreach (var grp in q)
        //{
        //    returnJSON += "['" + grp.Key + "'," + grp.Count() + "],";
        //}

        //returnJSON += "]";


        //var ser = new JavaScriptSerializer();
        //returnJSON = ser.Serialize(q);
        //return returnJSON;
        return "[['test1' , 1],['test2' , 5]]";
    }

If I take the same string I return here and put it as text in the jquery code, the plot is shown. I put an alert in the plot function, and the data is as I sent it.
Any ideas?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2024-12-06 04:09:26

使用数据渲染器

$(document).ready(function(){

    var ajaxDataRenderer = function(url, plot) {
        var ret = null;
        $.ajax({
            // have to use synchronous here, else returns before data is fetched
            async: false,
            url: url,
            dataType:'json',
            success: function(data) {
                ret = data;
            }
        });
        return ret;
    };

    var jsonurl = "/validate.asmx/GetGraphData";

    plo12 = $.jqplot('chart2', jsonurl,{
        title: 'AJAX JSON Data Renderer',
        dataRenderer: ajaxDataRenderer,
        seriesDefaults: {
            renderer: $.jqplot.PieRenderer,
            rendererOptions: {
                showDataLabels: true
            }
        },
        legend: { show: true, location: 'e' }

    });
});

use the dataRenderer

$(document).ready(function(){

    var ajaxDataRenderer = function(url, plot) {
        var ret = null;
        $.ajax({
            // have to use synchronous here, else returns before data is fetched
            async: false,
            url: url,
            dataType:'json',
            success: function(data) {
                ret = data;
            }
        });
        return ret;
    };

    var jsonurl = "/validate.asmx/GetGraphData";

    plo12 = $.jqplot('chart2', jsonurl,{
        title: 'AJAX JSON Data Renderer',
        dataRenderer: ajaxDataRenderer,
        seriesDefaults: {
            renderer: $.jqplot.PieRenderer,
            rendererOptions: {
                showDataLabels: true
            }
        },
        legend: { show: true, location: 'e' }

    });
});
混浊又暗下来 2024-12-06 04:09:26

将 ajax 数据放入 jqPlot dataRenderer 是一种选择。但它也会按照您选择的方式工作。

确保数据不被处理为字符串。

如果您的数据是 "[['test1' , 1],['test2' , 5]]"
arr=eval("[['test1' , 1],['test2' , 5]]") 应该根据您的数据生成一个数组。

如果您使用 console.warn("[['test1' , 1],['test2' , 5]]");console.warn(arr );在 Firebug 控制台上。

Putting ajax data to jqPlot dataRenderer is one option. But it will also work at the way you choose.

Make sure, that the data is not processed as string.

if your data is "[['test1' , 1],['test2' , 5]]" than
arr=eval("[['test1' , 1],['test2' , 5]]") should generate an array from your data.

You will see the difference, if you use console.warn("[['test1' , 1],['test2' , 5]]"); and console.warn(arr);on the firebug console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文