- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个用 d3.js 构建的分区统计图。我将 data.csv 中的“Total_Score”属性映射到 JSON 文件中的“id”属性,并使用该分数为状态着色。我还在鼠标悬停时使用工具提示显示它。
但是,我也希望能够显示 CSV 文件中的其他属性。还有四个——“排名”、“第一”、“第二”、“第三”。我是否也必须为每个项目创建一张 map ?我从未绘制过多个 map ,因此任何指导或示例将不胜感激。
谢谢!
Here is a plunker for reference.
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Worst Drivers</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="d3.tip.js"></script>
<style type="text/css">
body{
background:#333;
font-family:Helvetica, Arial, sans-serif;
font-size:13px;
color:#fff;
padding:0px;
margin:0px;
}
h1{
margin:50px 0px 0px;
text-align:center;
font-size:30px;
font-weight:normal;
}
h2{
margin:0px;
padding:0px;
}
h4{
font-size:16px;
}
a{
text-decoration: none;
color: #2bbe8c;
}
#panel{
background:#fff;
color:#111;
width:27%;
height:100%;
float:right;
padding:3%;
}
#map{
width:66%;
float:left;
}
.sources{
font-size:12px;
}
.d3-tip{
background:#000;
opacity:.7;
padding:20px;
border-radius:3px;
}
.statename{
font-size:16px;
}
.mainno{
font-size:14px;
}
#legend{
position:absolute;
width:66%;
height:20px;
}
#legendinner{
margin:0 auto;
position:relative;
width:245px;
}
.legendleft{
width:50px;
float:left;
margin:0px;
}
.legendright{
width:50px;
float:right;
text-align:right;
margin:0px;
}
</style>
</head>
<body>
<div id="container">
<div id="map">
<div id="legend">
<div id="legendinner">
<svg width="250" height="15">
<rect x="0" y="0" width="50" height="10" fill="#041e47"></rect>
<rect x="50" y="0" width="50" height="10" fill="#063685"></rect>
<rect x="100" y="0" width="50" height="10" fill="#0449bb"></rect>
<rect x="150" y="0" width="50" height="10" fill="#055ced"></rect>
<rect x="200" y="0" width="50" height="10" fill="#5092ff"></rect>
</svg>
<p class="legendleft">Most</p>
<p class="legendright">Least</p>
</div>
</div>
</div>
</div>
<script type="text/javascript">
//Width and height
var w = Math.max(window.innerWidth) / 3 *2;
var h = Math.max(window.innerHeight) - 100;
//Create SVG element
map = d3.select("#map")
.append("svg")
.attr("id", "usstates")
.attr("width", w)
.attr("height", h);
var maph = document.getElementById('usstates').clientHeight;
d3.select("#legend")
.style("top", maph + 150 + "px");
//Define map projection
var p = d3.geo.albersUsa()
.translate([w/2, h/2])
.scale([1000]);
//Define path generator
var path = d3.geo.path()
.projection(p);
//load the geoJSON file for drawing the map
d3.json("states.json", function(error, states) {
var newDict = {}; //mapping for choropleth
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([40, 0])
.html(function(d,i) {
return "<span class='statename'>" + d.properties.name + "</span>" +
"<hr/>" +
"<span class='mainno'>Total Score: " + newDict[d.id] +
"<br/>rank: </span>" +
"<hr/>" +
"First: " +
"<br/>Second: " +
"<br/>Third: " ;
})
var mapstates = map.append("svg:g")
.attr("id", "states")
.style("fill", "#dedee0")
.style("stroke", "#aaa")
.style("stroke-width", .5);
mapstates.call(tip);
mapstates
.selectAll("path")
.data(states.features)
.enter().append("path")
.attr("d", path);
d3.csv("data.csv", function(error, data) {
data.forEach(function(d){
d.rank = + d.rank;
d.Total_Score = +d.Total_Score;
newDict[d.id] = +d.Total_Score;
});
var minValue = d3.min(data, function(d,data) { return d.Total_Score; });
var maxValue = d3.max(data, function(d,data) { return d.Total_Score; });
//Quantize scale for map
var color = d3.scale.quantize()
.domain([minValue, maxValue])
.range(["#041e47", "#063685", "#0449bb", "#055ced", "#5092ff"]);
mapstates
.selectAll("path")
.attr("d", path)
.on('mouseover', function(d) {
d3.select(this).style('fill-opacity', .75);
})
.on('mouseout', function(d){
d3.select(this).style("fill-opacity", 1);
})
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.attr("class", function(d){return newDict[d.id];})
.attr("fill", function(d) { return color(newDict[d.id]); })
.text("heyo");
mapstates.selectAll("text")
.data(states.features)
.enter()
.append("text")
.html(function(d){
return d.properties.abbr + ": " + newDict[d.id] ;
})
.attr("x", function(d){
return path.centroid(d)[0];
})
.attr("y", function(d){
return path.centroid(d)[1];
})
.attr("text-anchor","middle")
.attr('font-size',11)
.attr('font-weight', 'normal');
}); //close csv
}); // close json
</script>
</body>
</html>
data.csv 文件:
"State","id","first","second","third","TotalScore","rank"
"Alabama",1,15,24,29,113,18
"Alaska",2,22,51,50,195,49
"Arizona",4,14,28,41,109,14
"Arkansas",5,5,21,43,141,28
"California",6,42,26,39,146,33
"Colorado",8,33,4,24,101,11
"Connecticut",9,48,44,9,185,47
"Delaware",10,21,31,12,74,7
"District of Columbia",11,51,8,32,139,27
"Florida",12,19,29,30,131,24
"Georgia",13,28,11,46,147,34
"Hawaii",15,20,33,6,70,5
"Idaho",16,27,20,36,152,38
"Illinois",17,39,39,10,125,22
"Indiana",18,36,19,34,165,42
"Iowa",19,25,38,48,205,50
"Kansas",20,17,15,47,151,37
"Kentucky",21,6,2,49,131,24
"Louisiana",22,7,5,19,71,6
"Maine",23,26,10,17,103,12
"Maryland",24,41,50,18,141,28
"Massachusetts",25,50,37,8,156,39
"Michigan",26,37,42,37,179,45
"Minnesota",27,49,34,35,205,50
"Mississippi",28,8,12,26,111,17
"Missouri",29,24,18,13,91,10
"Montana",30,3,6,1,59,2
"Nebraska",31,29,16,7,141,28
"Nevada",32,30,14,14,79,8
"New Hampshire",33,43,46,33,190,48
"New Jersey",34,46,43,21,160,40
"New Mexico",35,11,27,42,109,14
"New York",36,40,36,20,143,31
"North Carolina",37,23,35,23,109,14
"North Dakota",38,4,3,2,66,4
"Ohio",39,35,22,15,135,26
"Oklahoma",40,9,1,31,87,9
"Oregon",41,34,49,45,184,46
"Pennsylvania",42,18,32,25,105,13
"Rhode Island",44,44,48,4,147,34
"South Carolina",45,1,30,3,54,1
"South Dakota",46,10,7,11,114,20
"Tennessee",47,13,25,27,143,31
"Texas",48,12,23,5,63,3
"Utah",49,45,9,51,169,44
"Vermont",50,31,13,40,113,18
"Virginia",51,38,40,38,163,41
"Washington",53,47,47,22,165,42
"West Virginia",54,2,45,44,116,21
"Wisconsin",55,32,17,16,130,23
"Wyoming",56,16,41,28,149,36
(我无法将 states.json 文件粘贴到此处,因为它超出了帖子允许的正文大小,但如果您转到 Plunker 就可以看到它。)
最佳答案
您可以创建对象并将它们放入 newDict 中。任何其他集合也可以工作。
下面是添加具有排名和分数属性的新对象的代码。
data.forEach(function(d){
d.rank = d.rank;
d.Total_Score = d.Total_Score;
newDict[d.id] = {rank:d.rank, score:d.Total_Score};
});
然后可以像普通对象一样使用它们。
mapstates
.selectAll("path")
.attr("d", path)
.on('mouseover', function(d) {
d3.select(this).style('fill-opacity', .75);
})
.on('mouseout', function(d){
d3.select(this).style("fill-opacity", 1);
})
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.attr("class", function(d){return newDict[d.id];})
attr("fill", function(d) { return color(newDict[d.id].score); });
添加任意数量的值是对 data.forEach 闭包的简单添加。
Here's my fork您的笨蛋已实现了一些更改。
关于javascript - d3.js choropleth map -- 如何将多个属性从 CSV 映射到 JSON,以便我可以在工具提示中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34293735/
以下代码根据层次聚类创建 Choropleth map (使用 hclust() 和 cutree() ): library(plotly) library(cluster) hc %
我想使用plotly和Choroplet根据某些国家的人口流动情况显示世界地图,但它不起作用,我不明白为什么。 This is my pandas table 这段代码应该绘制图表: import p
我昨天问了一个问题,但我猜是代码太多或不清楚,所以我已将其删除,并将尝试使我的问题更清楚。 我有一个包含世界上所有国家/地区的数据框 (df),它看起来像这样: Country CountryCode
我正在使用 Potly Dashboard 构建仪表板。我使用的是深色引导主题,因此我不需要白色背景。 但是,我的 map 现在看起来像这样: 生成它的代码如下所示: trace_map = html
我正在使用 Bostock's Quantile Choropleth 的变体. 我已经成功地缩放了投影并整合了我自己的数据。我目前还在过滤 json 县数据,以仅包括以州 ID 48 开头的县 ID
我尝试按照有关 leaflet 上的图层控件的教程进行操作,但无法使其按我想要的方式运行。我希望有 2 个不同的 choropleth 层可供用户在图层控件中选择,一次显示一个。但是,当我取消选中 h
我正在关注这个例子: http://bl.ocks.org/ElefHead/ebff082d41ef8b9658059c408096f782 但是,我不明白为什么我只画了 3 个东西(县、州、州边界
我根据人口使用传单制作了等值线。 Leaflet 中是否有一个属性假设 5 个地方的人口恰好为 10,然后根本不给它们着色?没有填充之类的东西? 我也无法理解为什么我设置成奶油色的时候还有深蓝色。 这
我正在使用 Bostock's Quantile Choropleth 的变体. 我已经成功缩放了投影并集成了我自己的数据。我目前还在过滤 json 县数据,仅包含以州 ID 48 开头的县 ID。
我正在尝试使用 folium 创建等值线图,按照此处的示例:https://pypi.python.org/pypi/folium .目标是制作美国失业率的等值线图,但当我打开 map 时,美国各州没
我正在尝试使用从此处下载的 geojson 数据文件绘制英国的等值线图:https://data.gov.uk/dataset/regions-december-2016-full-extent-bo
编辑** 这里是一个链接 my website .传单测试一页是目前有效的传单测试页 2 是当我尝试添加交互功能时。 我很难让我的交互式 map 真正具有交互性。我正在绘制宾夕法尼亚州各县水井数量的
我正在努力解决我在使用 folium choropleth map 时遇到的问题。我正在可视化 COVID-19 病例,并尝试根据它们的分位数隶属关系绘制颜色。然而,不幸的是,传说重叠了。因此,我想知
在 R 中,我可以像这样移动阿拉斯加和夏威夷: https://www.storybench.org/how-to-shift-alaska-and-hawaii-below-the-lower-48
我有一个关于 folium 中的 Choropleth 函数的问题。 state_data = pd.read_csv('data.csv') mssp = folium.Map() folium.C
我正在用 geopandas 制作等值线图。我想绘制具有两层边界的 map :较薄的用于民族国家(geopandas 默认),较厚的用于各种经济社区。这在地理 Pandas 中可行吗? 下面是一个例子
我很抱歉,因为我很确定这是一个基本问题。我想要做的就是使用 maps 包在 R 中创建一个非常简单的等值线图。这是我第一次尝试在 R 中映射任何数据。 我的地理位置是包括哥伦比亚特区在内的 48 个州
我有以下 Pandas 数据框 df,如下所示: import pandas as pd df = pd.DataFrame({'state' : ['NY', 'CA', 'FL', 'NJ', '
我想删除 ggplot 中生成的等值线区域之间的线。我的问题是由一张非常大的 map 引起的,其中包含非常非常小的区域(人口普查区 block 组),这些区域数量如此之多,以至于鉴于边界的密度,不可能
我已经在 Folium 中使用 Choropleth 和 Cluster 标记图工作了一段时间(非常棒)。我的问题是是否可以将它们组合在一张图中,这样我就可以看到一个变量对另一个变量的影响有多大。我可
我是一名优秀的程序员,十分优秀!