Send JSON data to highcharts pie from asp.net MVC controller in C#

Dragouf picture Dragouf · Jun 6, 2011 · Viewed 9.1k times · Source

I want to send json data from my asp.net MVC controller in C# and use those datas to draw a highcharts pie chart.

So for the moment I use this :

Dictionary<string, double> result = new Dictionary<string, double>();
result.Add("test1", 45);
result.Add("test2", 64);

var jsonResult = Json(new
{
    graphDivId = "divGraph",
    legend = "A legend",
    stats = result ,
});

jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return jsonResult;

but when I received data by an ajax call in jquery I have this :

stats:[{"Key":"test1","Value":45},{"Key":"test2","Value":64}]

but I need this :

stats:[["test1",45],["test2",64]]

Any ideas ?

Answer

ek_ny picture ek_ny · Jun 6, 2011

I was playing with this a little this morning and I came up with what i have below-- but it won't work because you'll get [{x:"testx", y:60}....]. So that's no good. You might be able to use it though and override a ToJSON method on the SimpleClass.

One other thought I had (and I'm not sure it would work) is to have a collection of ArrayList's. Since ArrayList's are not strongly type, you can add a string and double property to them.

Let me know how this turns out.

What if you used a list of Simple objects. You might be able to use a key value pair or some other existing class. But you could create a simple class which will hold your data.

   class SimpleClass{
       public int x{set; get;}
       public double y{set; get;}
   } 

    var results = new List<SimpleClass>();
    results.Add(new SimpleClass{x="test3", y=42});        
    results.Add(new SimpleClass{x="test2", y=99});