119 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			119 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| ---
 | |
| id: 587d7faa367417b2b2512bd4
 | |
| title: Añade un efecto "Hover" a un elemento D3
 | |
| challengeType: 6
 | |
| forumTopicId: 301469
 | |
| dashedName: add-a-hover-effect-to-a-d3-element
 | |
| ---
 | |
| 
 | |
| # --description--
 | |
| 
 | |
| Es posible añadir efectos que resalten una barra cuando el usuario pasa mouse sobre ella. Hasta ahora, el estilo de los rectángulos se aplica con los métodos incorporados en D3 y SVG, pero también puedes usar CSS.
 | |
| 
 | |
| Tú estableces la clase CSS en los elementos SVG con el método `attr()`. Después la pseudo-clase `:hover` para tu nueva clase mantiene las reglas de estilo para cualquier efecto hover.
 | |
| 
 | |
| # --instructions--
 | |
| 
 | |
| Utiliza el método `attr()` para añadir una clase de `bar` a todos los elementos `rect`. Esto cambia el color de (relleno) `fill` de la barra a marrón cuando pases el mouse sobre ella.
 | |
| 
 | |
| # --hints--
 | |
| 
 | |
| Tu elemento `rect` debe tener una clase de `bar`.
 | |
| 
 | |
| ```js
 | |
| assert($('rect').attr('class').trim().split(/\s+/g).includes('bar'));
 | |
| ```
 | |
| 
 | |
| # --seed--
 | |
| 
 | |
| ## --seed-contents--
 | |
| 
 | |
| ```html
 | |
| <style>
 | |
|   .bar:hover {
 | |
|     fill: brown;
 | |
|   }
 | |
| </style>
 | |
| <body>
 | |
|   <script>
 | |
|     const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
 | |
| 
 | |
|     const w = 500;
 | |
|     const h = 100;
 | |
| 
 | |
|     const svg = d3.select("body")
 | |
|                   .append("svg")
 | |
|                   .attr("width", w)
 | |
|                   .attr("height", h);
 | |
| 
 | |
|     svg.selectAll("rect")
 | |
|        .data(dataset)
 | |
|        .enter()
 | |
|        .append("rect")
 | |
|        .attr("x", (d, i) => i * 30)
 | |
|        .attr("y", (d, i) => h - 3 * d)
 | |
|        .attr("width", 25)
 | |
|        .attr("height", (d, i) => 3 * d)
 | |
|        .attr("fill", "navy")
 | |
|        // Add your code below this line
 | |
| 
 | |
| 
 | |
| 
 | |
|        // Add your code above this line
 | |
| 
 | |
|     svg.selectAll("text")
 | |
|        .data(dataset)
 | |
|        .enter()
 | |
|        .append("text")
 | |
|        .text((d) => d)
 | |
|        .attr("x", (d, i) => i * 30)
 | |
|        .attr("y", (d, i) => h - (3 * d) - 3);
 | |
| 
 | |
|   </script>
 | |
| </body>
 | |
| ```
 | |
| 
 | |
| # --solutions--
 | |
| 
 | |
| ```html
 | |
| <style>
 | |
|   .bar:hover {
 | |
|     fill: brown;
 | |
|   }
 | |
| </style>
 | |
| <body>
 | |
|   <script>
 | |
|     const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
 | |
| 
 | |
|     const w = 500;
 | |
|     const h = 100;
 | |
| 
 | |
|     const svg = d3.select("body")
 | |
|                   .append("svg")
 | |
|                   .attr("width", w)
 | |
|                   .attr("height", h);
 | |
| 
 | |
|     svg.selectAll("rect")
 | |
|        .data(dataset)
 | |
|        .enter()
 | |
|        .append("rect")
 | |
|        .attr("x", (d, i) => i * 30)
 | |
|        .attr("y", (d, i) => h - 3 * d)
 | |
|        .attr("width", 25)
 | |
|        .attr("height", (d, i) => 3 * d)
 | |
|        .attr("fill", "navy")
 | |
|        // Add your code below this line
 | |
|        .attr('class', 'bar')
 | |
|        // Add your code above this line
 | |
| 
 | |
|     svg.selectAll("text")
 | |
|        .data(dataset)
 | |
|        .enter()
 | |
|        .append("text")
 | |
|        .text((d) => d)
 | |
|        .attr("x", (d, i) => i * 30)
 | |
|        .attr("y", (d, i) => h - (3 * d) - 3);
 | |
|   </script>
 | |
| </body>
 | |
| ```
 |