上下文是包含图表的this
DOM 元素:即g
元素。将一些变量绑定到 DOM 元素,例如this.myvar = state
,提供了一种处理图表特定状态的方法。对一个特定图表 g 元素的多次更新调用,将都可以访问同一个变量。
Mike 和 Jason__chart__
在各种图表以及d3 轴组件中使用了属性名称,以跟踪图表的特定状态。
没错,在这种情况下,它是存储在 g 元素__chart__
属性中的比例。请参阅 bullet.js 的摘录:
// Compute the new x-scale.
var x1 = d3.scale.linear()
.domain([0, Math.max(rangez[0], markerz[0], measurez[0])])
.range(reverse ? [width, 0] : [0, width]);
// Retrieve the old x-scale, if this is an update.
var x0 = this.__chart__ || d3.scale.linear()
.domain([0, Infinity])
.range(x1.range());
// Stash the new scale.
this.__chart__ = x1;
x1
因此,根据当前数据确定比例。它将存储在 中__chart__
,以供将来使用新数据更新此图表时使用。
The previous scale is taken from this.__chart__
and kept in x0
. The this.__chart__
will return undefined
when the chart is just being constructed (i.e. the enter phase). In that case x0
will instead become d3.scale.linear().domain([0, Infinity]).range(x1.range())
. See short circuit eval.
The old scale is needed for smooth transition. When new data points are entered, we first want to plot them on the chart using the old scale. After that we will transition all points (new and updated) according to the new scale.
Regarding the [0, Infinity] domain. So, a scale with this domain will only be used when the chart is just constructed. That means it provides a way to setup the initial transition when introducing the chart. A infinite domain with a finite range means that all points are scaled to 0. So, when the chart is set up, all points will be plotted at 0 and transition to the proper values according the x1 scale.