简体   繁体   English

使用javascript垂直和水平对齐图的策略

[英]Strategy for aligning graphs vertically and horizontally using javascript

I would like to align different views of the same data set using javascript ( d3.js in particular). 我想使用javascript (尤其是d3.js )对齐同一数据集的不同视图。 This will eventually be interactive, but I am including a static example to show what I mean. 最终这将是交互式的,但是我将提供一个静态示例来说明我的意思。 The contour plot and the two slices/projections (corresponding to the cursor in the contour plot) need to remain aligned/in register. 等高线图和两个切片/投影(对应于等高线图中的光标)需要保持对齐/对齐。

I'm in need of a suggestion about a strategy to accomplish this, or perhaps a pointer to an example. 我需要有关实现此目标的策略的建议,或者可能是指向示例的指针。 The majority of the examples I have studied have graphs which are loosely related and don't need to be aligned, and those that do align do so in the vertical direction. 我研究的大多数示例都具有松散相关的图形,不需要对齐,而那些对齐的图形则在垂直方向上对齐。 The horizontal direction seems much more challenging. 水平方向似乎更具挑战性。 I saw this SO question which may be relevant, but I was hoping there was a pure javascript means of ensuring the alignment. 我看到这个SO问题,这可能是相关的,但我希望有一个纯粹javascript确保对准装置。

I wish I could give you some sample code I already tried but I'm really so early in this process I'm not even sure which way to go. 我希望可以给我一些我已经尝试过的示例代码,但是我确实在这个过程的早期,甚​​至不确定该走哪条路。 Vote me down if you must but I'd sure appreciate some suggestions. 如果需要的话,请否决我,但我肯定会建议您。

在此处输入图片说明

While its not a complete solution to your problem, I thought I would help you get started with an overview and simple (if a bit contrived) example: 尽管它不能完全解决您的问题,但我认为我可以帮助您开始进行概述和简单的示例(如果有些人为作弊):

http://jsfiddle.net/TxZK3/3/ http://jsfiddle.net/TxZK3/3/

In this example, I've taken a contour plot--straight from the D3 examples page--and placed it side-by-side with a copy of itself. 在此示例中,我直接从D3示例页面获取了轮廓图,并将其与自身副本并排放置。 The CSS doesn't reflect how you would implement this exactly, but rather illustrates that such a thing can be done and further that maintaining the same scaling for the SVG viewspace is how you can properly line things up. CSS并未反映出您将如何精确地实现此目的,而是说明了可以完成此事,并且进一步说明了如何正确地排列SVG视图空间的比例。 In short: 简而言之:

  1. Create 2 SVG's with the same width and height 创建两个具有相同宽度和高度的SVG
  2. Use CSS to move them next to each other 使用CSS将它们彼此相邻移动

That setup is exemplified by the following snippet: 以下代码段说明了该设置:

var margin = {top: 20, right: 20, bottom: 30, left: 30},
    width = 960/500 * 150 - margin.left - margin.right,
    height = 150 - margin.top - margin.bottom;

These values are shared by both SVGs in my example and thus mean they draw into the same sized space. 在我的示例中, 这两个 SVG共享了这些值,因此意味着它们会占用相同大小的空间。

After that, it will be a matter of creating a different visualization within each SVG space. 之后,将需要在每个SVG空间中创建不同的可视化文件。 When you create axis' and draw things into each SVG, D3 will scale them appropriately. 当您创建“轴”并将其绘制到每个SVG中时,D3将对其进行适当缩放。 Once the base graphs are setup the last piece of the puzzle will be making sure that each graph has the appropriate corresponding data to match each visualization. 一旦设置了基础图,难题的最后一部分就是确保每个图都有适当的对应数据以匹配每个可视化。 In your situation that would entail computing partial x or y over the same available axis as the main graph. 在您的情况下,将需要在与主图相同的可用轴上计算部分x或y。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM