Problem with introduction to d3.js

So, I'm just starting d3.js, and I keep getting JavaScript error, and I have no idea why. I just created three circles with svg and want to select them with d3. Here is my code:

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script type="text/javascript"></script>        
</head>
<body>
    <svg width="360" height="180">
        <circle class="little" cx="180" cy="45" r="12"></circle>
        <circle class="little" cx="60" cy="90" r="12"></circle>
        <circle class="little" cx="300" cy="135" r="12"></circle>
    </svg>
    <script type="text/javascript">
        var circle = svg.selectAll("circle");
    </script>
</body>
</html>

It is assumed that you select circles on the page so that I can manipulate them, but I continue to receive a link error in my web console, which says that svg is not defined? But does the introductory tutorial say nothing about defining svg?

+5
source share
1 answer

You need to select the svg element first before using svg.selectAll.

var svg = d3.select(document.getElementById('sampleSVGId')),
    circle = svg.selectAll('circle');
+8
source

All Articles