我一直在寻找相当长的一段时间,但我无法弄明白。
我希望在单行的两个插值“基础”和“后续”之间进行转换。
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 Interpolation Transition
我想在基线和后续行之间“变形”。但是d3从左边开始逐步滑动,而不是将基线“弯曲”成一个接一个的步骤。
感谢您的帮助。 克里斯托弗
答案 0 :(得分:1)
必须有一种方法可以做到这一点,当然比下面的产品更好(我期待看到其他答案),但我还没有看到答案,这至少会做对你来说是“变形”,虽然它也会在过渡期间使线与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));
}