Uncaught TypeError: Cannot read property 'axis' of undefined
Here is my code snippet:
<html>
<head>
<meta charset="utf-8">
<title>a picture</title>
<style>
.axis path,
.axis line{
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text{
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script>
<script>
var width=300;
var height=300;
var svg=d3.select("body")
.append("svg")
.attr("width",width)
.attr("height",height);
var dataset = [ 2.5 , 2.1 , 1.7 , 1.3 , 0.9 ];
var linear = d3.scaleLinear()
.domain([0, d3.max(dataset)])
.range([0, 250]);
var rectHeight=25;
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x",20)
.attr("y",function(d,i){
return i * rectHeight;
})
.attr("width",function(d){
return linear(d);
})
.attr("height",rectHeight-2)
.attr("fill","steelblue");
var axis = d3.svg.axis()
.scale(linear)
.orient("bottom")
.ticks(7);
svg.append("g")
.attr("class","axis")
.attr("transform","translate(20,130)")
.call(axis);
</script>
</body>
</html>
this is my code, but when i use it i get an error: plot: 53 Uncaught TypeError: Unable to read the 'axis' of undefined property. After searching for some problems, I think there may be an update, but I do not know how to deal with this, could you give me a good introduction? Thank!
+4
1 answer