views:

433

answers:

1

Hi, i'm trying to learn how to use flot and i think your example is a very nice, simple, very understandable code so i've been trying to implement it but... well, here is my code in the index.aspx:

$(function () {
    $.getJSON("../../Home/JsonValues", function (data) {
        alert('json: ' + data + ' ...');
        var plotarea = $("#plot_area");
        $.plot(plotarea, data);
        //$.plot(plotarea,[ [[0, 0], [1, 1]] ]);
    });
});

and here is the code in the HomeController:

public ActionResult JsonValues()
{
    //string s = "[ [[0, 0], [1, 1]] ]";
    //return Json(s, JsonRequestBehavior.AllowGet);
    StringBuilder sb = new StringBuilder();
    sb.Append("[[0, 0], [1, 1]]");
    return Json("[" + sb.ToString() + "]", JsonRequestBehavior.AllowGet);
}

all i'm getting is an empty graph, although when alerting in the index, i get the perfect formatted json data! any solution? what am i doing wrong? thanks in advance

+1  A: 

I would advice you to avoid creating JSON by hand in your controller. Try this instead:

public ActionResult JsonValues()
{
    return Json(
        new[] { new[] { 0, 0 }, new[] { 1, 1 } }, 
        JsonRequestBehavior.AllowGet);
}

And in the view:

<div id="plot_area" style="width:600px;height:300px;"></div>

<script type="text/javascript">
$(function() {
    $.getJSON('../../Home/JsonValues', function (data) {
        $.plot($('#plot_area'), [data]);
    });
});
</script>
Darin Dimitrov
wohoooo, thank you so much, that worked great for me :)
Lina