score:3
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});
score:0
Just in case. Highcharts can't display chart if value are set to null. And Json method of asp.net mvc controler class can't filter null value.
For that you can use json.net library and create, for example, a 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 controler to replace Json method of asp.net mvc :
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 controler action like that :
public JsonNetResult MyAction()
{
MyClass myObject = new MyClass();
return JsonNet(myObject);
}
Ah and an other thing, don't hesitate to use Json.Net DefaultValue attribute on MyClass properties :
[DefaultValue(null)]
score:1
perfect ek_ny !
Thanks to your help I found how to do also 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}));
score:1
Another thing nice to know.
highcharts series.data is a point object which 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;}
}
reference : http://www.highcharts.com/ref/#point
ek_ny class is a part of point object representation. events and marker is commented because it's another class to write. Representation of it is there :
events : http://www.highcharts.com/ref/#point-events
marker : http://www.highcharts.com/ref/#point-marker
So now you can use it like that :
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 it help...
Source: stackoverflow.com
Related Query
- Send JSON data to highcharts pie from asp.net MVC controller in C#
- Retrieve data from Controller to View using Json to HighCharts
- Highcharts displays series names but missing data points from json source
- Ember.js , HighCharts - accessing JSON data from controller
- get json data from my controller in highcharts.js
- Highcharts bargraph from json data in angularJS
- Formatting JSON from a Pandas data frame for Highcharts within a Django template
- How to make Highcharts fetch data from external JSON file?
- Extracting data from a JSON call to a Postgres table for use in Highcharts (without PHP)
- Getting data from Postgres in JSON format in ROR into a Highcharts javascript
- passing json values to highcharts from .net code behind
- How to hardcode chart data in my Model then have my Controller pull it from there to display it. Using Highcharts
- Highcharts polar chart wind rose data from JSON
- HighCharts data from SQL Server, classic asp
- highcharts example for using data from database with mvc
- How to send json to highcharts from php
- How to create seriesfor Highcharts from JSON data to use
- How to add Series data in Highcharts from MVC
- Highcharts display label for pie chart using html table as data source
- HighCharts populate Pie Chart with data from SQL Database
- How to use json data as highcharts Pie Chart?
- Highcharts chart dynamic json data from sql
- Extract data series from a JSON array for a Highcharts chart with 2 y-axis
- Plotting Highcharts Chart from multiple data points in json
- Highcharts combination chart from JSON data
- Having trouble JSON encoded data from MySQL through Highcharts
- Passing data from Javascript MVC controller
- Set HighCharts data from JSON string with setData() function
- Highcharts to populate data for pie chart using json object
- How to pass JSON data to update Highcharts Pie chart
More Query from same tag
- Issue on Drilling Down and Up in Highcharts.js Using External Dom Buttons
- How to display positive and negative values in positive side of graph in Highchart?
- Removing zoom in/out from Highmaps
- Highcharts Pie Chart: How to ignore disabled legend items
- Highcharts won't draw line when series values are all 0
- Generate image in a folder from Highchart in PHP
- Highcharts - Data won't get loaded into chart on Android WebView
- GeoJSON drawing incorrectly in Highmaps
- Old highchart data in react component when going back and forward
- Data Not Showing in Highcharts
- Set HighCharts marginTop dynamically based on mobile device screen size
- HighMaps Undefined ZoomTo()
- Highcharts change line style after intersect
- Highchart jquery charts not working SetAttribute is not a function
- Highcharts- this.category returns undefined
- Control spacing and height between bars in Highcharts.js bar chart
- Highcharts heatmap does not render correct colours according to specified colour stops
- Highcharts - programmatically slice
- onclick popout highchart piechart
- How to set 'lang' property in Highcharts using Angular JS?
- Highcharts - Issue with the chart reflow function
- How to avoid horizontal scrollbar in highcharts
- Rshiny Highchart: keeping fixed color for order-changing categories on x axis
- Legend name and color need to be displayed in the export of highchart as image
- How to display data coming from the controller as collection in a ViewData dynamically Highcharts
- Highcharts Gaussian Chart / Gaussian Distribution, with PHP
- Dynamic resizing height not working of bar chart as number of teams increasing in my case
- Drilldown on multiple Highcharts with the same data
- Highcharts tootip for stacked bar charts
- Highcharts, labeling axes, last label not shown correctly, potential bug