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

I want to send json data from my asp.net MVC controller to C # and use this data to draw a pie chart of tall charts.

So at the moment I am using 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 got data on ajax call in jquery, i have this:

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

but I need it:

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

Any ideas?

+6
source share
4 answers

This morning I played with this a bit, and I came up with what is below me, but it will not work, because you will get [{x: "testx", y: 60} ....]. So nothing good. Perhaps you can use it and override the ToJSON method on SimpleClass.

Another thought that I had (and I'm not sure if this will work) was to put together an ArrayList collection. Since ArrayList do not have a strict type, you can add the string and double property to them.

Let me know how it turns out.

What if you used a list of simple objects. You might be able to use a pair of key values ​​or some other existing class. But you can create a simple class that will store 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}); 
+3
source

perfect ek_ny!

Thanks to your help, I found how to do this without a specific class:

 var results = new List<List<object>>(); results.Add(new List<object>(new object[]{"test1", 45})); results.Add(new List<object>(new object[]{"test2", 99})); 
+1
source

Another nice thing.

highcharts series.data is a point feature that you can represent by this in C #:

 class HighChartsPoint { public double x {set; get;} public double y {set; get;} public string color {set; get;} //public HighChartsEvent events{set; get;} public string id {set; get;} //public HighChartsMarker marker {set; get;} public string name {set; get;} public bool sliced {set; get;} } 

link: http://www.highcharts.com/ref/#point

The ek_ny class is part of the representation of a point feature. events and marker are commented out because it is a different class for writing. Presentation of it is:

events: http://www.highcharts.com/ref/#point-events

: http://www.highcharts.com/ref/#point-marker

So now you can use it like this:

 var results = new List<HighChartsPoint>(); results.Add(new HighChartsPoint { name="test3", y=42, color="red", id="someid", sliced=false }); var jsonResult = Json(results); jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet; return jsonResult; 

Hope this helps ...

+1
source

Just in case. Screen charts cannot display a chart if the value is set to zero. And the Json method of asp.net mvc mpc class cannot filter null value.

To do this, you can use the json.net library and create, for example, JsonNetResult (inherit from ActionResult):

 public class JsonNetResult : ActionResult { public Encoding ContentEncoding { get; set; } public string ContentType { get; set; } public object Data { get; set; } public JsonSerializerSettings SerializerSettings { get; set; } public Formatting Formatting { get; set; } public JsonNetResult() { SerializerSettings = new JsonSerializerSettings(); } public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); HttpResponseBase response = context.HttpContext.Response; response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; if (ContentEncoding != null) response.ContentEncoding = ContentEncoding; if (Data != null) { JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting }; JsonSerializer serializer = JsonSerializer.Create(SerializerSettings); serializer.Serialize(writer, Data); writer.Flush(); } } } 

and then add this method to your controller to replace the asp.net mvc Json method:

 protected JsonNetResult JsonNet(object data, bool needDefaultSettings) { var result = new JsonNetResult(); result.Data = data; if (needDefaultSettings) { var defaultSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }; result.SerializerSettings = defaultSettings; } return result; } 

So now you can use it in your action to control as follows:

 public JsonNetResult MyAction() { MyClass myObject = new MyClass(); return JsonNet(myObject); } 

And also, feel free to use the Json.Net DefaultValue attribute for MyClass properties:

 [DefaultValue(null)] 
0
source

Source: https://habr.com/ru/post/889855/


All Articles