Search code examples
javascriptarrayshighchartsdrilldownkeyvaluepair

Highchart.js drilldown adding to data dynamicaly without hardcoding.


I want to add a fully populated array or keyvalue paired object to the drill down data section but when I do so it either only shows me the first value in the array or just does not display any information at all.

Is there a way of populating this data section without hard coding the values?

Below is a brief section of the code i've tried using keyvalue pairs, i left out most of the original hightchart pie code, will post the rest of it if needed.

var pieChart = function (userTypes) {
// Create the chart
console.log('pieChart');
var array_keys = new Array();
var array_values = new Array();

for (var key in userTypes) {
    array_keys.push(key);
    array_values.push(userTypes[key]);
}

....
adminNames ={}
for(i =0; i<array_values.length;i++){   
    if(array_values[i]== "admin"){
        admin =admin +1;
        adminNames[array_keys[i]]=i;
    }

....
 drilldown: {
        series: [{
            name: "Admin",
            id: "Admin",

            data: [
                  adminNames
            ]
        },.....

Solution

  • Populating the drilldown array programatically should work fine, but Highcharts is expecting the data property to be an array of arrays, see example in documentation:

    data: [
        ['Cats', 4],
        ['Dogs', 2],
        ['Cows', 1],
        ['Sheep', 2],
        ['Pigs', 1]
    ]
    

    Your code is initialising it as an array with one element which is an associative array - something like this:

    data: [
      { 
        'Cats': 4,
        'Dogs': 2,
        'Cows': 1,
        'Sheep': 2,
        'Pigs': 1
      }
    ]
    

    Also the logic for populating adminNames is a bit confusing but hard to say without knowing what its supposed to do.