Question

I'm trying out the Highcharts jQuery plugin for creating charts of data in an MVC application. But I need to get the data for the function dynamically from an Action Method. How can I do that?

Taking the example from the Highcharts site (http://highcharts.com/documentation/how-to-use):

var chart1; // globally available
$(document).ready(function() {
      chart1 = new Highcharts.Chart({
         chart: {
            renderTo: 'chart-container-1',
            defaultSeriesType: 'bar'
         },
         title: {
            text: 'Fruit Consumption'
         },
         xAxis: {
            categories: ['Apples', 'Bananas', 'Oranges']
         },
         yAxis: {
            title: {
               text: 'Fruit eaten'
            }
         },
         series: [{
            name: 'Jane',
            data: [1, 0, 4]
         }, {
            name: 'John',
            data: [5, 7, 3]
         }]
      });
   });

How can I get the data in there dynamically from the action method? Someone suggested I might use JSon, but couldn't specify how. If this is the case, I would really appreciate a simple and specific example, because I don't know much about JSon.

Any help appreciated!

Was it helpful?

Solution

I just got it working myself after having some trouble with formatting my dynamic data in a Rails app I'm working on.

I ended up using trial and error, viewing the page source until it looked the way I wanted it to.

series [{
name: "#{@model_name}",
data: #{@model_name.get_some_nested_array.to_json}
}]

So in the page source it looks like this:

series [{
name: "Model Name",
data: [[1, 4], [2, 3], [3, 7]]
}]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top