我一直在嘗試使用D3.js,現在有了這個條形圖,我必須在按鈕單擊時動態刷新。D3.js:動態更改條形圖
我已經將它包裝在一個函數中,但是,似乎並不奏效。
什麼是問題,我該如何解決?
代碼:
var margin = {top: 40, right: 20, bottom: 30, left: 40},
width = 900,
height = 700;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
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)
.tickFormat(d3.format("s"));
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Popularity:</strong> <span style='color:teal'>" + d.popularity+"</span>";
})
var svg = d3.select("#chart").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 + ")");
svg.call(tip);
function updateChart(concert){
d3.tsv(concert+".tsv", type, function(error, data) {
x.domain(data.map(function(d) { return d.month; }));
y.domain([0, 45]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "1em")
.style("text-anchor", "end")
.text("Popularity");
var pops= svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("width", x)
.attr("fill", function(d) {
if (d.popularity> 30) {
return "#010";
else
return "000";
})
.attr("class", "bar")
.attr("x", function(d) { return x(d.month); })
.attr("width", x.rangeBand())
.attr("y", height - 1)
.attr("height", 1);
pops.transition()
.duration(1000)
.delay(function (d, i) {
return i * 100;
})
.attr("y", function(d) { return y(d.popularity); })
.attr("height", function(d) { return height- y(d.popularity); });
pops.on('mouseover', tip.show)
.on('mouseout', tip.hide);
});
}
function type(d) {
d.popularity= +d.popularity;
return d;
}
我打電話它構成了我的html文件。 例如:
<button onclick="updateChart('JB')">JB</button>
原始數據:
month popularity
January 24.6
February 26.2
March 28.4
April 30.9
May 32.9
June 32.4
July 30.7
August 30.1
September 29.7
October 28.2
November 26.1
December 25
更新時間:
month popularity
January 73
February 72
March 70
April 69
May 62
June 57
July 64
August 66
September 72
October 77
November 78
December 77
我只看到你定義了'.enter',但你還需要'.update'和'.exit'來處理更新後的數據 – Fabricator
我如何添加它們? @Fabricator – QuikProBroNa
你能提供原始和更新的數據嗎? – Fabricator