Javascript D3.js - 无法读取未定义的属性“轴”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38583085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:37:26  来源:igfitidea点击:

D3.js - Cannot read property 'axis' of undefined

javascriptd3.jsd3v4

提问by cmplx96

Here is my code snippet:

这是我的代码片段:

var xScale = d3.scaleLinear().range([MARGINS.left, WIDTH -  MARGINS.right]).domain([scaleMin, scaleMax]);
var yScale = d3.scaleLinear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([minValue,maxValue+numberOfSignals*300]);
xAxis = d3.svg.axis().scale(xScale);
yAxis = d3.svg.axis().scale(yScale).orient("left");

vis.append("svg:g")
        .attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")
        .call(xAxis);

vis.append("svg:g")
        .attr("transform", "translate(" + (MARGINS.left) + ",0)")
        .call(yAxis);

this is my code. But everytime i run it i get the error "plot:91 Uncaught TypeError: Cannot read property 'axis' of undefined". The thing is that it worked before. was there an update? Thanks!

这是我的代码。但每次我运行它时,我都会收到错误消息“plot:91 Uncaught TypeError: Cannot read property 'axis' of undefined”。问题是它以前有效。有更新吗?谢谢!

回答by Cyril Cherian

For version 4 It has to be:

对于版本 4 它必须是:

var xScale = d3.scaleLinear().range([MARGINS.left, WIDTH - MARGINS.right]).domain([scaleMin, scaleMax]);

var yScale = d3.scaleLinear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([minValue,maxValue+numberOfSignals*300]);


var xAxis = d3.axisBottom()
    .scale(xScale);

var yAxis = d3.axisLeft()
    .scale(yScale);

Instead of this code below(will work in version 3):

而不是下面的代码(将在版本 3 中工作):

xAxis = d3.svg.axis().scale(xScale);
yAxis = d3.svg.axis().scale(yScale).orient("left");