我的目标是将乘客的姓名添加到我的可视化中,以便当人们将光标移到某个点上时 - 显示该人的姓名。
到目前为止,我已经实现了目标:
在对数刻度上绘制 x 轴和 y 轴,以及表格中的数据,其中 x 轴为年龄,y 轴为票价
女的是圆的,男的是方的
幸存者的颜色较浅,而死者的颜色较亮。
现在我想附加文本,但我不确定它是否应该是:
// Add Text Labels
svg.selectAll("text")
.data(data_scatter)
.enter()
.append("text")
.text(function(d) {
return d.name;
})
.attr("font_family", "sans-serif") // Font type
.attr("font-size", "11px") // Font size
.attr("fill", "darkgreen"); // Font color
我使用此代码进行可视化:
// set the dimensions and margins of the graph
var margin = {
top: 10,
right: 30,
bottom: 30,
left: 60
},
width = 460 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
//Read the data
d3.csv("https://gist.githubusercontent.com/michhar/2dfd2de0d4f8727f873422c5d959fff5/raw/fa71405126017e6a37bea592440b4bee94bf7b9e/titanic.csv", function(rawData) {
// All values are strings here, so we need to parse some of them.
// You can do that using `+x` or `Number(x)`, where `x = "123"`
const data = rawData.map(function(d) {
return {
age: Number(d.Age),
// cabin: d.Cabin,
// embarked: e.Embarked,
fare: Number(d.Fare),
// name: d.Name,
// parch: Number(d.Parch),
// passengerId: Number(d.PassengerId)
// pclass: Number(Pclass),
sex: d.Sex,
// sibSp: Number(d.SibSp),
survived: d.Survived === "1"
// ticket: d.Ticket,
};
});
qq_遁去的一_1
斯蒂芬大帝
相关分类