猿问

在网络浏览器中显示 D3.js 示例(菜鸟问题)

如果我想显示地图,您可以在这里看到: https: //observablehq.com/@d3/zoom-to-bounding-box in chrome 例如,我该如何实现该工作?我尝试将您可以在地图下方看到的源代码复制为标准 html 格式(在正文中),但它只显示 chrome 中的源代码或根本不显示任何内容。

对于这样一个菜鸟问题,我深表歉意,但如果您能帮助我,我将不胜感激!提前致谢



梵蒂冈之花
浏览 207回答 1
1回答

慕姐4208626

https://codesandbox.io/s/stackoverflow-64928268-fetch-12fxi?file=/src/index.js首先,您需要导入该页面底部的模块/文件不要忘记此存储库中的 JSON 数据:https://github.com/topojson/us-atlas为此,我建议使用 npm 以及 Parcel 或 Webpack 构建页面。为此,您可以package.json在新目录中创建该文件。确保你的机器上安装了nodejs和npm,然后:从控制台类型npm install然后键入npm run start在本地运行。或者npm run build将应用程序打包上传到网上./package.json{&nbsp; "name": "vanilla",&nbsp; "version": "1.0.0",&nbsp; "description": "StackOverflow",&nbsp; "main": "index.html",&nbsp; "scripts": {&nbsp; &nbsp; "start": "parcel index.html --open",&nbsp; &nbsp; "build": "parcel build index.html"&nbsp; },&nbsp; "dependencies": {&nbsp; &nbsp; "d3": "6.2.0",&nbsp; &nbsp; "path": "0.12.7",&nbsp; &nbsp; "topojson-client": "3.1.0"&nbsp; },&nbsp; "devDependencies": {&nbsp; &nbsp; "@babel/core": "7.2.0",&nbsp; &nbsp; "parcel-bundler": "^1.6.1"&nbsp; },&nbsp; "keywords": [&nbsp; &nbsp; "javascript",&nbsp; &nbsp; "starter"&nbsp; ]}./index.html<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <title>Stack Overflow</title>&nbsp; &nbsp; <meta charset="UTF-8" />&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <script src="src/index.js"></script>&nbsp; </body></html>然后我发现将图表定义为函数很有用。./src/index.jsconst topojson = require("topojson-client");const d3 = require("d3");const path = d3.geoPath();const getMapData = async () => {&nbsp; const response = await fetch(&nbsp; &nbsp; "https://cdn.jsdelivr.net/npm/us-atlas@3/states-albers-10m.json"&nbsp; ).then((response) => response.json());&nbsp; return response;};const buildAndDisplayChart = async (domElement) => {&nbsp; const width = 975;&nbsp; const height = 610;&nbsp; const us = await getMapData()&nbsp; const zoom = d3.zoom().scaleExtent([1, 8]).on("zoom", zoomed);&nbsp; const svg = d3&nbsp; &nbsp; .create("svg")&nbsp; &nbsp; .attr("viewBox", [0, 0, width, height])&nbsp; &nbsp; .on("click", reset);&nbsp; const g = svg.append("g");&nbsp; const states = g&nbsp; &nbsp; .append("g")&nbsp; &nbsp; .attr("fill", "#444")&nbsp; &nbsp; .attr("cursor", "pointer")&nbsp; &nbsp; .selectAll("path")&nbsp; &nbsp; .data(topojson.feature(us, us.objects.states).features)&nbsp; &nbsp; .join("path")&nbsp; &nbsp; .on("click", clicked)&nbsp; &nbsp; .attr("d", path);&nbsp; states.append("title").text((d) => d.properties.name);&nbsp; g.append("path")&nbsp; &nbsp; .attr("fill", "none")&nbsp; &nbsp; .attr("stroke", "white")&nbsp; &nbsp; .attr("stroke-linejoin", "round")&nbsp; &nbsp; .attr("d", path(topojson.mesh(us, us.objects.states, (a, b) => a !== b)));&nbsp; svg.call(zoom);&nbsp; function reset() {&nbsp; &nbsp; states.transition().style("fill", null);&nbsp; &nbsp; svg&nbsp; &nbsp; &nbsp; .transition()&nbsp; &nbsp; &nbsp; .duration(750)&nbsp; &nbsp; &nbsp; .call(&nbsp; &nbsp; &nbsp; &nbsp; zoom.transform,&nbsp; &nbsp; &nbsp; &nbsp; d3.zoomIdentity,&nbsp; &nbsp; &nbsp; &nbsp; d3.zoomTransform(svg.node()).invert([width / 2, height / 2])&nbsp; &nbsp; &nbsp; );&nbsp; }&nbsp; function clicked(event, d) {&nbsp; &nbsp; const [[x0, y0], [x1, y1]] = path.bounds(d);&nbsp; &nbsp; event.stopPropagation();&nbsp; &nbsp; states.transition().style("fill", null);&nbsp; &nbsp; d3.select(this).transition().style("fill", "red");&nbsp; &nbsp; svg&nbsp; &nbsp; &nbsp; .transition()&nbsp; &nbsp; &nbsp; .duration(750)&nbsp; &nbsp; &nbsp; .call(&nbsp; &nbsp; &nbsp; &nbsp; zoom.transform,&nbsp; &nbsp; &nbsp; &nbsp; d3.zoomIdentity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .translate(width / 2, height / 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .scale(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Math.min(8, 0.9 / Math.max((x1 - x0) / width, (y1 - y0) / height))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .translate(-(x0 + x1) / 2, -(y0 + y1) / 2),&nbsp; &nbsp; &nbsp; &nbsp; d3.pointer(event, svg.node())&nbsp; &nbsp; &nbsp; );&nbsp; }&nbsp; function zoomed(event) {&nbsp; &nbsp; const { transform } = event;&nbsp; &nbsp; g.attr("transform", transform);&nbsp; &nbsp; g.attr("stroke-width", 1 / transform.k);&nbsp; }&nbsp; document.querySelector(domElement).appendChild(svg.node())};buildAndDisplayChart("body")该函数将生成一个 SVG,然后您可以将其附加到您选择的 DOM 元素中。您的最终目录列表应如下所示:
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答