简体   繁体   中英

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. 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/

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?

Your tick function is setting the domain as:

[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):

[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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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