adjustments to scatter plot

master
Matt Huntington 7 years ago
parent ee1d67618f
commit 41019c8ad9

@ -454,11 +454,9 @@ Notice we moved `parseTime` and `formatTime` up so they could be used within the
![](https://i.imgur.com/gSA05gP.png) ![](https://i.imgur.com/gSA05gP.png)
## Dynamically generate svg elements ## Dynamically generate SVG elements
- Currently, we have just enough `<circle>` elements to fit our data. What if we don't want to count how many elements are in the array? Currently, we have just enough `<circle>` elements to fit our data. What if we don't want to count how many elements are in the array? D3 can create elements as needed. First, remove all `<circle>` elements from `index.html`. Your `<body>` tag should look like this now:
- D3 Can create elements as needed
- First, remove all `<circle>` elements from `index.html`
```html ```html
<svg></svg> <svg></svg>
@ -487,10 +485,18 @@ d3.selectAll('circle')
}); });
``` ```
It should look exactly the same as before, but now circles are being created for each object in the "runs" array It should look exactly the same as before, but now circles are being created for each object in the `runs` array:
![](https://i.imgur.com/r59oUuJ.png) ![](https://i.imgur.com/r59oUuJ.png)
Here's a more in depth explanation of what we just wrote. Take a look at the first line of new code from above:
```javascript
d3.select('svg').selectAll('circle')
```
This might seem unnecessary. Why not just do `d3.selectAll('circle')`? Well, at the moment, there are no `circle` elements. We're going to be appending `circle` elements dynamically, so `d3.select('svg')` tells D3 where to append them. We still need `.selectAll('circle')` though, so that when we call `.data(runs)` on the next line, D3 knows what elements to bind the various objects in the `runs` array to. But there aren't any `circle` elements to bind data to. That's okay: `.enter()` finds the "run" objects that haven't been bound to any `circle` elements yet (in this case all of them). We then use `.append('circle')` to append a circle for each unbound "run" object that `.enter()` found.
## Create axes ## Create axes
D3 can automatically generate axes for you: D3 can automatically generate axes for you:

@ -31,7 +31,12 @@ var yDomain = d3.extent(runs, function(datum, index){
}) })
yScale.domain(yDomain); yScale.domain(yDomain);
d3.selectAll('circle').data(runs) d3.select('svg').selectAll('circle') //since no circles exist, we need to select('svg') so that d3 knows where to append the new circles
.data(runs) //attach the data as before
.enter() //find the data objects that have not yet been attached to visual elements
.append('circle'); //for each data object that hasn't been attached, append a <circle> to the <svg>
d3.selectAll('circle')
.attr('cy', function(datum, index){ .attr('cy', function(datum, index){
return yScale(datum.distance); return yScale(datum.distance);
}); });

@ -6,11 +6,7 @@
<link rel="stylesheet" href="app.css"> <link rel="stylesheet" href="app.css">
</head> </head>
<body> <body>
<svg> <svg></svg>
<circle/>
<circle/>
<circle/>
</svg>
<script src="https://d3js.org/d3.v5.min.js"></script> <script src="https://d3js.org/d3.v5.min.js"></script>
<script src="app.js" charset="utf-8"></script> <script src="app.js" charset="utf-8"></script>
</body> </body>

Loading…
Cancel
Save