Skip to content

Commit d9287da

Browse files
authored
Concept Entry D3 Interactivity (#3942)
* Concept Entry D3 Interactivity * tag update * Tooltip code * image * Update interactivity.md * lint fix * Update interactivity.md * Update content/d3/concepts/interactivity/interactivity.md Co-authored-by: Avdhoot <[email protected]> * Add files via upload * add images * Update interactivity.md lint * image and text edits * link typo ---------
1 parent 22895da commit d9287da

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
Title: 'Interactivity'
3+
Description: 'D3 interactivity enriches visualizations with dynamic user engagement and interactive features.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Data Visualization'
7+
Tags:
8+
- 'D3'
9+
- 'Data'
10+
CatalogContent:
11+
- 'interactive-data-visualization-with-generative-ai'
12+
- 'paths/data-visualization'
13+
---
14+
15+
D3.js, or Data-Driven Documents, is a powerful JavaScript library for creating dynamic and interactive data visualizations in web browsers. It facilitates the binding of data to DOM elements, enabling seamless updates and transitions. With its declarative approach to manipulating the Document Object Model (DOM), D3.js empowers developers to create engaging and responsive visualizations, making it a go-to tool for building data-driven applications on the web.
16+
17+
## Adding Tooltips for Enhanced Information
18+
19+
Tooltips are a crucial element in interactive visualizations, providing additional context when users hover over data points. The following code demonstrates how to integrate tooltips into a D3.js visualization. This is a very basic example, it creates a square block. When a user hovers over the block they will activate a tooltip with the text "this is a square!"
20+
21+
```js
22+
// Select the square element
23+
const square = d3.select('rect.square');
24+
25+
// Event listener for mouseover event
26+
square.on('mouseover', () => {
27+
// Show the tooltip
28+
d3.select('.tooltip')
29+
.style('opacity', 1)
30+
.style('left', d3.event.pageX + 'px')
31+
.style('top', d3.event.pageY + 'px');
32+
});
33+
34+
// Event listener for mouseout event
35+
square.on('mouseout', () => {
36+
// Hide the tooltip
37+
d3.select('.tooltip').style('opacity', 0);
38+
});
39+
```
40+
41+
Make sure you have your html file setup using this script before running.
42+
43+
## Output
44+
45+
Block when mouse pointer is not hovering:
46+
![No tooltip if image is not hovered](https://raw.githubusercontent.com/Codecademy/docs/main/media/d3-non-hover.png)
47+
48+
Tooltip appears when hovered over:
49+
![Tooltip appears on hover](https://raw.githubusercontent.com/Codecademy/docs/main/media/d3-tooltip-hover.png)
50+
51+
## Functions and Future Considerations
52+
53+
D3.js provides a plethora of functions and methods for creating interactive and engaging visualizations. Delving deeper into D3.js interactivity, there are a range of functions worth exploring like `.transition()` for smooth animations, incorporating user interactions with `.on()` events, and experimenting with force-directed layouts for dynamic network visualizations.

media/d3-non-hover.png

3.25 KB
Loading

media/d3-tooltip-hover.png

9.66 KB
Loading

0 commit comments

Comments
 (0)