简体   繁体   English

D3预渲染

[英]D3 Pre-Rendering

I'm still a beginner with JS + Python and I'd be really happy to get your help on one problem. 我仍然是JS + Python的初学者,我很乐意在一个问题上得到你的帮助。 What I basically need to to is to pre-render a d3js visualization of a network (as for example http://bl.ocks.org/mbostock/4062045 ) into a static page. 我基本上需要的是将网络的d3js可视化(例如http://bl.ocks.org/mbostock/4062045预渲染到静态页面中。 The main aim is to reduce the pageload. 主要目的是减少页面负载。 As a side-effect, a static page would also be a better input for scripts that render a png that could be used for thumbnails of the graph. 作为副作用,静态页面对于呈现可用于图形缩略图的png的脚本也是更好的输入。

So I imagine one site where I run the Force Directed algorithm once and then store the values of final node positions into a static file (where the node positions then are embedded). 所以我想象一个我运行Force Directed算法的站点,然后将最终节点位置的值存储到静态文件中(然后嵌入节点位置)。 The system is currently running with Python/Django on Google App Engine. 该系统目前在Google App Engine上使用Python / Django运行。 Is that possible? 那可能吗?

Code examples/Fiddle would be great! 代码示例/小提琴会很棒!

Disclosure : I have read this one here d3js large force-directed graph server side simulation and this one http://mango-is.com/blog/engineering/pre-render-d3-js-charts-at-server-side.html 披露 :我在这里读过这个d3js大型强制导向图服务器端模拟 ,这个http://mango-is.com/blog/engineering/pre-render-d3-js-charts-at-server-side。 HTML

You can satisfy both requirements with CasperJS . 您可以使用CasperJS满足这两个要求。 Casper is a library that will use a headless browser to request a web page, which it can also programmatically interact with. Casper是一个使用无头浏览器来请求网页的库,它也可以通过编程方式与之交互。 A simple snippet of how to grab a screen shot goes something like this: 如何获取屏幕截图的简单片段如下:

casper.start('http://ninjaPixel.io/', function() {
  this.capture('page.png', undefined, {
      format: 'png'
  });
});

You can add a delay to this, to make sure that your force diagram is in a stable state when you take the screen shot. 您可以为此添加延迟,以确保在拍摄屏幕截图时力图处于稳定状态。 Casper can grab anything off the page, so if you display your computed node values in a html table, for example, tell Casper the id of that table and it can then grab it and save the values for you. Casper可以抓取页面上的任何内容,因此,如果您在html表中显示计算出的节点值,请告诉Casper该表的ID,然后它可以抓取它并为您保存值。 You could even save the SVG object generated by your d3.js code. 您甚至可以保存由d3.js代码生成的SVG对象。

This excellent graphic (by mbostock for nytimes) shows how to render a static graph layout in the DOM based on pre-computed positions. 这个优秀的图形 (由nytimes的mbostock)显示了如何基于预先计算的位置在DOM中呈现静态图形布局。 All the code is on the main html doc, so just "view source". 所有代码都在主html文档中,所以只是“查看源代码”。

It first loads the data, and then calls ready : 它首先加载数据,然后调用ready

queue()
  .defer(d3.json, "http://graphics8.nytimes.com/newsgraphics/2013/09/07/director-star-chart/ed9eaa686bc2e11f0657a78d9be292a730c0567a/graph.json")
  .defer(d3.json, "http://graphics8.nytimes.com/newsgraphics/2013/09/07/director-star-chart/ed9eaa686bc2e11f0657a78d9be292a730c0567a/layout.json")
  .await(ready);

If you dig look inside the .json files, you'll get a sense of how they chose to capture the graph data. 如果您仔细查看.json文件,您将了解他们如何选择捕获图表数据。

The ready handler begins like this: ready处理程序像这样开始:

function ready(error, graph, positions) {
  force
    .nodes(graph.nodes)
    .links(graph.links)
    .on("tick", ticked)
    .start();

  if (positions) force.alpha(.0051);

  ....

You can see that it's initializing a force layout and setting its alpha() to a very low number, signaling that the layout is essentially settled. 您可以看到它正在初始化一个力布局并将其alpha()设置为一个非常低的数字,表明布局基本上已经解决。 As a result, the tick handler ticked() probably only gets called one time, before the layout stops ticking due to the low alpha value. 因此,滴答处理程序ticked()可能只会在布局由于低alpha值而停止滴答之前被调用一次。

However, also note that the setting of alpha to a very low value is conditional, depending on positions being available. 但是,请注意,将alpha设置为非常低的值是有条件的,具体取决于可用的positions So if positions were not pre-loaded and hence would be undefined , the layout would tick repeatedly for a while until it settles on some positioning. 因此,如果positions没有预先加载并因此undefined ,则布局将反复打勾一段时间,直到它确定一些定位。 This suggests to me that the same code rendering this static layout was also used — probably with some additional code but without the calculated positions being loaded — during their process of exporting the positions. 这表明,我认为同样的代码渲染这个静态布局也使用了-可能与一些额外的代码,但没有计算的positions被加载-在其出口位置的过程。 That's really quite clever of them(!). 那真的很聪明(!)。

The code that was used to gather and write the positions to file does not appear to be part of this page's source code, which makes sense, but you can hopefully come up with a way to do that within your project environment. 用于收集位置并将其写入文件的代码似乎不是此页面源代码的一部分,这是有道理的,但您可以在项目环境中找到一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将HTML页面预渲染到图像中 - Pre-rendering html page into image 在JavaScript中的画布上预渲染动画 - Pre-Rendering an animation on a canvas in javascript GridLayout使用灰色框预渲染 - WinJS - GridLayout pre-rendering with grey boxes - WinJS 用文本预渲染画布矩形,这是不好的表现吗? - Pre-rendering a canvas rectangle with text, is this bad performance? 在实时Canvas中进行预渲染与渲染时结果奇怪 - Strange results when pre-rendering vs rendering in real-time Canvas d3中的渲染性能 - Rendering performance in d3 使用PhantomJS预渲染AngualrJS网站时“无法实例化模块ngSanitize” - “Failed to instantiate module ngSanitize” when pre-rendering AngualrJS site with PhantomJS 异步加载 vue.config.js 以预渲染元数据 - Asynchronous Loading of vue.config.js for Pre-Rendering Meta Data 我们可以使用Angular 2的服务器端预呈现来代替服务器模板引擎吗? - Can we use Angular 2's server-side pre-rendering in place of server templating engine? Next.js 带有 SSR 的应用程序未预渲染 HTML,因此网络抓取工具无法访问内容 - Next js app with SSR is not pre-rendering HTML, so web-scrapers cannot access the content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM