This is a guest blog post from our community. Join our Discord to meet the author and connect with other developers passionate about Code Search and AI!
Excluding the visually appealing nature of graphically represented data, data represented using images is statistically easier to understand compared to textual data. Suppose you want to convey a rank on some data. You can choose to serially rank the data or adopt a better functionality for visualizing this data using D3.js, an open source JavaScript library for visualizing data.
This article discusses data visualization using D3.js and enhancing your productivity using Cody.
Prerequisites
This article will break down complex concepts to be easily understood. However, to properly follow through, you’ll need:
Before we get into the technical aspects, it’s important to understand the concept of data visualization and why we visualize data. One way to convey data is to use text. However, there are situations where textual representation of data does not suffice. For example, showing the rank of some data in ascending order. In conveying data, the most important factor to consider is the comprehension of viewers or anyone who reads the data, which means a conscious effort must be adopted to convey data, through text or graphics.
Data visualization refers to the graphical representation of data using visual elements, such as charts, graphs, maps, and tables.
D3.js
D3.js (or simply D3, short for Data-Driven Documents) is an open source JavaScript library used to visualize data on the web. It is an extremely fast and flexible library. It allows you to express creativity in data visualization using HTML, CSS, and JavaScript (at the basic level). The term “Data-Driven Documents†means the visual elements are dynamically generated from the data provided, which makes D3 exciting to use. You provide the raw data and it generates the visual element. We’ll see how this works in a bit.
Cody AI
A key factor in choosing a library or tool in software development is ease of use, or how well it aids developers’ productivity. With Cody in the picture, this becomes less of a worry because it adds the missing piece to your development workflow.
Cody is an open-source coding AI assistant developed by Sourcegraph that aids your development process by providing context-aware solutions based on your codebase. It has features such as code autocomplete, AI-powered chat with multiple large language models (LLMs), provides explanations on your code, can help optimize code for best practices, and more. In this article, you will learn how Cody helps you enhance productivity when visualizing data with D3.js.
Why use D3.js for data visualization?
The use of D3.js in data visualization is beneficial for many reasons, some of which include:
Flexibility: As I mentioned earlier, one aspect where D3.js stands tall, especially in comparison with other visualization libraries, is its flexibility. This is particularly interesting for developers looking to explore the full capabilities of data visualization without restriction. Unlike other libraries with pre-built charts, D3.js provides APIs to build any sort of chart you can imagine based on your data.
Dynamic manipulation: D3.js provides ease in manipulating visual elements. Once you change the values of the data, the visualization automatically updates in tune with that change.
Scalability: When dealing with graphically represented data or visual elements, developers are often concerned about performance and speed. D3.js is highly scalable for use in small and large projects.
Support for Animations: D3.js allows you to apply custom animations to your visualizations, making it more engaging and visually appealing.
Framework Support: As a JavaScript library, D3.js works with all JavaScript frameworks, making it widely used by a highly active community of users.
Getting started with D3.js
Now that you have learned about data visualization and the features of D3.js, it is time to see how it works in our application. In this article, we will be working in a Vanilla HTML environment. You can load D3.js from a CDN like so:
```html
```
Working principle
D3.js allows developers to bind arbitrary data to the DOM, which makes it easy to manipulate and update data in real-time. In this section, we will go through the basic logic of selecting elements to be visualized, modifying the elements with custom styling, and joining the data.
Selecting elements
The concept of selecting elements in D3.js isn’t so strange, especially from a JavaScript background. Selecting elements basically entails targeting the particular element you want to modify and visualize.
Suppose you want to visualize elements with the class chartbox, you simply use the .chartbox class selector, or a div element selector to select a div element. D3.js provides two methods of selecting elements; the d3.select() method and the d3.selectAll method. The d3.select method selects only the first matching element while d3.selectAll selects all matching instances in the DOM(from top to bottom). Consider the practical example below to learn more about how the select methods work:
```html
Cody by Sourcegraph is the best AI coding assistant
Cody by Sourcegraph is the best AI coding assistant
Cody by Sourcegraph is the best AI coding assistant
```
Then I select the h1 element with the .select() method:
javascript d3.select("h1").style("color", "red");
Here’s the result:
We see that only the first h1 element is affected because we used the .select() method. We can toggle the codebase to target all matching instances of h1 using the .selectAll(). Rather than manually make the changes, we can allow Cody to make further edits or manipulation by simply telling it to do so, since we already established a context in our codebase.
Subscribe for the latest code AI news and product updates