私は、スライドするx軸を持つd3を使ってグラフを実装しています。 Demod3 v4でトランジションの再生を修正するにはどうすればよいですか?
問題は、別のタブに変更して戻ったとき(たとえば10秒後)に、d3が欠落したトランジションを再生しようとしているように見えて、軸の動作が非常に面倒です。 hereを参照してください。
マイク・ボストックmentions that:
D3 4.0フィックス時間の定義を変更することでこの問題。遷移は通常、絶対時間と同期する必要はありません。トランジションは、ビュー全体でオブジェクトを追跡するための主な知覚支援ツールです。したがって、D3 4.0は、認識された時間に実行されます。これは、ページがフォアグラウンドにある場合にのみ前進します。タブがバックグラウンドにされ、フォアグラウンドに戻されると、何も起こっていなかったかのように単に取り上げられます。
これは本当に修正されていますか?私は間違って何かしていますか?
const timeWindow = 10000;
const transitionDuration = 3000;
const xScaleDomain = (now = new Date()) =>
[now - timeWindow, now];
const totalWidth = 500;
const totalHeight = 200;
const margin = {
top: 30,
right: 50,
bottom: 30,
left: 50
};
const width = totalWidth - margin.left - margin.right;
const height = totalHeight - margin.top - margin.bottom;
const svg = d3.select('.chart')
.append('svg')
.attr('width', totalWidth)
.attr('height', totalHeight)
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`)
svg
.append('rect')
.attr('width', width)
.attr('height', height);
// Add x axis
const xScale = d3.scaleTime()
.domain(xScaleDomain(new Date() - transitionDuration))
.range([0, width]);
const xAxis = d3.axisBottom(xScale);
const xAxisSelection = svg
.append('g')
.attr('transform', `translate(0, ${height})`)
.call(xAxis);
// Animate
const animate =() => {
xScale.domain(xScaleDomain());
xAxisSelection
.transition()
.duration(transitionDuration)
.ease(d3.easeLinear)
.call(xAxis)
.on('end', animate);
};
animate();
svg {
margin: 30px;
background-color: #ccc;
}
rect {
fill: #fff;
outline: 1px dashed #ddd;
}
<script src="https://unpkg.com/[email protected]/build/d3.js"></script>
<div class="chart"></div>
ありがとうございます!意味あり。 d3は「遷移の一時停止」と「遷移の再開」のフックを提供するので、時間差を測定できますか? –
私の頭の上からは、ネイティブではないと思うが、あなた自身の機能を作ることができる。 –