[Chartjs]-Function not return values

2👍

You should execute the IIFE. Add a set of parantheses after that function.

datasets: (function () {
    var ret = [];

    for (var i = 0; i < total; i++) {
        var background = [Math.round(Math.random() * 254), Math.round(Math.random() * 254), Math.round(Math.random() * 254)];

        ret[i] = {
            label: names[Math.round(Math.random() * names.length)],
            data: (function () {
                var dat = [];

                for (var j = 0; j < months.length; j++) {
                    dat[j] = Math.round(Math.random() * 50);
                }

                return dat;
            }),
            backgroundColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',0.2)',
            borderColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',1)',
            borderWidth: 1
        };
    }

    return ret;
})() // <----------------------- Look here

Working Snippet

And you don’t need to set var total = total; instead see the below way:

function generateData(total) {
  var names = ['Antonie Lereno', 'Laura Saucini', 'Marco Mendez Ortega', 'Lucas Simon Jainte', 'Angel Rodriguez', 'Manuel Salgado', 'Rosario Parrales'];
  var months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];

  return {
    labels: months,
    datasets: (function() {
      var ret = [];

      for (var i = 0; i < total; i++) {
        var background = [Math.round(Math.random() * 254), Math.round(Math.random() * 254), Math.round(Math.random() * 254)];

        ret[i] = {
          label: names[Math.round(Math.random() * names.length)],
          data: (function() {
            var dat = [];

            for (var j = 0; j < months.length; j++) {
              dat[j] = Math.round(Math.random() * 50);
            }

            return dat;
          }),
          backgroundColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',0.2)',
          borderColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',1)',
          borderWidth: 1
        };
      }

      return ret;
    })()
  };
}
console.log(generateData(5));

0👍

I made few changes to your code. in the inner functions, I tuned it to a IIFE. Hope this is your expected result.

function generateData (total) {

            var total = total;

            var names = ['Antonie Lereno', 'Laura Saucini', 'Marco Mendez Ortega', 'Lucas Simon Jainte', 'Angel Rodriguez', 'Manuel Salgado', 'Rosario Parrales'];
            var months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];

            return {
                labels: months,
                datasets: (function () {

                    var ret = [];

                    for (var i = 0; i < total; i++) {

                        var background = [Math.round(Math.random() * 254), Math.round(Math.random() * 254), Math.round(Math.random() * 254)];

                        ret[i] = {
                            label: names[Math.round(Math.random() * names.length)],
                            data: (function () {

                                var dat = [];

                                for (var j = 0; j < months.length; j++) {

                                    dat[j] = Math.round(Math.random() * 50);

                                }

                                return dat;

                            })(),
                            backgroundColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',0.2)',
                            borderColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',1)',
                            borderWidth: 1
                        };

                    }

                    return ret;

                })()
            };

        }
        
        
        console.log(generateData(1));

Leave a comment