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

有些话、适合烂在心里 提交于 2019-11-29 01:44:03

问题


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");


来源:https://stackoverflow.com/questions/38583085/d3-js-cannot-read-property-axis-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!