d3.js v5 数据加载

d3.js v5 数据加载

d3提供了几种数据加载的方式:

  • d3.csv()
  • d3.json()
  • d3.tsv()
  • d3.xml()

在这里插入图片描述

d3.js 数据加载在 v5 版本之后有重大的变化:
D3现在使用Promises而不是异步回调来加载数据。Promises简化了异步代码的结构,尤其是在支持async和await的现代浏览器中。

Changes in D3 5.0

Released March 22, 2018.

D3 5.0 introduces only a few non-backwards-compatible changes.

D3 now uses Promises instead of asynchronous callbacks to load data. Promises simplify the structure of asynchronous code, especially in modern browsers that support async and await. (See this introduction to promises on Observable.) For example, to load a CSV file in v4, you might say:

d3.csv("file.csv", function(error, data) {
  if (error) throw error;
  console.log(data);
});

In v5, using promises:

d3.csv("file.csv").then(function(data) {
  console.log(data);
});

Note that you don’t need to rethrow the error—the promise will reject automatically, and you can promise.catch if desired. Using await, the code is even simpler:

const data = await d3.csv("file.csv");
console.log(data);

With the adoption of promises, D3 now uses the Fetch API instead of XMLHttpRequest: the d3-request module has been replaced by d3-fetch. Fetch supports many powerful new features, such as streaming responses. D3 5.0 also deprecates and removes the d3-queue module. Use Promise.all to run a batch of asynchronous tasks in parallel, or a helper library such as p-queue to control concurrency.

D3 no longer provides the d3.schemeCategory20* categorical color schemes. These twenty-color schemes were flawed because their grouped design could falsely imply relationships in the data: a shared hue can imply that the encoded data are part of a group (a super-category), while relative lightness can imply order. Instead, D3 now includes d3-scale-chromatic, which implements excellent schemes from ColorBrewer, including categorical, diverging, sequential single-hue and sequential multi-hue schemes. These schemes are available in both discrete and continuous variants.

D3 now provides implementations of marching squares and density estimation via d3-contour! There are two new d3-selection methods: selection.clone for inserting clones of the selected nodes, and d3.create for creating detached elements. Geographic projections now support projection.angle, which has enabled several fantastic new polyhedral projections by Philippe Rivière.

Lastly, D3’s package.json no longer pins exact versions of the dependent D3 modules. This fixes an issue with duplicate installs of D3 modules.

我发现我在使用d3.js v6 版本加载中国地图的json文件渲染地图的时候,湖北省的path出现了错误,当我降低版本到v5之后错误得到的解决。

异步加载地图数据可以提高页面加载速度和渲染效率,D3.js提供了多种方式实现异步加载地图数据,以下是一种常用方式的代码示例: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>中国地图</title> <style> svg { width: 100%; height: 100%; } .province { fill: #ccc; stroke: #fff; stroke-width: 1px; } .city { fill: #f00; stroke: #fff; stroke-width: 1px; } </style> <script src="https://d3js.org/d3.v5.min.js"></script> </head> <body> <svg id="map"></svg> <script> // 创建地图容器 var svg = d3.select("#map"); var width = svg.node().getBoundingClientRect().width; var height = svg.node().getBoundingClientRect().height; // 定义投影 var projection = d3.geoMercator() .center([105, 38]) .scale(600) .translate([width / 2, height / 2]); // 定义路径生成器 var path = d3.geoPath() .projection(projection); // 异步加载省份数据 d3.json("provinces.json").then(function(provinces) { // 绘制省份 svg.selectAll(".province") .data(provinces.features) .enter() .append("path") .attr("class", "province") .attr("d", path); }); // 异步加载城市数据 d3.json("cities.json").then(function(cities) { // 绘制城市 svg.selectAll(".city") .data(cities.features) .enter() .append("circle") .attr("class", "city") .attr("cx", function(d) { return projection(d.geometry.coordinates)[0]; }) .attr("cy", function(d) { return projection(d.geometry.coordinates)[1]; }) .attr("r", 5); }); </script> </body> </html> ``` 该示例代码中使用了D3.js的Promise机制实现异步加载地图数据。通过`d3.json()`方法异步加载JSON格式的地图数据,然后在`then()`回调函数中绘制地图。由于异步加载是并行进行的,因此可以提高地图绘制效率。需要注意的是,在异步加载过程中需要进行错误处理,确保地图数据加载成功并正确解析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X01动力装甲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值