第一次使用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