0
我正在使用d3.js v4,難以在水平條形圖和鼠標懸停上顯示值,並且鼠標懸停彈出以顯示數據中所需的值文本和鼠標懸停在d3.js中的條形圖上並將鼠標移出值
請找到下面的代碼,我已經使用
var data = [{Issue:"Other",Number:808,Stage:"A"},
{Issue:"Other1",Number:80,Stage:"A"},
{Issue:"Other2",Number:8,Stage:"A"},
{Issue:"Other3",Number:67,Stage:"A"},
{Issue:"Other4",Number:45,Stage:"A"},
{Issue:"Other5",Number:9,Stage:"A"},
{Issue:"Other6",Number:60,Stage:"A"},
{Issue:"Other7",Number:8,Stage:"A"}];
var margin = {top: 15, right: 30, bottom: 40, left: 400},
width = 1200 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var y = d3.scaleBand()
.range([height, 0])
.padding(0.1);
var x = d3.scaleLinear()
.range([0, width]);
var svg = d3.select("#barchart").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 + ")");
data.forEach(function(d) {
d.Number = +d.Number;
});
x.domain([0, d3.max(data, function(d){ return d.Number; })])
y.domain(data.map(function(d) { return d.Issue; }));
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("width", function(d) {return x(d.Number); })
.attr("y", function(d) { return y(d.Issue); })
.style("fill",function(d) {
if(d.Stage=='I')
return '#3dc778'
if(d.Stage=='B')
return '#e52929'
if(d.Stage=='U')
return '#3021fd'
if(d.Stage=='A')
return '#eefd21'
})
.attr("height", y.bandwidth());
// add the x Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// add the y Axis
svg.append("g")
.call(d3.axisLeft(y));
我們怎樣才能文本各條和鼠標懸停和鼠標進行功能上面的代碼片斷?
感謝 普拉薩德
嗨安迪,你可以進行更改,使數將顯示在每個圖表上而不是mous e Hover.on將鼠標懸停在彈出窗口中應顯示數據中的「舞臺」值 – user3349850
@ user3349850完成! – AndyB
嗨安迪彈出工作正常。但數字值不顯示在圖形欄上。 – user3349850