📌  相关文章
📜  script.js:15 Uncaught ReferenceError: d3 is not defined at script.js - Javascript (1)

📅  最后修改于: 2023-12-03 14:47:19.518000             🧑  作者: Mango

D3.js: A Powerful Data Visualization Library

D3.js, or Data-Driven Documents, is a JavaScript library for creating dynamic and interactive data visualizations in web browsers. It provides powerful tools for manipulating data, creating scalable graphics, and facilitating interaction with user input.

Features

D3.js offers a range of features that make it one of the most popular data visualization libraries available. Here are some of the main features:

  • Data manipulation: D3 provides an array of built-in methods for working with data, including filtering, grouping, sorting, and aggregating data. It also supports importing data from a wide range of sources, such as CSV, TSV, JSON, and XML files.

  • Scalable vector graphics: D3 leverages the power of SVG, or Scalable Vector Graphics, to create scalable graphics that can be easily resized and manipulated without losing resolution or clarity.

  • Interactive visualizations: D3’s data-driven approach enables the creation of interactive and dynamic visualizations that respond to user input. This includes support for user events such as mouse clicks, mouseovers, and key presses.

  • Modular design: D3 is built on a modular design, which allows developers to pick and choose the functionality they need. This not only reduces the size of the resulting code, but also makes it easier to maintain and update as new features are added.

Getting Started with D3.js

To get started with D3.js, you’ll need to include it in your project. This can be done by either downloading the library and including it in your HTML file, or by using a content delivery network (CDN) such as the one provided by Google.

<script src="https://d3js.org/d3.v7.min.js"></script>

Once you’ve included D3.js in your project, you can start using its powerful tools to create dynamic and interactive data visualizations.

Example Code

Here’s a basic example of using D3.js to create a simple bar chart:

// set the data
var data = [4, 8, 15, 16, 23, 42];

// create the SVG element
var svg = d3.select("body")
  .append("svg")
  .attr("width", 420)
  .attr("height", 120);

// create the bars
svg.selectAll("rect")
  .data(data)
  .enter()
  .append("rect")
  .attr("x", function(d, i) { return i * 70; })
  .attr("y", function(d) { return 120 - (d * 5); })
  .attr("width", 65)
  .attr("height", function(d) { return d * 5; })
  .attr("fill", "steelblue");

// add labels
svg.selectAll("text")
  .data(data)
  .enter()
  .append("text")
  .text(function(d) { return d; })
  .attr("x", function(d, i) { return i * 70 + 32.5; })
  .attr("y", function(d) { return 115 - (d * 5); })
  .attr("font-size", "10px")
  .attr("text-anchor", "middle")
  .attr("fill", "white");

This code generates a bar chart using the data array [4, 8, 15, 16, 23, 42]. The resulting chart displays six bars with varying heights, and includes labels on each bar indicating its value.

Conclusion

D3.js provides a powerful set of tools for creating dynamic and interactive data visualizations that can help bring your data to life. Whether you’re working with simple datasets or complex data structures, D3.js has the capabilities you need to create compelling visual representations of your data.