简体   繁体   中英

add array of unknown length in a dataset javascript

I am creating a js chart and here is a code snipet

  var period=array(3,6,7,4,9);
   var marks=array(76,87,45,87,44);
        var randomScalingFactor = function(){ return    Math.round(Math.random()*100)};   

                var lineChartData = {
                    labels : [period[0],period[1],period[2],period[3]],
        datasets : [
            {
                label: "My First dataset",
                fillColor : "rgba(220,220,220,0.2)",
                strokeColor : "rgba(220,220,220,1)",
                pointColor : "rgba(220,220,220,1)",
                pointStrokeColor : "#fff",
                pointHighlightFill : "#fff",
                pointHighlightStroke : "rgba(220,220,220,1)",
                data :    [marks[0],marks[1],marks[2],marks[3]]
            }
        ]

    }

  window.onload = function(){
    var ctx = document.getElementById("canvas").getContext("2d");
    window.myLine = new Chart(ctx).Line(lineChartData, {
        responsive: true
    });
  }

I need to replace data:[10,20,30...] with an array values[1,3,6,34,56,...] with unknown width. I cant figure out how to inject array inside the 'data'. I tried converting the array to comma separated string and added it like

var mar=5+','+6+','+7+','+31;   
data : [mar]

and it couldn't work. Every suggestion is welcome ///////////////////////// I have edited to show marks and period which are arrays of known width. Now I have an array of unknown length that I want to inject to 'data[]'

You must to make this:

 var key = 0;
 var mar = [5,6,7,31];   
 datasets[key].data = mar;

This works, but depending the mode you are developing. If you share more code I can help you better

EDITION

You are making this:

    data :    [marks[0],marks[1],marks[2],marks[3]]

That's the same to this:

    data : marks;

The only reason to make the first option is to cut the array in the fourth element, I don't know if you want it. If you don't know how many elements are inside the array, please, use the second option data:marks;

Good luck

写吧

datasets[0].data = [1, 3, 6, 34, 56];

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