简体   繁体   English

在d3js中,是否可以在x轴上转换固定范围的值?

[英]In d3js, is it possible to transition a fixed range of values on x-axis?

I wish to create a line chart with transition. 我希望创建一个带过渡的折线图。 So for the first step, I wish to simply draw the axis and move the x-axis. 因此,对于第一步,我希望简单地绘制轴并移动x轴。 The x-axis has 0-15 as values, and I want these values to keep on moving in a loop.. I took help from this code which I got through stackoverflow: http://jsfiddle.net/aggz2qbn/ x轴有0-15作为值,我希望这些值继续在循环中移动..我从这个代码获得帮助,我通过stackoverflow: http//jsfiddle.net/aggz2qbn/

Here is the code I have: 这是我的代码:

var t = 1, maxval;
var i, n = 40;
var duration = 750;
function refilter(){

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

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

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

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

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

// extra svg to clip the graph and x axis as they transition in and out
var graph = g.append("svg")
    .attr("width", width)
    .attr("height", height + margin.top + margin.bottom);   

var xAxis = d3.svg.axis().scale(x).orient("bottom");
var axis = graph.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(x.axis=xAxis);

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

tick();

function tick() {
    t++;
    if(t>=15)
        t=1;
    else if(t<=15)
    {   
        x.domain([t,15]);
        axis.transition()
        .duration(500)
        .ease("linear")
        .call(xAxis)
        .each("end", tick);
    }
     // slide the x-axis left

}
}

The values on x-axis do move, but not in a repetitive way, instead they simply stretch frm 1 to 15. Can anybody help me out? x轴上的值确实会移动,但不会以重复的方式移动,相反,它们只是将1到15伸展。任何人都可以帮助我吗?

Your tick function is setting the domain as: 您的tick函数将域设置为:

[1,15]
then
[2,15]
then
[3,15]
etc...

This is more a zoom towards the end. 这是更接近结束的缩放。 What you want is a rolling effect (say with an N of 5 ticks): 你想要的是滚动效果(比如N为5个刻度):

[1,5]
then
[2,6]
then
[3,7]
etc...

So: 所以:

function tick() {
  var curD = x.domain(); // get current domain   
  if (curD[1] >= 15){ // at 15 reset to 1,5
     curD[0] = 0;
     curD[1] = n-1;
   }
  x.domain([curD[0]+1,curD[1]+1]); // increase both sides by one

  // slide the x-axis left
  axis.transition()
    .duration(1500)
    .ease("linear")
    .call(xAxis)
    .each("end", tick);
}

Example here . 这里的例子。

EDITS FOR COMMENT 评论的编辑

Datetimes can be handled the same way, but do the math different. 日期时间可以以相同的方式处理,但数学运算不同。 For instance, scrolling my months: 例如,滚动我的月份:

function tick() {
  var curD = x.domain();
  var newD = [curD[0].setMonth(curD[0].getMonth() + 1),
            curD[1].setMonth(curD[1].getMonth() + 1)]
  x.domain(newD);
  // slide the x-axis left
  axis.transition()
    .duration(1500)
    .ease("linear")
    .call(xAxis)
    .each("end", tick);
}

Updated example . 更新的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM