This article discusses D3.js, a powerful JavaScript library for creating dynamic and interactive data visualizations. It uses HTML, SVG and CSS to render data in web browsers.
Core points:
D3.js is a data-based document operation JavaScript library. It helps you bring your data to life using HTML, SVG and CSS. The three JavaScript libraries that every web developer should learn are: jQuery, Underscore, and D3. These libraries allow you to think about code in new ways: jQuery allows you to do more about DOM with less code; Underscore (or lodash) provides functional tools to change the way you write programs; and D3 A rich set of tools is provided for data processing and graphical programming. If you are new to D3, take a moment to browse its impressive library of examples to see what it can do. This is not a chart library from your father's time. William Playfair invented the bar chart, line chart and area chart in 1786, and invented the pie chart in 1801. These are still the main ways most datasets are presented today. These charts are excellent, but D3 gives you the tools and flexibility to create unique web data visualizations, and your creativity is the only limiting factor. D3 is an extremely flexible low-level visualization library with a jQuery-like API for mapping data to HTML and SVG documents. It contains a large number of useful mathematical functions for data transformation and physical calculations, although most of its functions come from manipulating geometric figures and paths in SVG. This article aims to provide you with a high-level overview of the D3 features, in each example you will be able to see input data, convert and output documents. I wouldn't explain what each function does, but instead show you the code, you should be able to get a rough idea of how it works. I only go into the most important concepts: Scale and Select . Creating simple bar charts using HTML is one of the easiest ways to understand how D3 converts data into documents. The code is as follows: This code maps the input data With just a few extra lines of code, the above bar chart can be converted into a contribution chart similar to GitHub. We no longer set the height according to the data's numerical value, but set the background color. Most of D3's functionality comes from the fact that it works with SVG, which contains labels for drawing 2D graphics such as circles, polygons, paths, and text. D3 can handle more complex data types. Drawing a line chart in SVG is very simple. Scale is a function that maps the input domain to the output range. This is an example of an animation visualization showing flights between Melbourne and Sydney. This article only covers a small part of the D3.js library. Hopefully this advanced overview and some practical examples will give you an idea of how to use selections, scales, and transformations. Think about the best way to represent data and enjoy creating your own unique data visualization. (The image position remains unchanged)Bar Chart
d3.select('#chart')
.selectAll("div")
.data([4, 8, 15, 16, 23, 42])
.enter()
.append("div")
.style("height", (d)=> d + "px")
[4, 8, 15, 16, 23, 42]
to the output HTML. GitHub contribution chart
Beginner of SVG
Circular
Line Chart
Scale
Animation Flight Visualization
Conclusion
The above is the detailed content of Learn to Create D3.js Data Visualizations by Example. For more information, please follow other related articles on the PHP Chinese website!