Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138192

How to use Vega to generate plots in the backend while using express.js

$
0
0

https://vega.github.io/vega/usage/#node There are examples for rendering plots using front end javascript. Is it possible to generate plots in the backend and send the plot to front end? I think I want to achieve something like this:

const vega = require('vega')
const lite = require('vega-lite')
const embed = require('vega-embed')
var yourVlSpec = {
  $schema: 'https://vega.github.io/schema/vega-lite/v2.0.json',
  description: 'A simple bar chart with embedded data.',
  data: {
    values: [
      {a: 'A', b: 28},
      {a: 'B', b: 55},
      {a: 'C', b: 43},
      {a: 'D', b: 91},
      {a: 'E', b: 81},
      {a: 'F', b: 53},
      {a: 'G', b: 19},
      {a: 'H', b: 87},
      {a: 'I', b: 52}
    ]
  },
  mark: 'bar',
  encoding: {
    x: {field: 'a', type: 'ordinal'},
    y: {field: 'b', type: 'quantitative'}
  }
};
let vegaspec = lite.compile(yourVlSpec)
var view = new vega.View(vega.parse(vegaspec), 
{renderer: 'none'})


var express = require('express');
var app = express();

app.get('/', function(req, res){
//   Can I send view directly to the front end?
//   res.send(view.toHTML());
});
app.listen(3000);

Viewing all articles
Browse latest Browse all 138192

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>