javascript - 仅在线点的 y 值上过渡?

标签 javascript d3.js

我使用 D3.js 创建了一个折线图,用于显示基于时间的一系列值。图表可以平移和缩放。为了改善用户体验,我还在 Y 轴上实现了自动缩放,它将 y 轴的域设置为当前可见数据范围的值 [min, max]。

现在我想向图表添加一个过渡,以动画方式显示 y 轴刻度 (like in this example) 的变化。它有效,但不幸的是,它在平移图表时看起来和表现都很糟糕,因为过渡不仅使每个线点的 y 部分动画化,而且还使 x 部分动画化,这导致水平涂抹线的非常不愉快的效果以及平移图表时明显的滞后。就是感觉不对。

所以我想实现的是:线数据的 x 属性应该立即设置而不进行过渡以避免延迟,y 属性应该是动画的。这是我更新图表的部分:

self.svg.select("path.line").transition().attr("d", self.valueline);

valueline 看起来像这样:

self.valueline = d3.svg.line()
  .x(function (d) { return self.x(d.t); })
  .y(function (d) { return self.y(d.v); });

有没有办法只将转换应用到 d.v(值)?

最佳答案

首先,示例中的方法不是很惯用,最好先将数据绑定(bind)到路径,然后再应用转换。

然后您可以在开始时使用所有 d.v == 0 绑定(bind)一些空数据,然后使用转换绑定(bind)真实数据。这给出了仅 y 值的转换。 The following edits in the example将显示我的意思...

// Set the dimensions of the canvas / graph
  var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 600 - margin.left - margin.right,
    height = 270 - margin.top - margin.bottom;

  // Parse the date / time
  var parseDate = d3.time.format("%d-%b-%y").parse;

  // Set the ranges
  var x = d3.time.scale().range([0, width]);
  var y = d3.scale.linear().range([height, 0]);

  // Define the axes
  var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(5);

  var yAxis = d3.svg.axis().scale(y)
    .orient("left").ticks(5);

  // Define the line
  var valueline = d3.svg.line()
    .x(function (d) {
      return x(d.date);
    })
    .y(function (d) {
      return y(d.close);
    });

  // Adds the svg canvas
  var svg = d3.select("body")
    .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform",
    "translate(" + margin.left + "," + margin.top + ")");
  //EDIT ********************************************
  var line;

  // Get the data
  d3.csv("data.csv", function (error, data) {
    data.forEach(function (d) {
      d.date = parseDate(d.date);
      d.close = +d.close;
    });
    var entryData = data.map(function (d) {
      return ({date: d.date, close: 0})
    })

    // Scale the range of the data
    x.domain(d3.extent(data, function (d) {
      return d.date;
    }));
    y.domain([0, d3.max(data, function (d) {
      return d.close;
    })]);

    //Bind the data first THEN add the valueline path.
    //Start with all zero y values
    line = svg.selectAll("line").data([entryData]);
    line.enter().append("path")
      .attr("class", "line")
      .attr("d", valueline);
    // Transition initial data, y values only
    line.data([data]);
    line.transition().delay(500).call(trans, "entry")
      // Add the valueline path.
      .attr("d", valueline);

    // Add the X Axis
    svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

    // Add the Y Axis with entry transition
    var gYaxis = svg.append("g")
      .attr("class", "y axis");
    //Null axis starting point
    y.domain([0, 0]);
    gYaxis.call(yAxis);
    //Final axis after entry transition
    y.domain([0, d3.max(data, function (d) {
      return d.close;
    })]);
    gYaxis.transition().delay(500).call(trans, "entry")
      .call(yAxis);

  });

  // ** Update data section (Called from the onclick)
  function updateData() {

    // Get the data again
    d3.csv("data-alt.csv", function (error, data) {
      data.forEach(function (d) {
        d.date = parseDate(d.date);
        d.close = +d.close;
      });

      // Scale the range of the data again
      x.domain(d3.extent(data, function (d) {
        return d.date;
      }));
      y.domain([0, d3.max(data, function (d) {
        return d.close;
      })]);

      //EDIT ********************************************
      // Bind the new data and then transition
      line = svg.selectAll(".line").data([data]);
      line.enter().append("path")
        .attr("class", "line")
      line.transition().call(trans)
        // Add the valueline path.
        .attr("d", valueline);

      // Select the section we want to apply our changes to
      var svgTrans = d3.select("body").transition();

      // Make the changes
      svgTrans.select(".x.axis") // change the x axis
        .call(trans)
        .call(xAxis);
      svgTrans.select(".y.axis") // change the y axis
        .call(trans)
        .call(yAxis);

    });
  }
  function trans (transition, name){
    var delays = {normal:0, entry: 500};
    name = name || "normal";
    transition.duration(750).delay(delays[name]).ease("sin-in-out")
  }

关于javascript - 仅在线点的 y 值上过渡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31179599/

相关文章:

javascript - d3.js arc.centroid(d): Error: Invalid value for <text> attribute transform ="translate(NaN,NaN)"

javascript - 通过 SVG 背景传递鼠标事件

用于房间 3D 演示的 Javascript 库

javascript - 使用源映射还原 JavaScript 缩小

javascript - 如何在 JSP/Struts WEB-INF 中获取资源

javascript - y 轴垂直范围自动勾选为 5,即使我更改为不同的数字

javascript - 在 Dimple.js 中向行添加工具提示

javascript - 我如何使用 querySelector() 选择具有双类的元素

javascript - d3.js 树 - 为指定的子节点设置深度?

javascript - d3 - 如何在数组上设置动画