用d3js来绘制心跳示意图

December 19, 2014

资料

老外叫做 heartbeat line

这里有个简单的例子,不过是不会动的: http://bl.ocks.org/benjchristensen/2579599

这个例子就更直接了, 直接就是会动的: http://bl.ocks.org/mbostock/1642874

例子很详细了

<!DOCTYPE html>
<meta charset="utf-8">
<style>

svg {
  font: 10px sans-serif;
}

.line {
  fill: none;
  stroke: #000;
  stroke-width: 1.5px;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var n = 40,
    random = d3.random.normal(0, .2),
    data = d3.range(n).map(random);

var margin = {top: 20, right: 20, bottom: 20, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x = d3.scale.linear()
    .domain([0, n - 1])
    .range([0, width]);

var y = d3.scale.linear()
    .domain([-1, 1])
    .range([height, 0]);

var line = d3.svg.line()
    .x(function(d, i) { return x(i); })
    .y(function(d, i) { return y(d); });

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 + ")");

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

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + y(0) + ")")
    .call(d3.svg.axis().scale(x).orient("bottom"));

svg.append("g")
    .attr("class", "y axis")
    .call(d3.svg.axis().scale(y).orient("left"));

var path = svg.append("g")
    .attr("clip-path", "url(#clip)")
  .append("path")
    .datum(data)
    .attr("class", "line")
    .attr("d", line);

tick();

function tick() {

  // push a new data point onto the back
  data.push(random());

  // redraw the line, and slide it to the left
  path
      .attr("d", line)
      .attr("transform", null)
    .transition()
      .duration(500)
      .ease("linear")
      .attr("transform", "translate(" + x(-1) + ",0)")
      .each("end", tick);

  // pop the old data point off the front
  data.shift();

}

</script>

去掉 x y 轴

我要用的地方不能显示 x y 轴线,得去掉

瞬间被我找到,把这两段注释了就可以了

//svg.append("g")
//    .attr("class", "x axis")
//    .attr("transform", "translate(0," + y(0) + ")")
//    .call(d3.svg.axis().scale(x).orient("bottom"));
//
//svg.append("g")
//    .attr("class", "y axis")
//    .call(d3.svg.axis().scale(y).orient("left"));

按直线移动

没有给定数据的时候, 要按照直线移动, 不要跳

是用 tick 函数来进行跳动的

tick 使用

var n = 40,
    random = d3.random.normal(0, .2),
    data = d3.range(n).map(random);

来生成初始的数据.

其实很简单. 往 data 里面塞入0, 那就是直线了

其实已经很完善了, 需要的时候,塞一个数据, 那么就会跳了.

模拟心跳

还是用随机数来进行心跳, 这样比较自然一点, 否则都是固定值太假了

random = d3.random.normal(0, .2),

是生成 -0.2 到 0.2 之间的一个随机数

我要能控制正负, 不然会出来一个坑.

Math.random()

不能输入参数,我要保证在 0.5-1 之间会好一些

Math.random()*(1-0.5)+0.5

下一步需要设置大小

整个来说,修改成这样基本能够满足使用了.

<!DOCTYPE html>
<meta charset="utf-8">
<style>

svg {
  font: 10px sans-serif;
}

.line {
  fill: none;
  stroke: #000;
  stroke-width: 1.5px;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var n = 40,
    random = d3.random.normal(0, 0),
    //data = d3.range(n).map(0);
    data = d3.range(n).map(random);

var margin = {top: 20, right: 20, bottom: 20, left: 40},
    width = 200 - margin.left - margin.right,
    height = 65 - margin.top - margin.bottom;

var x = d3.scale.linear()
    .domain([0, n - 1])
    .range([0, width]);

var y = d3.scale.linear()
    .domain([0, 1])
    .range([height, 0]);

var line = d3.svg.line()
    .x(function(d, i) { return x(i); })
    .y(function(d, i) { return y(d); });

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 + ")");

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

//svg.append("g")
//    .attr("class", "x axis")
//    .attr("transform", "translate(0," + y(0) + ")")
//    .call(d3.svg.axis().scale(x).orient("bottom"));
//
//svg.append("g")
//    .attr("class", "y axis")
//    .call(d3.svg.axis().scale(y).orient("left"));

var path = svg.append("g")
    .attr("clip-path", "url(#clip)")
  .append("path")
    .datum(data)
    .attr("class", "line")
    .attr("d", line);

dump = 0;
function new_random(){
    return Math.random()*(1-0.5)+0.5
};
tick();

function tick() {

  // push a new data point onto the back
  data.push(dump);
  dump = 0;

  // redraw the line, and slide it to the left
  path
      .attr("d", line)
      .attr("transform", null)
    .transition()
      .duration(500)
      .ease("linear")
      .attr("transform", "translate(" + x(-1) + ",0)")
      .each("end", tick);

  // pop the old data point off the front
  data.shift();
}

</script>

精简代码


comments powered by Disqus