Skip to content

d3-fetch

此模块在 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");

来源 · 将指定的 input URL 的二进制文件作为 Blob 获取。如果指定了 init,它将传递给底层的 fetch 调用;有关允许的字段,请参阅 RequestInit

缓冲(input, init)

🌐 buffer(input, init)

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

· 将指定 input URL 的二进制文件作为 ArrayBuffer 获取。如果指定了 init,它会传递给底层的 fetch 调用;有关允许字段,请参阅 RequestInit

csv(输入, 初始化, )

🌐 csv(input, init, row)

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

来源 · 相当于使用逗号作为分隔符的 d3.dsv

dsv(分隔符, 输入, 初始值, )

🌐 dsv(delimiter, input, init, row)

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

来源 · 获取指定 input URL 的 DSV 文件。如果指定了 init,它会传递给底层的 fetch 调用;允许的字段请参见 RequestInit。可以指定一个可选的 row 转换函数,将行对象映射和过滤为更具体的表示;详情请参见 dsv.parse。例如:

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
  };
});

如果只指定了 initrow 中的一个,如果它是一个函数,则将其解释为 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");

来源 · 获取指定 input URL 的文件作为 文本,然后将其作为 HTML 解析。如果指定了 init,它将传递给底层的 fetch 调用;有关允许的字段,请参见 RequestInit

图片(输入, 初始)

🌐 image(input, init)

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

来源 · 获取指定 input URL 的图片。如果指定了 init,则在加载前设置图片的任何附加属性。例如,要启用匿名的 跨来源请求

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

json(input, init)

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

来源 · 获取指定 input URL 的 JSON 文件。如果指定了 init,它会传递给底层的 fetch 调用;允许的字段请参见 RequestInit。如果服务器返回 204 无内容205 重置内容 的状态码,promise 会解析为 undefined

svg(输入, 初始化)

🌐 svg(input, init)

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

来源 · 获取指定 input URL 的文件作为 文本,然后将其 解析 为 SVG。如果指定了 init,它将传递给底层的 fetch 调用;有关允许的字段,请参见 RequestInit

text(input, init)

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

· 获取指定 input URL 的文本文件。如果指定了 init,它将传递给底层的 fetch 调用;有关允许的字段,请参见 RequestInit

tsv(输入, 初始化, 行)

🌐 tsv(input, init, row)

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

来源 · 相当于使用制表符作为分隔符的 d3.dsv

xml(输入, 初始化)

🌐 xml(input, init)

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

来源 · 获取指定 input URL 的文件作为 文本,然后将其作为 XML 解析。如果指定了 init,它将传递给底层的 fetch 调用;有关允许的字段,请参见 RequestInit