简体   繁体   English

D3:刻度通过轴时会做什么?

[英]D3: What does the scale do when passed an axis?

In d3, if you want to create an axis you might do something like this: 在d3中,如果要创建轴,可以执行以下操作:

var xAxis = d3.svg.axis()
.scale(x)

where x is a scale function. 其中x是比例函数。 I understand that the domain of x defines the start and ending values for the ticks. 我知道x的域定义了报价的开始和结束值。 I'm having trouble understanding how the range of x changes the resulting axis. 我无法理解x的范围如何更改结果轴。 What does the domain map to in the context of an axis. 域在轴的上下文中映射到什么。

Think about what one must do to create a visual representation of any data set. 想一想如何创建任何数据集的可视化表示。 You must convert each data point (eg 1 million dollars) into a point on the screen. 您必须将每个数据点(例如1百万美元)转换为屏幕上的一个点。 If your data has a minimum value of $0 and maximum value of $1000000, you have a domain of 0 to 1000000. Now to represent your data on a computer screen you must convert each data point (eg $25) into a number of pixels. 如果数据的最小值为$ 0,最大值为$ 1000000,则域为0到1000000。现在要在计算机屏幕上表示数据,必须将每个数据点(例如$ 25)转换为许多像素。 You could try a simple 1 to 1 linear conversion ($25 converts to 25 pixels on the screen), in which case your range would be the same as your domain = 0 to 1000000. But this would require a bloody big screen. 您可以尝试简单的1到1线性转换($ 25转换为屏幕上的25像素),在这种情况下,您的范围将与您的域= 0到1000000相同。但这将需要一个大屏幕。 More likely we have an idea of how large we want the graphic to appear on the screen, so we set our range accordingly (eg 0 to 600). 我们更可能希望图形显示在屏幕上的大小,因此我们相应地设置了范围(例如0到600)。

The d3 scale function converts each data point in your dataset into a corresponding value within your range. d3比例函数将数据集中的每个数据点转换为您范围内的相应值。 That enables it to be presented on the screen. 这样就可以在屏幕上显示它。 The previous example is a simple conversion so the d3.scale() function is not doing much for you, but spend some time converting data points into a visual representation and you will quickly discover some situations where the scale function is doing a lot of work for you. 前面的示例是一个简单的转换,因此d3.scale()函数对您没有多大帮助,但是花一些时间将数据点转换为可视表示形式,您会很快发现scale函数正在做大量工作的情况为了你。

In the particular case of an axis, the scale function is doing exactly the same thing. 在轴的特定情况下,比例函数的作用完全相同。 It is doing the conversion (to pixels) for each 'tick' and placing them on the screen. 它正在为每个“刻度”进行转换(至像素),并将其放置在屏幕上。

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

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