使用d3.js缩放窗口上的轴重新调整大小
我试图重新绘制条形图,如果窗口重新调整大小,工作正常,但我还需要重新绘制轴,因为某些原因,轴不工作,这是我的代码使用d3.js缩放窗口上的轴重新调整大小,d3.js,D3.js,我试图重新绘制条形图,如果窗口重新调整大小,工作正常,但我还需要重新绘制轴,因为某些原因,轴不工作,这是我的代码 $(window).resize(function change() { var tempScale; var newAxis; //bar chart is drawn horizontally if ($(window).w
$(window).resize(function change() {
var tempScale;
var newAxis;
//bar chart is drawn horizontally
if ($(window).width() < width) {
//create a new scale on window resize
tempScale = d3.scale.linear()
.domain([0, d3.max(arr)])
.range([0, $(window).width()]);
// intialize newAxis which is adjusted to tempScale
newAxis = d3.svg.axis().scale(tempScale);
}
else {
//set tempScale to default scale
tempScale = scale1;
}
//modify the bar chart according to new scale
canvas2.selectAll("rect")
.data(arr)
.attr("width", function (d) { return tempScale(d); })
.attr("height", 10)
//this call is not working
.call(newAxis);
});
$(窗口)。调整大小(函数更改(){
var-tempScale;
var newAxis;
//条形图是水平绘制的
如果($(窗口).width()
此外,我还收到can错误“无法读取未定义的属性‘apply’”。希望我提前提供了足够的信息,谢谢。您没有在您的条件的
else
分支中初始化newAxis
。谢谢@larskothoff,我不再收到错误。但是现在一张脸出现了一个奇怪的问题,新的轴被画在旧的轴上,它变得非常难看,如何用新的轴正确地替换旧的轴?必须有几十个这样做的例子,例如,或。