第一次使用D3.js.我堅持讓條形圖在條上顯示數值標籤,並在刷新數據時轉換x/y軸。我沒有收到任何錯誤,代碼來自於將幾個例子放在一起。代碼如下,jfiddle here工作示例。在真正的代碼中,我使用d3.json來拉取數據,在jsfiddle中我使用了靜態變量fyi。任何幫助表示讚賞。下面D3.js條形圖 - 軸和標籤不工作/正在轉換
代碼:
var data;
var margin = {
top: 20,
right: 0,
bottom: 30,
left: 30
},
width = 838 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], 0.05);
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 svg = d3.select("#daychart")
.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("http://10.0.0.13/sma/sma-php/inverterdata.php?var=CDAY&id=C1200031", function (error, data) {
data.forEach(function (d) {
d.max_energy = +d.max_energy;
});
x.domain(data.map(function (d) {
return d.xaxis;
}));
y.domain([0, d3.max(data, function (d) {
return d.max_energy;
})]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("transform", "rotate(0)")
.attr("y", 23)
.attr("x", 340)
.attr("dy", ".71em")
.style("text-anchor", "bottom")
.text("Timeline");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(0)")
.attr("y", -15)
.attr("x", -25)
.attr("dy", ".71em")
.style("text-anchor", "top")
.text("Energy - KWh");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function (d) {
return x(d.xaxis);
})
.attr("width", x.rangeBand())
.attr("y", function (d) {
return y(d.max_energy);
})
.transition().delay(function (d, i) {
return i * 10;
}).duration(10)
.attr("height", function (d) {
return height - y(d.max_energy);
});
//Create labels
svg.selectAll("text")
.data(data)
.enter()
.append("text")
.text(function (d) {
return parseFloat(Math.round(d.max_energy * 100)/100).toFixed(1);
})
.attr("x", function (d) {
return x(d.xaxis) + x.rangeBand()/2;
})
.attr("y", function (d) {
return height - y(d.max_energy) + 14;
})
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "black");
//});
//On click, update with new data
d3.select("p").on("click", function() {
// Get the data again
d3.json("http://10.0.0.13/sma/sma-php/inverterdata.php?var=PDAY&id=P100023", function (error, data) {
data.forEach(function (d) {
d.max_energy = +d.max_energy;
});
// Scale the range of the data again
x.domain(data.map(function (d) {
return d.xaxis;
}));
y.domain([0, d3.max(data, function (d) {
return d.max_energy;
})]);
// Make the changes
svg.selectAll(".bar") // change the bar
.data(data) // Update the data within.
.transition().delay(function (d, i) {
return i/data.length * 1000;
})
.duration(500)
.attr("x", function (d) {
return x(d.xaxis);
})
.attr("y", function (d) {
return y(d.max_energy);
})
.attr("width", x.rangeBand())
.attr("height", function (d) {
return height - y(d.max_energy);
});
//Update all labels
svg.selectAll("text")
.data(data).transition()
.delay(function (d, i) {
return i/data.length * 1000;
})
.duration(500)
.text(function (d) {
return parseFloat(Math.round(d.max_energy * 100)/100).toFixed(1);
})
.attr("x", function (d, i) {
return x(i) + x.rangeBand()/2;
})
.attr("y", function (d) {
return height - y(d.max_energy) + 24;
});
});
});
HTML:
<p>CLICK ME</p>
<svg id="daychart"></svg>
拉斯您好,感謝!你的JFiddle鏈接雖然不起作用。另外,爲什麼我的條形圖標籤不顯示?謝謝! – TheRealPapa
哎呀,對不起。不知道哪裏出了問題 - 鏈接固定。不確定你想要什麼類型的標籤 - 就像[本教程](http://bost.ocks.org/mike/bar/)中的東西? –
嗨拉爾斯,再次感謝!是的,這是我從代碼中提取代碼的教程,但不幸的是,欄上的值標籤不起作用。我想將這個值放在每個小節的頂部。非常感謝您花時間! – TheRealPapa