0

I have the following json data

var json= {
    "PFTs_likely_to_change": [
        [16]
    ],
    "PFTs_likely_to_remain_unchanged": [
        [2]
    ]
}

This data is coming through url

var data=$.getJSON('{% url "PFTpercentChange" %}');

then I parse the json data

var json = JSON.parse(data);

Now I want to create column chart using highcharts.The code is

$(document).ready(function() {
  $("#container").dialog({
    autoOpen: false,
    width: 600,
    height: 500

  });
  $("#btnclick").click(function() {
    $("#container").dialog("open");
    //var json = JSON.parse(data);
    var json = {
      "PFTs_likely_to_change": [
        [16]
      ],
      "PFTs_likely_to_remain_unchanged": [
        [2]
      ]
    };
    //var chart1; // globally available
    chart1 = new Highcharts.Chart({
      chart: {
        renderTo: 'container',
        type: 'column' // change with your choice
      },
      title: {
        text: 'Change in Plant Functional Types'
      },
      xAxis: {
        categories: ['Change in Plant Functional Types']
          // Should be replace with JSON
      },
      yAxis: {
        title: {
          text: 'Percentage(%)'
        }
      },
      series: [{ //Should be replace with JSON
        name: 'PFTs likely to change',
        data: [88]
      }, {
        name: 'PFTs likely to remain unchanged',
        data: [12]
      }]
    });
  });
});

How can I do this dynamically?

14
  • can u please share the fiddle Commented Feb 4, 2016 at 16:39
  • jsfiddle.net/CPT6d/72 Commented Feb 4, 2016 at 16:41
  • $.getJSON returns an jqXHR object. You're trying to JSON.parse that object and it's working? Commented Feb 4, 2016 at 16:41
  • No Im not using JSON.parse. I have shared the fiddle. I read in some docs that we need to parse JSON when using external data. My problem is how to do this dynamically? Commented Feb 4, 2016 at 16:43
  • 1
    @Aparna check the fiddle in answer below Commented Feb 4, 2016 at 19:54

1 Answer 1

1

Update Fiddle with chart in dialog with dynamic json data

Here is working fiddle

have a separate div for popup/modal. Now put your highchart container div in it. change your code to get series data dynamically as below code :

 var json= {
"PFTs_likely_to_change": [
    [16]
],
"PFTs_likely_to_remain_unchanged": [
    [2]
]
 }
var seriesData=[];
$.each(json,function(key,val){
console.log(key+"----key----and value"+val);
seriesData.push({name:key,data:val}); //put seriesData in chart's series
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.