2016-08-28 120 views
0

Line chart growthD3折線圖動畫左至右

我有興趣創建一個折線圖 - 繪製本身從左至右 - 在x軸基本連接點。

http://bl.ocks.org/markmarkoh/8700606 http://bl.ocks.org/duopixel/4063326

這是我做了,開始創建這個圖表的jsfiddle。 http://jsfiddle.net/NYEaX/1512/

//__invoke line 
$('[data-role="line"]').each(function(index) { 
    createLine(this); 
}); 



function createLine(el){ 
    var w = $(el).data("width"); 
    var h = $(el).data("height"); 

    var svg = d3.select($(el)[0]) 
     .append("svg") 
     .attr("width", w) 
     .attr("height", h); 

    var data = d3.range(11).map(function(){return Math.random()*10}) 
    var x = d3.scale.linear().domain([0, 10]).range([0, 700]); 
    var y = d3.scale.linear().domain([0, 10]).range([10, 290]); 
    var line = d3.svg.line() 
     .interpolate("cardinal") 
     .x(function(d,i) {return x(i);}) 
     .y(function(d) {return y(d);}) 

    var path = svg.append("path") 
     .attr("d", line(data)) 
     .attr("stroke", "steelblue") 
     .attr("stroke-width", "2") 
     .attr("fill", "none"); 

    var totalLength = path.node().getTotalLength(); 

    path 
     .attr("stroke-dasharray", totalLength + " " + totalLength) 
     .attr("stroke-dashoffset", totalLength) 
     .transition() 
     .duration(2000) 
     .ease("linear") 
     .attr("stroke-dashoffset", 0); 

    svg.on("click", function(){ 
     path  
     .transition() 
     .duration(2000) 
     .ease("linear") 
     .attr("stroke-dashoffset", totalLength); 
    }); 



    /* plot axis */ 
    var padding = 100; // space around the chart, not including labels 

    // define the x axis 
    var xAxis = d3.svg.axis() 
     .orient("bottom") 
     .scale(x); 
     //.tickFormat(date_format); 

    // draw x axis with labels and move to the bottom of the chart area 
     svg.append("g") 
      .attr("class", "xaxis axis") // two classes, one for css formatting, one for selection below 
      .attr("transform", "translate(0," + (h - padding) + ")") 
      .call(xAxis); 

    /* plot axis */ 
} 

回答

2

一個解決方案,你可以做的是追加一條繪製路徑之前與路徑對應SVG圈。例如小提琴:http://jsfiddle.net/stancheta/ystymLm4/6/

var circles = svg.selectAll("dot") 
     .data(data) 
     .enter().append("svg:circle") 
     .attr('class', 'circ') 
     .attr("r", 3) 
     .attr("cx", function(d, i) { return x(i); }) 
     .attr("cy", function(d, i) { return y(d); }) 
     .style('fill', 'lightsteelblue'); 
+0

是的,這就是好男人 - 我會怎麼收拾軸 - 去除黑線基本上是 - 我不知道還如何與日期範圍創建一些假的JSON數據如果確實要將x尺度調整到一個時間尺度? –

+0

如果你想改變最小的代碼量,我已經更新了小提琴以模仿上面的圖像:http://jsfiddle.net/stancheta/ystymLm4/7/並放入一些評論來解釋我改變了什麼。讓我知道他們是否不清楚。 –

+0

這看起來不錯的男人 - 有一個賞金,如果你想幫助解決這個部分--- http://stackoverflow.com/questions/39156400/d3-animating-a-bubble-chart-within-a-given-半徑/ 39197206#39197206 –