I'm just starting to learn D3 and follow the tutorial on creating this piece of code.
I created a couple of bars and intended to create an x axis for my chart. The problem is that when I add ".call (xAxis)" to my canvas, the browsers do not show me anything, and I get the following error in the console:
Uncaught TypeError: Cannot call method 'copy' of undefined d3.min.js:5
(anonymous function) d3.min.js:5
(anonymous function) d3.min.js:3
R d3.min.js:1
da.each d3.min.js:3
n d3.min.js:5
da.call d3.min.js:3
(anonymous function)
Can someone please help me with what is wrong? I really cannot understand what is missing or what I am doing wrong!
<!doctype html>
<html>
<head>
<title>Intro to D3</title>
<script src="d3.min.js"></script>
<head>
<body>
<script>
var width = 1024;
var height = 768;
var dataArray = [20, 40, 60, 120];
var xAxis = d3.svg.axis()
.scale(widthScale);
var widthScale = d3.scale.linear()
.domain([0, 120])
.range([0, width]);
var color = d3.scale.linear()
.domain([0, 120])
.range(["red", "blue"]);
var canvas = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(20, 0)")
.call(xAxis);
var bars = canvas.selectAll("rect")
.data(dataArray)
.enter()
.append("rect")
.attr("width", function(d){ return widthScale(d); })
.attr("height", 20)
.attr("fill", function(d){ return color(d); })
.attr("y", function(d, i){ return i*30; });
</script>
</body>
</html>
source
share