【问题标题】:Time series line chart is not sync with axis时间序列折线图与轴不同步
【发布时间】:2014-08-04 18:49:34
【问题描述】:

本实验基于this d3 official example。我想要实现的是可视化时间序列数据的最后 x 分钟。我在此jsfiddle 中有一份代码副本。点击添加新数据。

问题是图表与轴不同步。图表和轴都通过相同的转换进行平移。

也许我的方法不对,我想要实现的目标可以通过一些最简单的方法来完成。请帮忙。

到底是jsfiddle代码。

html:

<h1>Click to plot</h1>
<div id="chart"></div>

js:

var n = 200,
    duration = 150,
    count = 0,
    data = d3.range(n).map(function() { return 0; });

// canvas size
var margin = {top: 10, right: 10, bottom: 10, left: 10},
    width = 400,
    height = 200;

// input data model
var x = d3.time.scale();
var y = d3.scale.linear();
updateDomains(new Date() - duration);

var svgContainer = d3.select("#chart").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom);

// axis
var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .ticks(6);

var xAxisG = svgContainer.append("g")
    .attr("class", "axis")
    .attr("transform", 
          "translate("+(margin.left)+"," + (height) + ")")
    .call(xAxis);

// the line chart
var line = d3.svg.line()
    .interpolate("linear")
    .x(function(d, i) { return x(new Date() - (n - 1 - i) * duration); })
    .y(function(d, i) { return y(d); });

var path = svgContainer.append("g")
    .attr("transform", 
          "translate("+(margin.left)+",0)")
    .attr("class", "path")
    .append("path")
    .data([data])
    .attr("class", "line");

tick();

d3.select(window)
    .on("click", function() { ++count; });

function tick() {

  // update the domains
  updateDomains(new Date());

  // push the accumulated count onto the back, and reset the count
  data.push(Math.min(30, count));
  count = 0;

  redrawLine();

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

function redrawLine() {
  // redraw the line
  svgContainer.select(".line")
      .attr("d", line)
      .attr("transform", null);

  // slide the x-axis left
  xAxisG.transition()
      .duration(duration)
      .ease("linear")
      .call(xAxis);

  // slide the line left
  path.transition()
      .duration(duration)
      .ease("linear")
      .attr("transform", 
            "translate(" + x(new Date() - (n - 1) * duration) + ")")
      .each("end", tick);
}

function updateDomains(now) {
    x.domain([now - (n - 2) * duration, now - duration]);
    x.range([0, width]);

    y.range([height, 0]);
    y.domain([0, d3.max(data)]);
}

css:

body {
  font: 10px sans-serif;
}

#chart {
    border: 1px solid gray;
}


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


.x.axis path {
  display: none;
}

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

【问题讨论】:

  • 您可能会更好地使用 x 轴和数据的通用比例,它就是用于此类事情的。
  • 对不起,你能解释一下吗?

标签: javascript d3.js time-series


【解决方案1】:

实际上,在您的代码中,我看到您正在使用 new Date(),它会在函数执行时发生变化。所以你的轴比例和绘图元素的比例会改变。 你可以尝试替换下面的行

updateDomains(new Date() - duration);

与

var currentDate = new Date();
updateDomains(currentDate - duration);

同样在 tick 函数中将 currentDate 发送到 displayDomains 函数。 当你想改变比例时,你改变变量 currentDate。

希望对你有帮助。

象头神

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 2019-11-18
    • 2019-06-25
    • 2013-07-24
    相关资源
    最近更新 更多