第一次使用D3.js.我坚持让条形图在条形图上显示值标签,并在刷新数据时转换x / y轴。我没有得到任何错误,代码来自于将几个例子放在一起。代码如下,jfiddle here为工作示例。在真正的代码中,我使用d3.json来提取数据,在jsfiddle我使用静态变量,fyi。任何帮助表示赞赏。
以下代码:
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>
答案 0 :(得分:2)
它实际上比你想象的要容易。您只需再次调用轴功能:
svg.select("g.x").call(xAxis);
svg.select("g.y").call(yAxis);
更改了jsfiddle here。
要使标签生效,您可以为这些text
元素指定一个特定的类并进行相应的选择。默认情况下,D3根据索引匹配元素。也就是说,在通过调用轴添加轴标签后,将选择这些轴标签并与数据进行匹配。因此,.enter()
选项将为空,且未添加新标签。
Jsfiddle with that here。
答案 1 :(得分:2)
对于问题的第二部分:
标签未正确显示,因为您只是使用selectAll("text")
来抓取它们。问题在于轴上的刻度标签也是文本元素,因此它们被选中并分配了数据 - 但是不包括在“输入”中。这也是更新数据时轴标签移出屏幕的原因(因为在这种情况下,您在所有元素上设置属性,而不仅仅是新元素)。
要修复:在条形标签中添加一个类,并使用它来专门选择它们:
svg.selectAll("text.barLabels") //use this selector in your update as well
.data(data)
.enter()
.append("text")
.classed("barLabels", true) //remember to actually add the class!
它现在应该全部工作......差不多了。
你可能会自己弄清楚这一点,但你的标签y定位功能没有考虑到你使用倒置刻度的事实,所以标签都最终处于奇怪的位置。您还需要更改
.attr("y", function (d) {
return height - y(d.max_energy) + 24;
});
到
.attr("y", function (d) {
return y(d.max_energy) + 24;
})
或
.attr("y", function (d) {
return Math.min(y(d.max_energy) + 24, height);
})
使“0.0”标签不位于轴线之外;甚至
.attr("y", function (d,i) {
return Math.min(y(d.max_energy) + 24*(1+i%2), height);
})
如果相邻标签对于条形太宽,则不会重叠。 (i%2
只返回0表示偶数,1表示奇数;模运算符%
从整数除法返回“余数”。)
最后一个版本是我在这里实现的:http://jsfiddle.net/h8Rsg/40/