【问题标题】:Populating plotly graph with firebase database array使用 firebase 数据库数组填充绘图图
【发布时间】:2018-08-19 07:39:00
【问题描述】:

如何根据来自 Firebase 后端的数据填充绘图条形图。如果我将图形代码和数组变量放在 for each 数组中只向图形添加一个值,但是如果我将 plotly 图形代码和数组变量数组和 array2 在 for each 循环之外,则 数组的值在 for each 循环之外是未定义的。如何使用数据库中的值填充 x 和 y //示例数组[约翰,凯文,梅尔] //示例数组2 [45,56,67]

var array= [];
var array2 = [];
firebase.database().ref("Scores").child('gameOneScores').on('value', function(snap){

   snap.forEach(function(childNodes){

    array.push(childNodes.val().finalScore);
    array2.push(childNodes.val().nameOfPlayer);

    var data = [{
    type: 'bar',
    x: [array],
    y: [array2],
    orientation: 'h'
  }];
  Plotly.newPlot('graphDiv', data);


      console.log(array);
      console.log(array2);

  });
  console.log("New array "+this.array);

此处的控制台日志未定义,因为我无法访问 for each 循环内的数组值。我尝试了又尝试,但仍然没有运气。谢谢你们的时间。

【问题讨论】:

    标签: javascript arrays firebase web plotly


    【解决方案1】:

    别担心解决了,我在括号内使用了 [array] 而不仅仅是 array

     var data = [{
        type: 'bar',
        x: array,
        y: array2,
        orientation: 'h'
      }];
      Plotly.newPlot('graphDiv', data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      相关资源
      最近更新 更多