3

我一直在寻找很长一段时间,但我一直无法弄清楚。

我想在单行的两个插值“基础”和“后一步”之间进行转换。

var time_scale, kpi_scale;
var line;

var data = [
{"kpi": 100, "time": 1317448800000},
{"kpi": 200, "time": 1320127200000},
{"kpi": 250, "time": 1322719200000},
{"kpi": 180, "time": 1325397600000},
{"kpi": 230, "time": 1328076000000},
{"kpi": 360, "time": 1330581600000},
{"kpi": 140, "time": 1333260000000},
{"kpi": 120, "time": 1335852000000},
{"kpi": 240, "time": 1338530400000},
{"kpi": 190, "time": 1341122400000},
{"kpi": 185, "time": 1343800800000},
{"kpi": 130, "time": 1346479200000},
{"kpi": 340, "time": 1349071200000},
{"kpi": 320, "time": 1351749600000},
{"kpi": 250, "time": 1354341600000}
];

// set up the viewport, the scales, and axis generators      
var container_dimensions = {width: 900, height: 400},
    margins = {top: 10, right: 20, bottom: 30, left: 60},
    chart_dimensions = {
        width: container_dimensions.width - margins.left - margins.right,
        height: container_dimensions.height - margins.top - margins.bottom
    };

var time_extent = d3.extent(
    data,
    function(d){return d.time;}
);

time_scale = d3.time.scale()
    .domain(time_extent)
    .range([0, chart_dimensions.width]);

var kpi_extent = d3.extent(
    data,
    function(d){return d.kpi;}
);

kpi_scale = d3.scale.linear()
    .domain(kpi_extent)
    .range([chart_dimensions.height, 0]);

var container = d3.select('body')
    .append('svg')
        .attr("width", container_dimensions.width)
        .attr("height", container_dimensions.height)
    .append("g")
        .attr("transform", "translate(" + margins.left + "," + margins.top + ")")
        .attr("id","chart");

draw_timeseries (data);
transitionToStepped (data);

function draw_timeseries (data) {
    line = d3.svg.line()
        .x(function(d){return time_scale(d.time);})
        .y(function(d){return kpi_scale(d.kpi);})
        .interpolate("basis");

    d3.select('#chart').append('path')
        .attr('d', line(data))
        .attr('id', 'line')
        .attr('class', 'line');               
}

function transitionToStepped (data) {

    line.interpolate("step-after");

    d3.select("#chart").selectAll(".line")
        .transition()
        .duration(2000)
        .attr("d", line(data))
        .delay(2000);           
}

我保存了一个包含我的代码的小 jsfiddle。

jsFiddle 插值转换

我想在基准线和后续线之间“变形”。但是 d3 是从左侧滑入后续线,而不是将基准线“弯曲”成后续线。

谢谢你的帮助。克里斯托夫

4

1 回答 1

2

必须有一种方法可以做到这一点,而且肯定比下面的产品更好(我期待看到其他答案),但我还没有在这里看到任何答案,这至少会做一个“变形” " 对你来说,虽然它也在过渡期间使线平行于 x 轴。不是一件可怕的事情,但我不完全相信你所追求的。

这是完整代码和演示的小提琴,从您的原始修改:http:
//jsfiddle.net/n5P6z/2/

该方法的核心在这里:

function xLineReduce() {
    line.x(function (d) {
        return time_scale(d.time);
    }).y(function (d) {
        return chart_dimensions.height;
    });
}

function resetInterpolator() {
    line.interpolate("cubic-in-out");
}

function stepItUp() {
    line.x(function (d, i) {
        return time_scale(d.time);
    })
        .y(function (d, i) {
        return kpi_scale(d.kpi);
    })
        .interpolate("step-after");
}

function transitionToStepped(myData) {
    path.data(myData)
        .call(xLineReduce)
        .transition()
        .delay(1000)
        .duration(500)
        .attr('d', line(myData));

    path.data(myData)
        .call(resetInterpolator)
        .transition()
        .delay(1500)
        .duration(0)
        .attr('d', line(myData));

    path.data(myData)
        .call(stepItUp)
        .transition()
        .delay(1500)
        .duration(500)
        .attr('d', line(myData));
}
于 2013-02-02T01:42:52.613 回答