javascript - JSON 映射上的用户输入到 Project

标签 javascript json d3.js

我在此处提供了以下 D3.js 项目:

http://bl.ocks.org/diggetybo/raw/e75dcb649ae3b26e2312a63434fc970c/

纬度和经度输入位于 map 下方。

它应该接受用户输入的纬度和经度数字,并在给定坐标处“投影”svg 圆。问题是我要么得到____不是函数错误,要么开发工具根本没有抛出任何错误,但圆圈永远不会被投影。

这是一个短文件,有人可以解释为什么它没有按照我想象的方式工作吗?

最佳答案

你的更新功能没有任何意义。

  1. 它接受两个输入,但您只能用一个来调用它。
  2. .selectAll("circle").enter() 不是有效的 d3 语法。
  3. 您需要使用纬度和经度调用projection,您传递0,这将导致它返回null,因为它超出了投影。
  4. 解决所有这些问题后,您仍然会失败,因为您已经将路径移动了您的边距,并且最好将它们放入按边距移动的 g 中。<

话虽如此,简单的重写将是:

var lat = d3.select("#latValue").on("input", function() {
    update();
}).node();

var long = d3.select("#lonValue").on("input", function() {
    update();
}).node();

function update() {

  // lat/long to pixel
  var coors = projection([long.value, lat.value]);

  // if outside projection don't add circle
  if (coors === null) return;

  // add circle
    container
        .append("circle")
        .attr("cx", coors[0])
        .attr("cy", coors[1])
        .attr("r", Math.sqrt(5) * 4)
        .style("fill", "black")
        .style("opacity", 0.85);
}

运行代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<style type="text/css">

/* On mouse hover, lighten state color */
path:hover {
	fill-opacity: .7;
}
</style>
</head>
<body>
<script type="text/javascript">

//Width and height of map
var width = 960;
var height = 500;
var margins = { left: 0, top: 100, right: 0, bottom: 0 };
// D3 Projection
var projection = d3.geo.albersUsa()
				   .translate([width/2, height/2])    // translate to center of screen
				   .scale([1000]);          // scale things down so see entire US

// Define path generator
var path = d3.geo.path()               // path generator that will convert GeoJSON to SVG paths
		  	 .projection(projection);  // tell path generator to use albersUsa projection

// Define linear scale for output
var color = d3.scale.linear()
			  .range(["#c3e2ff","#15198e"]);

//Create SVG element and append map to the SVG
var svg = d3.select("body")
			.append("svg")
			.attr("width", width)
			.attr("height", height+margins.top);

svg.append('text')
  .text('Coordinate Query')
  .attr('font-size','24px')
  .attr('transform', 'translate(' + 30 + ',' +70 + ')')
  .attr('font-family','Calibri');

svg.append('text')
  .text('Data as of 12/2016')
  .attr('font-size','12px')
  .attr('transform', 'translate(' + 35 + ',' +100 + ')')
  .attr('font-family','Calibri');
// Load in my states data!
color.domain([0,100]); // setting the range of the input data
// Load GeoJSON data and merge with states data
d3.json("https://jsonblob.com/api/573228c3-d068-11e6-b16a-b501dc8d2b08", function(json) {

//var coordinates = d3.mouse(this);
// Bind the data to the SVG and create one path per GeoJSON feature
var container = svg.append("g")
	.attr('transform', 'translate(' + margins.left + ',' + margins.top + ')');

container.selectAll("path")
	.data(json.features)
	.enter()
	.append("path")
	.attr("d", path)  
	.style("stroke", "#fff")
  .style("stroke-linejoin","round")
	.style("stroke-width", "1.5")
	.style("fill", 'steelblue');


// Modified Legend Code from Mike Bostock: http://bl.ocks.org/mbostock/3888852
var lat = d3.select("#latValue").on("input", function() {
	update();
}).node();

var long = d3.select("#lonValue").on("input", function() {
	update();
}).node();

function update() {
  
  // lat/long to pixel
  var coors = projection([long.value, lat.value]);
  
  // if outside projection don't add circle
  if (coors === null) return;
  
  // add circle
	container
		.append("circle")
		.attr("cx", coors[0])
		.attr("cy", coors[1])
		.attr("r", Math.sqrt(5) * 4)
		.style("fill", "black")
		.style("opacity", 0.85);

}
  
});
</script>
<p>
	<label 	for="latValue"
					style="display: inline-block;width:240px;text-align:right;font-size:18px;font-family:Play">
					Lattitude:<span id="latValue-value"></span>
	</label>
	<input type="number"min="-360"max="360"step="1"value="0" id="latValue">
	<label 	for="lonValue"
					style="display: inline-block;width:240px;text-align:right;font-size:18px;font-family:Play">
					Longitude:<span id="lonValue-value"></span>
	</label>
	<input type="number"min="-360"max="360"step="1"value="0" id="lonValue">
</p>
</body>
</html>

关于javascript - JSON 映射上的用户输入到 Project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41419032/

相关文章:

d3.js - 如何等待 d3.json 完成?

javascript - 禁用当前页面的 JavaScript

javascript - 将所有具有类的 <p> 元素更改为 <code>

javascript - 使用 JavaScript 调用事件 click

javascript - Angular JS ng-repeat 具有复杂的 JSON 数据(数组、子数组、子对象)

ruby-on-rails - 在 Ruby 中查询 JSON 嵌套哈希响应时出现问题

java - 在 Android Studio 中解析 JSON 对象

javascript - donut 饼图 - 添加标题 - NVd3.js

javascript - Css 和 Javascript 的问题

javascript - 在 D3.js 中隐藏不相关的父节点但子节点