Skip to content

d3-fetch

此模块在 获取 之上提供了便捷的解析功能。例如,加载文本文件:

¥This module provides convenient parsing on top of Fetch. For example, to load a text file:

js
const text = await d3.text("hello-world.txt"); // "Hello, world!"

要加载并解析 CSV 文件:

¥To load and parse a CSV file:

js
const data = await d3.csv("hello-world.csv"); // [{"Hello": "world"}, …]

此模块内置了对 JSONCSVTSV 的解析支持。你可以直接使用 text 解析其他格式。(此模块取代了 d3-request。)

¥This module has built-in support for parsing JSON, CSV, and TSV. You can parse additional formats by using text directly. (This module replaced d3-request.)

blob(input, init)

js
const blob = await d3.blob("example.db");

源代码 · 从指定的输入 URL 获取二进制文件,并将其作为 Blob 对象。如果指定了 init,则将其传递给底层对 fetch 的调用;有关允许的字段,请参阅 RequestInit

¥Source · Fetches the binary file at the specified input URL as a Blob. If init is specified, it is passed along to the underlying call to fetch; see RequestInit for allowed fields.

buffer(input, init)

js
const buffer = await d3.buffer("example.db");

源代码 · 从指定的输入 URL 获取二进制文件,并将其作为 ArrayBuffer 对象。如果指定了 init,则将其传递给底层对 fetch 的调用;有关允许的字段,请参阅 RequestInit

¥Source · Fetches the binary file at the specified input URL as an ArrayBuffer. If init is specified, it is passed along to the underlying call to fetch; see RequestInit for allowed fields.

csv(input, init, row)

js
const data = await d3.csv("example.csv");

源代码 · 等同于 d3.dsv,但以逗号为分隔符。

¥Source · Equivalent to d3.dsv with the comma character as the delimiter.

dsv(delimiter, input, init, row)

js
const data = await d3.dsv(",", "example.csv");

源代码 · 从指定的输入 URL 获取 DSV 文件。如果指定了 init,则将其传递给底层对 fetch 的调用;有关允许的字段,请参阅 RequestInit。可以指定一个可选的行转换函数,用于将行对象映射和过滤为更具体的表示形式;详情参见 dsv.parse。例如:

¥Source · Fetches the DSV file at the specified input URL. If init is specified, it is passed along to the underlying call to fetch; see RequestInit for allowed fields. An optional row conversion function may be specified to map and filter row objects to a more-specific representation; see dsv.parse for details. For example:

js
const data = await d3.dsv(",", "example.csv", (d) => {
  return {
    year: new Date(+d.Year, 0, 1), // convert "Year" column to Date
    make: d.Make,
    model: d.Model,
    length: +d.Length // convert "Length" column to number
  };
});

如果只指定了 init 和 row 中的一个,则如果它是一个函数,则将其解释为 row 转换函数,否则解释为 init 对象。另请参阅 d3.csvd3.tsv

¥If only one of init and row is specified, it is interpreted as the row conversion function if it is a function, and otherwise an init object. See also d3.csv and d3.tsv.

html(input, init)

js
const document = await d3.html("example.html");

源代码 · 从指定的输入 URL 获取文件,并将其作为 text 对象,然后将 解析它 对象作为 HTML 对象。如果指定了 init,则将其传递给底层对 fetch 的调用;有关允许的字段,请参阅 RequestInit

¥Source · Fetches the file at the specified input URL as text and then parses it as HTML. If init is specified, it is passed along to the underlying call to fetch; see RequestInit for allowed fields.

image(input, init)

js
const image = await d3.image("example.png");

源代码 · 从指定的输入 URL 获取图片。如果指定了 init,则在加载图片之前设置图片的任何其他属性。例如,要启用匿名 跨源请求

¥Source · Fetches the image at the specified input URL. If init is specified, sets any additional properties on the image before loading. For example, to enable an anonymous cross-origin request:

js
const image = await d3.image("https://example.com/image.png", {crossOrigin: "anonymous"});

json(input, init)

js
const data = await d3.json("example.json");

源代码 · 从指定的输入 URL 获取 JSON 文件。如果指定了 init,则将其传递给底层对 fetch 的调用;有关允许的字段,请参阅 RequestInit。如果服务器返回状态码 204 无内容205 重置内容,则 Promise 解析为 undefined

¥Source · Fetches the JSON file at the specified input URL. If init is specified, it is passed along to the underlying call to fetch; see RequestInit for allowed fields. If the server returns a status code of 204 No Content or 205 Reset Content, the promise resolves to undefined.

svg(input, init)

js
const document = await d3.svg("example.svg");

源代码 · 从指定的输入 URL 获取文件,并将其作为 text 对象,然后将 解析它 对象作为 SVG 对象。如果指定了 init,则将其传递给底层对 fetch 的调用;有关允许的字段,请参阅 RequestInit

¥Source · Fetches the file at the specified input URL as text and then parses it as SVG. If init is specified, it is passed along to the underlying call to fetch; see RequestInit for allowed fields.

text(input, init)

js
const text = await d3.text("example.txt");

源代码 · 从指定的输入 URL 获取文本文件。如果指定了 init,则将其传递给底层对 fetch 的调用;有关允许的字段,请参阅 RequestInit

¥Source · Fetches the text file at the specified input URL. If init is specified, it is passed along to the underlying call to fetch; see RequestInit for allowed fields.

tsv(input, init, row)

js
const data = await d3.tsv("example.tsv");

源代码 · 等同于 d3.dsv,但以制表符为分隔符。

¥Source · Equivalent to d3.dsv with the tab character as the delimiter.

xml(input, init)

js
const document = await d3.xml("example.xml");

源代码 · 从指定的输入 URL 获取文件,并将其作为 text 对象,然后将 解析它 对象作为 XML 对象。如果指定了 init,则将其传递给底层对 fetch 的调用;有关允许的字段,请参阅 RequestInit

¥Source · Fetches the file at the specified input URL as text and then parses it as XML. If init is specified, it is passed along to the underlying call to fetch; see RequestInit for allowed fields.