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

2019-02-04 03:29发布

问题:

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!

回答1:

For version 4 It has to be:

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):

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