(警告:這是一個非常哈克解決方案,知道你有什麼樣的規模。如果你不舒服,請讓我知道,我會刪除它。)
每檯秤在D3 v4.x中有一個copy
函數,它公開了該尺度的類型。舉例來說,這是一個線性規模copy
功能:
scale.copy = function() {
return copy(scale, linear());
};
這是一種帶刻度的copy
功能:
scale.copy = function() {
return band()
.domain(domain())
.range(range)
.round(round)
.paddingInner(paddingInner)
.paddingOuter(paddingOuter)
.align(align);
};
等等......
,你可以請參閱類型的比例始終存在於返回值中(如果您使用D3的非縮小版本)。因此,我們可以用它來比較尺度(比較領域和範圍,就像你現在正在做的那樣)。這裏有一個例子:
var scale1 = d3.scaleBand();
var scale2 = d3.scaleLinear();
var scale3 = d3.scaleLinear();
console.log("Is scale1 the same of scale2? " + (scale1.copy.toString() === scale2.copy.toString()))
console.log("Is scale1 the same of scale3? " + (scale1.copy.toString() === scale3.copy.toString()))
console.log("Is scale2 the same of scale3? " + (scale2.copy.toString() === scale3.copy.toString()))
<script src="https://d3js.org/d3.v4.min.js"></script>
而這可以用來測試具有相同的域和範圍(如你在你的問題問)不同的尺度:
var scale1 = d3.scaleLinear()
.domain([10, 20])
.range([10, 20]);
var scale2 = d3.scaleLog()
.domain([10, 20])
.range([10, 20]);
console.log("Is scale1 the same of scale2? " + (scale1.copy.toString() === scale2.copy.toString()))
<script src="https://d3js.org/d3.v4.min.js"></script>
編輯:而不是比較copy
,它是比較整個功能是一個更好的主意。正如comments section中@altocumulus指出的那樣,一些比例尺返回相同的copy
。
我認爲你的問題的最佳答案取決於你是否在渲染方法中使用d3,或者是否在其他生命週期方法中使用d3選擇器。 –
如何使用一些實用變量來確定您定義的點的縮放類型,例如'var isLinear = true;'並在您更改縮放比例時對其進行修改。 – mtx