How to remove random value in data and impersonate it by values defined in javascript

1

Well, I have a function in javascript that what it does is print a graph, at the moment it does it with random values, but I'm interested in executing it with predefined values, and trying to save an array of numbers in a variable, and send it to call, but it does not work, here is the code, to see if you can resolve my question, thank you.

Javascript

(function () {
    $(document).on('ready page:load', function () {
         var midata = [9, 13, 10, 9, 15, 8, 9, 11, 20, 16, 14, 21];
        var randomData;
        randomData = function () {
            return _.map([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function (i) {
                return [i, parseInt((Math.floor(Math.random() * (1 + 20 - 10))) + 10)];
            });
        };
        return $('.flot-linea').each(function () {
            var $el, color;
            $el = $(this);
            color = EmVars.colorFromName($el.data('color') || 'info');
            return $.plot($el, [
              {
                  data: randomData()
              }
            ], {
                series: {
                    lines: {
                        show: true,
                        lineWidth: 1,
                        fill: true,
                        fillColor: {
                            colors: [
                              {
                                  opacity: 0.3
                              }, {
                                  opacity: 0.3
                              }
                            ]
                        }
                    },
                    points: {
                        radius: 3,
                        show: true
                    },
                    grow: {
                        active: true,
                        steps: 50
                    },
                    shadowSize: 2
                },
                grid: {
                    hoverable: true,
                    clickable: true,
                    tickColor: EmVars.colors.light.color,
                    borderWidth: 1,
                    color: EmVars.colors.light.color
                },
                colors: [color],
                xaxis: {},
                yaxis: {
                    ticks: 5
                },
                tooltip: true,
                tooltipOpts: {
                    content: "chart: %x.1 is %y.4",
                    defaultTheme: false,
                    shifts: {
                        x: 0,
                        y: 20
                    }
                }
            });
        });
    });

}).call(this);
    
asked by Guillermo Navarro 05.10.2016 в 06:29
source

1 answer

2

If I understood correctly, you want to replace the random data with the ones in the list:

var midata = [9, 13, 10, 9, 15, 8, 9, 11, 20, 16, 14, 21];

The issue is that the function ranomData returns an arrangement like this:

[[1,3],[2,6],[3,5],[4,...]]

You can use map to replicate the structure; since the second parameter of the callback function is the index with base 0, it would be necessary to add 1 to have the index of the graph and the value of midata comes in the first parameter.

var midata = [9, 13, 10, 9, 15, 8, 9, 11, 20, 16, 14, 21];
var mapedData = function () {
    return _.map(midata, function (val,ix) {
        return [ix + 1, val]; 
    });
};

Then instead of calling randomData() you invoke mappedData()

return $('.flot-linea').each(function () {
    var $el, color;
    $el = $(this);
    color = EmVars.colorFromName($el.data('color') || 'info');
    return $.plot($el, [
       {
              data: mappedData() /// AQUI <<<<<<<<<
       }
    ], { 
    /// resto del codigo
    
answered by 05.10.2016 / 13:26
source