I want to use dynamic chart name while using setExtremes (like chart1, chart2, chart3).
Instead of below line as chart1
chart1.yAxis[0].setExtremes(min, max); // which works fine if used
I use the below line in order to have dynamic chart name. But says properties of undefined.
'chart' + idconvertedtostr.yAxis[0].setExtremes(min, max); // does not work
‘chart’ + idconvertedtostr is not recognized as ‘chart1’. May I know where I am going wrong. Thank you.
My Code:
ID = parseInt(elem.getAttribute('id'))
var idconvertedtostr = ID.toString();
console.log("ID", ID) // returns 1
console.log("join", 'chart' + idconvertedtostr) // returns chart1
'chart' + idconvertedtostr.yAxis[0].setExtremes(min, max); // throws error, not recognized as chart1.setExtremes(min, max)
>Solution :
I would highly discourage doing it this way. If anything, you can use an array to store your charts (or better yet, dynamically make it in the array — for the sake of simplicity, ill do it this way.).
const charts = [
chart1,
chart2,
chart3
]
// then access a chart of an ID:
const ID = 1
// we subtract 1 because of array indexing: array[0] gives the first element
charts[ID - 1] // chart1
Though this is a terrible idea, you can also use window to get a global variable by its name:
window["chart1"] // returns chart1 (the variable)