簡體   English   中英

d3 通過更改參數化函數水平動畫線圖

[英]d3 animate line graph horizontally by changing parameterized function

我試圖通過用 d3 改變其均值和方差來為高斯設置動畫。 我遇到的問題是曲線的點只是在 Y 軸上平移,所以它看起來像一個高斯下降而另一個上升。 我怎樣才能修改這段代碼來實現我想要的?

//Define the curves, this would be replaced by a function call
var g1 = [{'x': 0, 'y': 0.0}, {'x': 2, 'y': 0.0}, ...];
var g2 = [{'x': 0, 'y': 0.0}, {'x': 2, 'y': 0.0}, ...];

var lineFunction = d3.svg.line()
                     .x(function(d) { return d.x*2; })
                     .y(function(d) { return d.y * 2000; })
                     .interpolate("linear");

var lineGraph = svg.append("path")
                     .attr("d", lineFunction(g1))
                     .attr("stroke", "blue")
                     .attr("stroke-width", 2)
                     .attr("fill", "none");

lineGraph.transition().duration(1000).attr('d', lineFunction(g2));

http://jsfiddle.net/x22xE/104/

感謝 meetamit 的幫助,這是(凌亂的)解決方案:

var svg = d3.select('#myelement').append('svg')
    .attr('width', 200)
    .attr('height', 200);

function gaussian(u, s, x) {
    var m = s * Math.sqrt(2 * Math.PI);
    var e = Math.exp(-Math.pow(x - u, 2) / (2 * s * s));
    return (e * 1000 / m).toFixed(3); // toFixed is important
}

function pdf(u, s, resolution) {
    var result = [];
    for (var i = 0; i < 200; i += resolution) {
        result.push({x: i, y: gaussian(u, s, i)})
    }
    return result;
}

var lineFunction = d3.svg.line()
                         .x(function(d) { return d.x; })
                         .y(function(d) { return d.y; })
                         .interpolate("linear");

var lineGraph = svg.append("path")
                            .attr("d", lineFunction(pdf(0,5,1)))
                            .attr("stroke", "blue")
                            .attr("stroke-width", 2)
                            .attr("fill", "none");
lineGraph.transition().duration(1000)
    .attrTween('d', function() {
    return function(t) {
        return lineFunction(pdf(150 * t, 5, 1));
    }
  })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM