Node.js 如何在节点中将图形渲染为图像
我想在服务器上以图像格式渲染堆叠条形图 其预期用途是推送到不支持SVG的twitter等服务。以及可部署到Heroku等服务的代码Node.js 如何在节点中将图形渲染为图像,node.js,image,graph,visualization,Node.js,Image,Graph,Visualization,我想在服务器上以图像格式渲染堆叠条形图 其预期用途是推送到不支持SVG的twitter等服务。以及可部署到Heroku等服务的代码 我已经试过了(他们的节点包非常过时,API文档也很差)。我也看过Google Graph、Chart.js和AnyChart,但它们不支持渲染我所看到的图像 Vega是一种可视化语法,一种用于创建、保存和共享交互式可视化设计的声明性格式。使用Vega,您可以用JSON格式描述数据可视化,并使用HTML5画布或SVG生成交互式视图 例如,使用可以使用以下代码将图表渲染
我已经试过了(他们的节点包非常过时,API文档也很差)。我也看过Google Graph、Chart.js和AnyChart,但它们不支持渲染我所看到的图像 Vega是一种可视化语法,一种用于创建、保存和共享交互式可视化设计的声明性格式。使用Vega,您可以用JSON格式描述数据可视化,并使用HTML5画布或SVG生成交互式视图 例如,使用可以使用以下代码将图表渲染为PNG文件:
// START vega-demo.js
var vega = require('vega')
var fs = require('fs')
var stackedBarChartSpec = require('./stacked-bar-chart.spec.json');
// create a new view instance for a given Vega JSON spec
var view = new vega
.View(vega.parse(stackedBarChartSpec))
.renderer('none')
.initialize();
// generate static PNG file from chart
view
.toCanvas()
.then(function (canvas) {
// process node-canvas instance for example, generate a PNG stream to write var
// stream = canvas.createPNGStream();
console.log('Writing PNG to file...')
fs.writeFile('stackedBarChart.png', canvas.toBuffer())
})
.catch(function (err) {
console.log("Error writing PNG to file:")
console.error(err)
});
// END vega-demo.js
// START stacked-bar-chart.spec.json
{
"$schema": "https://vega.github.io/schema/vega/v3.0.json",
"width": 500,
"height": 200,
"padding": 5,
"data": [
{
"name": "table",
"values": [
{"x": 0, "y": 28, "c":0}, {"x": 0, "y": 55, "c":1},
{"x": 1, "y": 43, "c":0}, {"x": 1, "y": 91, "c":1},
{"x": 2, "y": 81, "c":0}, {"x": 2, "y": 53, "c":1},
{"x": 3, "y": 19, "c":0}, {"x": 3, "y": 87, "c":1},
{"x": 4, "y": 52, "c":0}, {"x": 4, "y": 48, "c":1},
{"x": 5, "y": 24, "c":0}, {"x": 5, "y": 49, "c":1},
{"x": 6, "y": 87, "c":0}, {"x": 6, "y": 66, "c":1},
{"x": 7, "y": 17, "c":0}, {"x": 7, "y": 27, "c":1},
{"x": 8, "y": 68, "c":0}, {"x": 8, "y": 16, "c":1},
{"x": 9, "y": 49, "c":0}, {"x": 9, "y": 15, "c":1}
],
"transform": [
{
"type": "stack",
"groupby": ["x"],
"sort": {"field": "c"},
"field": "y"
}
]
}
],
"scales": [
{
"name": "x",
"type": "band",
"range": "width",
"domain": {"data": "table", "field": "x"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true, "zero": true,
"domain": {"data": "table", "field": "y1"}
},
{
"name": "color",
"type": "ordinal",
"range": "category",
"domain": {"data": "table", "field": "c"}
}
],
"axes": [
{"orient": "bottom", "scale": "x", "zindex": 1},
{"orient": "left", "scale": "y", "zindex": 1}
],
"marks": [
{
"type": "rect",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "x"},
"width": {"scale": "x", "band": 1, "offset": -1},
"y": {"scale": "y", "field": "y0"},
"y2": {"scale": "y", "field": "y1"},
"fill": {"scale": "color", "field": "c"}
},
"update": {
"fillOpacity": {"value": 1}
},
"hover": {
"fillOpacity": {"value": 0.5}
}
}
}
]
}
// END stacked-bar-chart.spec.json
将输出PNG文件:
我使用噩梦无头浏览器在Node.js下捕获图表、可视化和报告 使用“噩梦”可以使用Node.js下各种各样的基于浏览器的可视化框架,包括和,它们都很棒 实际上,我已经创建了一个名为“结束噩梦”的npm模块,它允许您通过向Node.js下提供一些数据和C3图表定义来呈现图表 按如下方式安装:
npm install --save c3-chart-maker
const c3ChartMaker = require('c3-chart-maker');
const yourData = ... your data ...
const chartDefinition = { ... c3 chart definition ... }
const outputFilePath = "your-chart-output-file.png";
c3ChartMaker(yourData, chartDefinition, outputFilePath)
.then(() => {
console.log('Done');
})
.catch(err => {
console.error(err);
});
像这样使用它:
npm install --save c3-chart-maker
const c3ChartMaker = require('c3-chart-maker');
const yourData = ... your data ...
const chartDefinition = { ... c3 chart definition ... }
const outputFilePath = "your-chart-output-file.png";
c3ChartMaker(yourData, chartDefinition, outputFilePath)
.then(() => {
console.log('Done');
})
.catch(err => {
console.error(err);
});
请查看图表示例并查看C3图表定义的外观
您还可以手动使用梦魇来捕获任何网页或基于浏览器的可视化
要安装噩梦,请执行以下操作:
npm install --save nightmare
下面是一个可以捕获网页的示例:
const Nightmare = require('nightmare');
// This is the web page to capture.
// It can also be a local web server!
// Or serve from the file system using file://
const urlToCapture = "http://my-visualization.com";
const outputFilePath = "your-chart-output-file.png";
const nightmare = new Nightmare(); // Create Nightmare instance.
nightmare.goto(urlToCapture) // Point the browser at the requested web page.
.wait("svg") // Wait until the specified HTML element appears on the screen.
.screenshot(outputImagePath) // Capture a screenshot to an image file.
.end() // End the Nightmare session. Any queued operations are completed and the headless browser is terminated.
.then(() => {
console.log("Done!");
})
.catch(err => {
console.error(err);
});
在我的书中,我还专门用了整整一章来阐述这一点。Vega是我最喜欢的,它既有情节性的,也有我发现的几个不同的用于包装图形的库。起初,我和织女星搏斗,但通过使用Codepen上的操场,我能够找出哪里出了问题。现在它肯定是我的最爱:DAlso,在vega之上有一个更高级别的api。如果您首先想要简单性(而不是灵活性),那么值得检查一下