简体   繁体   English

如何使用 d3.js 从数据中向我的散点图添加标签

[英]How to add labels to my scatterplot from data using d3.js

I am learning d3.js for visualization and using titanic dataset我正在学习 d3.js 进行可视化和使用泰坦尼克号数据集

My aim is to add the names of the passengers to my visualization so that when one moves cursor over a point - the name of the person is displayed.我的目标是将乘客的姓名添加到我的可视化中,这样当一个人将光标移到一个点上时,就会显示该人的姓名。

So far, I have achieved the goals:到目前为止,我已经实现了以下目标:

  1. plot the x axis and y axis on log scale and the data from the table which has age on x-axis and fare on y-axis在对数刻度上绘制 x 轴和 y 轴,以及表格中的数据,其中 x 轴为年龄,y 轴为票价
  2. The female is circle and male is square女性是圆形,男性是方形
  3. the survived has light color while the dead have brighter color.幸存的颜色较浅,而死者的颜色较亮。

Now I want to append text and I am not sure if it should be:现在我想附加文本,我不确定它是否应该是:

 // 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

I am using this code for the visualization:我正在使用此代码进行可视化:

 // 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, }; }); // Add X axis var x = d3.scaleLinear() .domain([0, 80]) .range([0, width]); svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); // Add Y axis var y = d3.scaleLog() .domain([1e+0, 1e+3]) .range([height, 0]); svg.append("g") .call(d3.axisLeft(y)); // Add dots svg.append('g') .selectAll("path") .data(data) .enter() .append("path") .attr("transform", function(d) { return "translate(" + [x(d.age), y(d.fare)] + ")"; }) .attr("d", function(d) { const path = d3.path(); const shape = d.sex == "female" ? d3.symbolCircle : d3.symbolSquare; shape.draw(path, 8); return path.toString(); }) .style("fill-opacity", function(d) { return d.survived ? "0.3" : "1"; }) })
 <script src="https://d3js.org/d3.v4.js"></script> <!-- Create a div where the graph will take place --> <div id="my_dataviz"></div>

I got so far:我到目前为止:

在此处输入图片说明

Now I want to add labels of names to the points.现在我想为点添加名称标签。

Can anyone please help me.谁能帮帮我吗。

To add a title attribute to each path , do something like this:要为每个path添加title属性,请执行以下操作:

Remember to uncomment name: d.Name to make sure name is known.记得取消注释name: d.Name以确保name是已知的。 Also note that if you open the generated HTML in the DOM inspector, you can see that every path now has a title child node.另请注意,如果您在 DOM 检查器中打开生成的 HTML,您可以看到每个path现在都有一个title子节点。 The DOM inspector should - after the console - always be the first thing you check when debugging d3.js在调试 d3.js 时,DOM 检查器应该 - 在控制台之后 - 始终是您检查的第一件事

 // 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, }; }); // Add X axis var x = d3.scaleLinear() .domain([0, 80]) .range([0, width]); svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); // Add Y axis var y = d3.scaleLog() .domain([1e+0, 1e+3]) .range([height, 0]); svg.append("g") .call(d3.axisLeft(y)); // Add dots svg.append('g') .selectAll("path") .data(data) .enter() .append("path") .attr("transform", function(d) { return "translate(" + [x(d.age), y(d.fare)] + ")"; }) .attr("d", function(d) { const path = d3.path(); const shape = d.sex == "female" ? d3.symbolCircle : d3.symbolSquare; shape.draw(path, 8); return path.toString(); }) .style("fill-opacity", function(d) { return d.survived ? "0.3" : "1"; }) .append("title") .text(function(d) { return d.name; }); })
 <script src="https://d3js.org/d3.v4.js"></script> <!-- Create a div where the graph will take place --> <div id="my_dataviz"></div>

In order to solve what I wanted to do, I just needed to do the following:为了解决我想做的事情,我只需要执行以下操作:

  1. using name: d.Name read the name column data and then in the final part: add使用 name: d.Name 读取 name 列数据,然后在最后一部分:添加

 .append("svg:title") .text(function(d) { return d.name});

after the styling component.在样式组件之后。

That gets me to displaying names when I hover over the points.当我将鼠标悬停在点上时,这让我可以显示名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM