D3画笔+缩放:修复更新延迟(请参见GIF)

时间:2019-04-17 17:48:22

标签: d3.js

解决此示例https://bl.ocks.org/mbostock/34f08d5e11952a80609169b7917d4172

我正在寻找为数据点本身添加元素的方法,我已经使用圆圈完成了此操作(然后,将鼠标悬停在显示元数据的位置)。

问题在于,使用笔刷或缩放功能时会有延迟。

如何消除面积重新计算和圆圈位置重新计算之间的延迟?

enter image description here

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 110, left: 40},
    margin2 = {top: 430, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

var parsetimestamp = d3.timeParse("%Y%m%d-%H%M");

var x = d3.scaleTime().range([0, width]),
    x2 = d3.scaleTime().range([0, width]),
    y = d3.scaleLinear().range([height, 0]),
    y2 = d3.scaleLinear().range([height2, 0]);

var xAxis = d3.axisBottom(x),
    xAxis2 = d3.axisBottom(x2),
    yAxis = d3.axisLeft(y);

var brush = d3.brushX()
    .extent([[0, 0], [width, height2]])
    .on("brush end", brushed);

var zoom = d3.zoom()
    .scaleExtent([1, Infinity])
    .translateExtent([[0, 0], [width, height]])
    .extent([[0, 0], [width, height]])
    .on("zoom", zoomed);

var area = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x(d.timestamp); })
    .y0(height)
    .y1(function(d) { return y(d.value); });

var area2 = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x2(d.timestamp); })
    .y0(height2)
    .y1(function(d) { return y2(d.value); });

svg.append("defs").append("clipPath")
    .attr("id", "clip")
  .append("rect")
    .attr("width", width)
    .attr("height", height);

var focus = svg.append("g")
    .attr("class", "focus")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var context = svg.append("g")
    .attr("class", "context")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

d3.json(file).then(function(data) {

data.forEach(function(d) {
    d.timestamp = parsetimestamp(d.timestamp);
})

console.log(data)

  x.domain(d3.extent(data, function(d) { return d.timestamp; }));
  y.domain([0, 1]);  // NOTE: this assumes 0-1, like precision/recall/accuracy etc.
  x2.domain(x.domain());
  y2.domain(y.domain());

  focus.append("path")
      .datum(data)
      .attr("class", "top area")
      .attr("d", area);

  focus.append("g")
        .attr("class", "scatter-points")
        .selectAll("circle")
            .data(data)
        .enter()
            .append("circle")
            .attr("class", "scatter-point")
            .attr("cx", function(d) { return x(d.timestamp); })
            .attr("cy", function(d) { return y(d.value); })
            .attr("r", 3)

  focus.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  focus.append("g")
      .attr("class", "axis axis--y")
      .call(yAxis);

  context.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area2);

  context.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height2 + ")")
      .call(xAxis2);

  context.append("g")
      .attr("class", "brush")
      .call(brush)
      .call(brush.move, x.range());

  svg.append("rect")
      .attr("class", "zoom")
      .attr("width", width)
      .attr("height", height)
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
      .call(zoom);
});

function brushed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type  === "zoom") return; // ignore brush-by-zoom
  var s = d3.event.selection || x2.range();
  x.domain(s.map(x2.invert, x2));
  focus.select(".area").attr("d", area);
  focus.select(".axis--x").call(xAxis);
  svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (s[1] - s[0]))
      .translate(-s[0], 0));
}

function zoomed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
  var t = d3.event.transform;
  x.domain(t.rescaleX(x2).domain());
  focus.select(".area").attr("d", area);
  focus.select(".axis--x").call(xAxis);
  focus.selectAll(".scatter-point").transition()
    .attr("cx", function(d) { return x(d.timestamp); });
  context.select(".brush").call(brush.move, x.range().map(t.invertX, t));

1 个答案:

答案 0 :(得分:0)

事实证明,过渡的默认持续时间为一些有意义的值。

zoomed()brushed()中的过渡持续时间配置为接近零的值就可以解决此问题。

    ...
    focus.selectAll(".scatter-point").transition()
      .duration(10)