D3面积图从底部过渡

时间:2018-07-26 18:26:05

标签: d3.js

我想为this的示例制作动画,并从x轴向上过渡。.我研究了this的示例,但控制y1()却相反。

2 个答案:

答案 0 :(得分:1)

您可以先用y1 = height将区域初始化为一条简单的线,然后再玩transition,它将向最后一个区域插值:

// initial area y = 0:
var initialarea = d3.area()
  .x(function(d) { return x(d.date); })
  .y0(height)
  .y1(height);

// the area in its final state
var area = d3.area()
  .x(function(d) { return x(d.date); })
  .y0(height)
  .y1(function(d) { return y(d.close); });

svg.append("path")
  .data([data])
  .attr("class", "area")
  .attr("d", initialarea) // initial state (line at the bottom)
  .transition().duration(1000)
  .attr("d", area); // final state

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 2px;
}

.area {
  fill: lightsteelblue;
}

</style>
<body>

<!-- load the d3.js library -->
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

  var data = [
    { date: "1-May-12", close: "58.13" },
    { date: "30-Apr-12", close: "53.98" },
    { date: "27-Apr-12", close: "67.00" },
    { date: "26-Apr-12", close: "89.70" },
    { date: "25-Apr-12", close: "99.00" },
    { date: "24-Apr-12", close: "130.28" },
    { date: "23-Apr-12", close: "166.70" },
    { date: "20-Apr-12", close: "234.98" },
    { date: "19-Apr-12", close: "345.44" },
    { date: "18-Apr-12", close: "443.34" },
    { date: "17-Apr-12", close: "543.70" },
    { date: "16-Apr-12", close: "580.13" },
    { date: "13-Apr-12", close: "605.23" },
    { date: "12-Apr-12", close: "622.77" },
    { date: "11-Apr-12", close: "626.20" },
    { date: "10-Apr-12", close: "628.44" },
    { date: "9-Apr-12", close: "636.23" },
    { date: "5-Apr-12", close: "633.68" },
    { date: "4-Apr-12", close: "624.31" },
    { date: "3-Apr-12", close: "629.32" },
    { date: "2-Apr-12", close: "618.63" },
    { date: "30-Mar-12", close: "599.55" },
    { date: "29-Mar-12", close: "609.86" },
    { date: "28-Mar-12", close: "617.62" },
    { date: "27-Mar-12", close: "614.48" },
    { date: "26-Mar-12", close: "606.98" }
  ];

  // set the dimensions and margins of the graph
  var margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = 400 - margin.left - margin.right,
    height = 200 - margin.top - margin.bottom;

  // parse the date / time
  var parseTime = d3.timeParse("%d-%b-%y");

  // set the ranges
  var x = d3.scaleTime().range([0, width]);
  var y = d3.scaleLinear().range([height, 0]);

  // define the area
  var area = d3.area()
    .x(function(d) { return x(d.date); })
    .y0(height)
    .y1(function(d) { return y(d.close); });

  // initial area y = 0:
  var initialarea = d3.area()
    .x(function(d) { return x(d.date); })
    .y0(height)
    .y1(height);

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

  // initial line y = 0:
  var initialvalueline = d3.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return height; });

  // append the svg obgect to the body of the page
  // appends a 'group' element to 'svg'
  // moves the 'group' element to the top left margin
  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 + ")");

  // get the data
  // d3.csv("data.csv", function(error, data) {
  //   if (error) throw error;

    // format the data
    data.forEach(function(d) {
      d.date = parseTime(d.date);
      d.close = +d.close;
    });

    // 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; })]);

    // add the area
    svg.append("path")
      .data([data])
      .attr("class", "area")
      .attr("d", initialarea)
      .transition().duration(1000)
      .attr("d", area);

    // add the valueline path.
    svg.append("path")
      .data([data])
      .attr("class", "line")
      .attr("d", initialvalueline)
      .transition().duration(1000)
      .attr("d", valueline);

    // add the X Axis
    svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

    // add the Y Axis
    svg.append("g")
      .call(d3.axisLeft(y));

  // });

</script>
</body>

答案 1 :(得分:0)

以下是将两者组合在一起的一个块:

https://bl.ocks.org/shashank2104/c55bf4c0c61e0e4f93e885a15724f8b4/2fd6f43a89cd9c76ecdc432e74f2e4351650af67

更改: 从y1y(0)的{​​{1}}

这是区域功能:

y(d.close)

编辑:还更新了// define the area var area = function (datum, boolean) { return (d3.area() .x(function(d) { return x(d.date); }) .y0(height) .y1(function(d) { return boolean ? y(d.close) : y(0); }))(datum); }; (行功能)(我以前很想念它,这很糟糕)。更新了链接

valueline

希望这会有所帮助。