简体   繁体   中英

Dynamically add code to JS

I've been asked to create Graphs dynamically according to a certain csv content.

I'm currently using the Chart.js library, which requires you to add elements to the "label" property in order to properly build the graph.

label : ["name1", "name2"]

What I am not able to do here, is dynamically add the elements contained in an array "Names" into the parameter.

var names = "asia", "africa";

What I am try to accomplish is that, according to the number of entries inside the array "names", those many names are gonna appear on the graph.

Any ideas? thanks in advance!

[EDIT]

function createChart(upData, lData, allData){

        var ctx = document.getElementById("myChart").getContext("2d");
            document.getElementById("myChart").style.width = "<?wsx5 document.write(parameters.widthsize.value);?>px";
            document.getElementById("myChart").style.height = "<?wsx5 document.write(parameters.heightsize.value);?>px";


            var data = {
                labels: [

                ],

This is about what I got now, the relevant part of course. upData is an array that contains the names I need, and those elements need to be added dynamically to the label section

I actually found out what the problem was. Apparently the code in order for the labels field to work is without brackets:

function createChart(upData, lData, allData){

    var ctx = document.getElementById("myChart").getContext("2d");
        document.getElementById("myChart").style.width = "<?wsx5 document.write(parameters.widthsize.value);?>px";
        document.getElementById("myChart").style.height = "<?wsx5 document.write(parameters.heightsize.value);?>px";


        var data = {
            labels: arrayname , }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM