1

I am reading data and loading in two dimensional array using following code by initializing 2d array (series)

where series is structure like as follows (highchart series)

series: [{ name: '' data: [] }]

Manual code:

  <!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>


  <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script>




  <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type='text/css'>

  </style>




<script type='text/javascript'>
$(function () {

 var options = {
        chart :{
                  type: 'polygon',
                  renderTo: 'container',

        },
        title: {
            text: 'Height vs Weight'
        },
        yAxis: {
            title: false,
            gridLineWidth:0,
            lineWidth:0,
            labels:{
                enabled: false
            }
        },
          series: [{},{}]
    };

    $.getJSON('data.json', function(data) {
        options.series[0].data = data[0];
        options.series[1].data = data[1];

        var chart = new Highcharts.Chart(options);
    })

});
</script>

</head>
<body>
  <script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/highcharts-more.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>


</body>

But i want to initialize dynamic array so that i can insert arrays(data[i]) dynamically.

data.json

[
    [
        [
            153,
            42
        ],
        [
            149,
            46
        ],
        [
            149,
            55
        ],
        [
            152,
            60
        ]
    ],
    [
        [
            253,
            142
        ],
        [
            349,
            146
        ],
        [
            349,
            155
        ],
        [
            352,
            160
        ]
    ]
]

dynamic code:

series: [[]]

 $.getJSON('data.json', function(data) {
        for (i=0; i<data.length(); i++) {
           series[i].data = data[i];
         }
    })

above code does not seem to be working. Pls help me how can i solve my problem.

1
  • 2
    edited my answer with the data structure you want. Commented Sep 29, 2015 at 10:04

1 Answer 1

3

You need to use the push() function to append dynamically

EDIT with second array to get the required "data" structure

// initialize array
var series = [];


$.getJSON('data.json', function(data) {
    var temparray = [];
    for (i=0; i<data.length; i++) {
        temparray["data"] = data[i];
        series.push(temparray);
    }
})
Sign up to request clarification or add additional context in comments.

3 Comments

And, .length is a property!
Thanks, missed that one.
just use this: series.push({ name : "", data : data[i] });

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.