这拨弄解决你的大部分问题:http://jsfiddle.net/CtTkP/ 的说明如下:
- 我不知道你所说的超出图表区域是什么意思。如果标签是insde
chart-area
?如果你的意思是在平移,标签超出轴,这个问题可以通过使用两个clip-path
小号明智得到解决,虽然这不允许其svg.axis
翻译提供值的曼妙衰落:
var clipX = svg.append("clipPath")
.attr('id', 'clip-x-axis')
.append('rect')
.attr('x', 0)
.attr('y', 0)
.attr('width', width)
.attr('height', margin.bottom);
svg.append("g")
.attr("class", "x axis")
.attr('clip-path', 'url(#clip-x-axis)')
.attr("transform", "translate(0, " + height + ")")
.call(xAxis);
// ...
var clipY = svg.append("clipPath")
.attr('id', 'clip-y-axis')
.append('rect')
.attr('x', - margin.left)
.attr('y', 0)
.attr('height', height)
.attr('width', margin.left);
svg.append("g")
.attr("class", "y axis")
.attr('clip-path', 'url(#clip-y-axis)')
.call(yAxis);
function zoomed() {
var trans = zoom.translate(),
scale = zoom.scale();
tx = Math.min(0, Math.max(width * (1 - scale), trans[0]));
ty = Math.min(0, Math.max(height * (1 - scale), trans[1]));
zoom.translate([tx, ty]);
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
// ...
这将不允许图形超出限制范围。
- 当你明确地重写
tickValues
,你可以调整值居中他们:
var tickValues2 = [];
tickValues.forEach(function (t, idx) {
if (idx < tickValues.length - 1) {
tickValues2.push((t + tickValues[idx + 1])/2);
}
});
然后,而不是使用tickValues
为xAxis
和yAxis
,使用tickValues2
。
是的......我同意剪辑路径不会提供与坐标轴相同的功能,但它是最佳选择/解决方法。谢谢你解决......我知道我错过了一些东西! – mattjvincent
超级解决方案。奇迹般有效。 –