deleting data

master
Matt Huntington 8 years ago
parent 85f24bf3e8
commit 9fb2283c97

@ -687,11 +687,11 @@ render(); //add this line
## Remove data ## Remove data
- Let's set up a click handler on a `<circle>` to remove that data element from the array - Let's set up a click handler on a `<circle>` to remove that circle and its associated data element from the array
- We'll need to do this inside the `render()` declaration so that the click handlers are attached **after** the circles are created - We'll need to do this inside the `render()` declaration so that the click handlers are attached **after** the circles are created
```javascript ```javascript
//put this inside the render function, so that click handlers are attached when the circle is created //put this at the bottom of the render function, so that click handlers are attached when the circle is created
d3.selectAll('circle').on('click', function(datum, index){ d3.selectAll('circle').on('click', function(datum, index){
d3.event.stopPropagation(); //stop click event from propagating to the SVG element and creating a run d3.event.stopPropagation(); //stop click event from propagating to the SVG element and creating a run
runs = runs.filter(function(run, index){ //create a new array that has removed the run with the correct id. Set it to the runs var runs = runs.filter(function(run, index){ //create a new array that has removed the run with the correct id. Set it to the runs var
@ -702,7 +702,13 @@ d3.selectAll('circle').on('click', function(datum, index){
}); });
``` ```
The `<circle>` elements aren't be removed though. Let's put them in a `<g>` so that it's easy to clear out: The `<circle>` elements aren't be removed though:
![]()
In the image above, it appears as though, there are only two circles, but really, the middle one has had its `cx` set to 800 and its `cy` set to 0. It's overlapping the other circle in the same position.
Let's put the circles in a `<g>` so that it's easy to clear out:
```html ```html
<svg> <svg>
@ -710,43 +716,60 @@ The `<circle>` elements aren't be removed though. Let's put them in a `<g>` so
</svg> </svg>
``` ```
Now we can clear out the `<circle>` elements each time `render()` is called. Now we can clear out the `<circle>` elements each time `render()` is called. This is a little crude, but it'll work for now. Later on, we'll do things in a more elegant fashion.
```javascript ```javascript
var render = function(){ var render = function(){
d3.select('#points').html(''); //clear out all circles when rendering d3.select('#points').html(''); //clear out all circles when rendering
d3.select('#points').selectAll('circle') //add circles to #points group, not svg d3.select('#points').selectAll('circle') //add circles to #points group, not svg
.data(runs) .data(runs)
.enter() .enter()
.append('circle'); .append('circle');
// ==================================
// rest of render function code goes here...
// ==================================
} }
render();
``` ```
Let's put in a little code to handle when the user has deleted all runs and tries to add a new one: ![]()
If you try to delete all the circles and then add a new one, you'll get an error:
![]()
Inside the `<svg>` click handler, let's put in a little code to handle when the user has deleted all runs and tries to add a new one:
```javascript ```javascript
//inside svg click handler //inside svg click handler
var newRun = { var newRun = {
id: ( runs.length > 0 ) ? runs[runs.length-1].id+1 : 1, id: ( runs.length > 0 ) ? runs[runs.length-1].id+1 : 1, //add this line
date: formatTime(date), date: formatTime(date),
distance: distance distance: distance
} }
``` ```
![]()
Lastly, let's put in some css, so we know we're clicking on a circle: Lastly, let's put in some css, so we know we're clicking on a circle:
```css ```css
circle { circle {
r: 5; r: 5;
fill: black; fill: black;
transition: r 0.5s linear, fill 0.5s linear; /* transition */ transition: r 0.5s linear, fill 0.5s linear; /* add this transition to original code */
} }
circle:hover { /* hover state */ /* add this css for the hover state */
circle:hover {
r:10; r:10;
fill: blue; fill: blue;
} }
``` ```
![]()
## Drag an element ## Drag an element
- D3 allows us to create complex interactions called "behaviors" which have multiple callbacks - D3 allows us to create complex interactions called "behaviors" which have multiple callbacks

Loading…
Cancel
Save