我需要从x轴过渡(从下到上)。但现在我正在从顶部到底部进行条形转换。我已经使用了d3.js和Json对象。
Js Code如下:
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .6);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10);
var svgBar = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("data.json", function(error, data) {
function type(d) {
d.Speed = +d.Speed;
}
x.domain(data.map(function(d) { return d.Days; }));
y.domain([0, d3.max(data, function(d) { return d.Speed; })]);
svgBar.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svgBar.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Speed");
var graphTransition = svgBar.selectAll("bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.Days); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.Speed); })
.attr("height",0);// Initial Height at loading
graphTransition
.transition()
.attr("height", function(d) { return height - y(d.Speed); }) //Bar height after transition
.duration(1000);
});
Json如下:
[
{"Days":"Mon","Speed":150},
{"Days":"Tue","Speed":200}
]
我可能错过了一件愚蠢的事情。任何帮助表示赞赏,并提前致谢。
答案 0 :(得分:1)
试试这个
var graphTransition = svgBar.selectAll("bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.Days); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return height; })
.attr("height",0);
graphTransition
.transition()
.delay(100)
.attr("height", function(d) { return height - y(d.Speed); })
.attr("y", function(d) { return y(d.Speed); })
.duration(1000);