Variable in the EJS

Asked

Viewed 455 times

1

Good morning, everyone!

So, come on: I’m trying to use Plotly to generate a bar graph, and it accepts variables. I have a connection on NODE.JS with a database and, NODE.JS in thesis (After a console.log on the route) is passing to the view which is the JSON object with all the values of the database I will use. What I’d like to know is... How can I use a variable in the view to store only the numeric values that will be used in the graph (No X)? Even because, if I have a value or 20 numerical values, the corresponding lines should be generated... Should I put this inside a is? The same logic I will use with the name (No Y)... Route:

    app.get('/barras', function(req,res){
    console.log('Database connection online');
    connection.query('select progresso from barra', function(error,result){
        barras = result;
        console.log(barras);
        res.render('admin/barras',barras);
    });
});

View:

    <!DOCTYPE html>
<html>
<head>
  <!-- Plotly.js -->
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>

<body>

  <div id="myDiv" style="width: 480px; height: 400px;"><!-- Plotly chart will be drawn inside this DIV --></div>
  <script>
  var barras = barras;
  console.log(barras);
  var data = [{
      type: 'bar',
      x: [17,20,84],
      y: ['teste1', 'teste2', 'teste3'],
      orientation: 'h'
  }];

Plotly.newPlot('myDiv', data);
  </script>
</body>
</html>

The JSON that is sent is [Rowdatapacket { progress: 11 },Rowdatapacket { progress: 10}, Rowdatapacket { progress: 20 }]

  • I have tried to declare to var alone within the <% %>...

  • I don’t understand it very well, but do you want to extract the numerical values from the search? If it is, it can be foreach or map.

  • Exactly. The point is that I was able to extract, the declaration of receipt was missing in a variable. bars = <%= JSON.parse(bars[i].progress) %>; and the variable in the graph later. The point now would be to make a way in which regardless of how many values are returned, the application can generate the corresponding number of bars. With foreach you can’t...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.