Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dynamic chart name not recognized

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    '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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading