1

I'm trying to pass a function a string as input to be used in chart building (chart.js).

Basically, I want to take an arbitrary string as an x-axis and another as a y-axis, and then use the names to search through the dataset.

However, every attempt I have in using the input to the function (referenced by variable name) fails as 'undefined', however, if I change it to be the explicit name of the column, such as 'year', it works.

E.g:

function charter(data,type,x,y){
    d3.csv(filename).then(function(loadedData){
    for (let i = 0 ; i < loadedData.length; i++){
               
               let x_axis = loadedData[i].x; // This does not work
               
               let x_axis = loadedData[i].year; // This works


        }
    }
}

How do I, for lack of a better word, 'escape' the variable name so that it's used as a reference instead of an absolute value?

'x' does not exist in the dataset, but the string it should represent, does

Hope that's clear. Thank you,

A console log output may be helpful to illustrate my issue:

enter image description here

Numpty
  • 1,461
  • 5
  • 19
  • 28

0 Answers0