- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在 d3.js 图表线工具提示上显示 csv 文件的温度和时间戳列下的数据。我希望在工具提示上显示相应的日期和温度
这是我从控制台收到的错误“未捕获类型错误:无法读取未定义的属性“时间戳” 在 SVGPathElement 处。 (索引.js:70) 在 SVGPathElement 处。 (d3.v5.js:2)"
div.html(formatTime((d.timestamp)) + "
"+ (+d.温度))
我希望日期和温度应该显示在工具提示上
let svg = d3.select('svg'),
height = +svg.attr('height'),
width = +svg.attr('width');
let formatTime = d3.timeFormat("%m/%d/%Y");
const render = data => {
const title = 'Engagements Over-Time(Total: 946K)';
const xValue = d => d.timestamp;
const yValue = d => d.temperature;
const margin = {top: 60, right: 60, bottom: 88, left: 105};
const innerwidth = width - margin.left - margin.right;
const innerheight = height - margin.top - margin.bottom;
const xScale = d3.scaleTime()
.domain(d3.extent(data, xValue))
.range([0, innerwidth - 150])
.nice();
const yScale = d3.scaleLinear()
.domain([0, d3.max(data, d => d.temperature)])
.range([innerheight, 0])
.nice();
const g = svg.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top+30})`);
let parseDate = d3.timeFormat("%m/%d/%Y");
const xAxis = d3.axisBottom(xScale)
.tickFormat(parseDate)
.tickPadding(5)
// Define the div for the tooltip
let div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0)
.style("display", "null");;
let yAxisTicFormat = number => d3.format('.1s')(number)
const yAxis = d3.axisLeft(yScale)
.tickPadding(35)
.tickFormat(yAxisTicFormat)
;
const yAxisG = g.append('g').call(yAxis);
yAxisG.selectAll('.domain').remove();
const xAxisG = g.append('g').call(xAxis)
.attr('transform', `translate(0, ${innerheight})`);
xAxisG.select('.domain').remove();
const lineGenerator = d3.line()
.x(d => xScale(xValue(d)))
.y(d => yScale(yValue(d)))
.curve(d3.curveBasis);
g.append('path')
.attr('class', 'line-path')
.attr('d', lineGenerator(data))
.on("mouseover", function(d) {
div.transition()
.duration(500)
.style("opacity", 1)
.style("display", "block");
console.log(xValue);
div.html(formatTime((d.timestamp)) + "<br/>" + (+d.temperature))
.style("left", (d3.event.pageX-1) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0); });;
g.append('text')
.attr('class', 'title')
.attr('y', -40)
.text(title);
};
d3.csv('temperature-in-san-francisco.csv').then(data =>{
//console.log(data)
data.forEach(d => {
d.timestamp =new Date (d.timestamp);
d.temperature = +d.temperature*1000;
});
render(data);
//console.log(data)
})
我的数据
时间戳、温度
2007-04-23,93.24
2007-04-24,95.35
2007-04-25,98.84
2007-04-26,99.92
最佳答案
看起来您认为在鼠标悬停事件上将返回不同的数据点,具体取决于光标触摸该行的位置,但重要的是要注意该行只是由路径定义字符串定义的单个 svg 元素.
您的数据数组已传递到行生成器,该生成器返回定义行的字符串,该字符串被设置为路径的d
(对于定义而不是数据(可能))属性(property)。已创建 svg path
元素,但尚未绑定(bind)任何数据。
所以原因
div.html(formatTime((d.timestamp))
抛出引用错误是因为没有数据绑定(bind)到 path
元素,因此回调的第一个参数 (d) 返回 undefined
。
如果您希望用户能够触摸线上的不同位置并查看数据,那么您可以使用与线定义相同的比例在该线上创建点(圆圈)。各个circle
元素将绑定(bind)了数据。
有多种方法可以在鼠标悬停在圆圈上时显示工具提示 on this SO post 。最简单的似乎是 Lars Kotthoff 建议的,它不需要鼠标悬停监听器,而是使用浏览器的内置功能,在悬停时显示元素的标题(这来自 Lar 的示例):
d3.selectAll("circle")
.data(data)
.enter()
.append("svg:circle")
.attr('cx', d => xScale(d.timestamp))
.attr('cy', d => yScale(d.temperature))
.attr('r', 3)
.attr('fill', 'steelblue')
.append("svg:title")
.text(function(d) { return d.x; });
关于javascript - d3.js 折线图工具提示内容未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57747207/
这个问题在这里已经有了答案: Android ADT version required 20.0.0 and above (10 个答案) 关闭 9 年前。 我刚刚安装了 Eclipse Juno
按照 This page from codeplex 上的指南进行操作后,我无法在我的工具/选项窗口中看到 Python 选项。我认为我与指南的唯一偏差是: 发行版:没有安装 activestate
我有一个非常大的 .sql 脚本。我将此脚本添加到 Visual Studio 2013 下的 SQL Server 项目中。当我尝试构建它时,我收到此错误消息 This T-SQL script e
当我在SpringBoot项目中想加个依赖,但是不确定现有依赖的依赖的依赖.....有没有添加过这个依赖,怎么办呢?如果添加过了但是不知道我需要的这个依赖属于哪个依赖的下面,怎么查呢? IDEA中提供
我正在做一个项目来减少 PDF 的大小,压缩它们。我想知道市场上是否有任何非常好的工具/库(.NET)。 我确实尝试了一些像 Onstream Compression 这样的工具,但结果并不令人满意。
我想从我的源代码编译一个安卓内核。 但我想使用工具或类似的东西。 所以我只需单击一个按钮并获得一个可闪存的 zip 文件... 有工具吗? 我可以用脚本来做吗? 谢谢! 最佳答案 这取决于您从哪里获得
我们生成 pdf 文件,其中包含有关数万名客户每月财务余额的数据。在高峰期(年底有 100.000 个文件),使用在 5 台服务器之间分配负载,该过程可能需要长达 5 天的时间才能完成。工作负载的分配
模块:xmllib xmllib 是一个非验证的低级语法分析器。应用程序员使用的 xmllib 可以覆盖 XMLParser 类,并提供处理文档元素(如特定或类属标记,或字符实体)的方法。从 Py
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 3 年前。
我在一家医疗保健公司工作,拥有有关患者位置(地址、城市、州、 zip )的信息。我试图确定有多少百分比的患者住在离 5 个特定位置最近的地方。我正在寻找的答案是“25% 的患者住在离#1 地点最近的地
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 4年前关闭。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
请问我在哪里可以得到 SvcTraceViewer 工具? 我尝试下载并安装许多 SDK。 我查看了程序文件的垃圾箱。 我需要它来跟踪我的 WCF 调用出了什么问题。 最佳答案 您可以通过下载 Win
我正在尝试在我最喜欢的编辑器中设置适当的代码完成功能,我们将其称为AnEditor,以避免互联网上充斥着特定于程序的答案。 (您知道语言是ALanguage。)编辑器具有两个我喜欢的功能:它既可以在控
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
当 merge 的两个分支对同一文件有更改时,Mercurial 是否总是使用外部 merge 工具? 或者它是否首先查看它是否可以 merge 文件本身,如果不能,则仅转向外部工具? 我问的原因是我
我正在为我使用的编辑器编写 Scala 插件,该插件将突出显示所有未使用的代码路径(可能未使用 defs 、 vals 、 classes 和 implicits ),并为用户提供一个选项以将它们从.
我有 jquery 工具滚动器...我喜欢它只为 swipeLeft swipeRight 实现触摸选项。 当我使用 touch: true 时,它也会在向上/向下滑动时旋转.. 我按照此处的说明
我已经尝试了一些用于构建 UML(对象/依赖图)的 Eclipse 工具,但我真正需要的是一个工具来生成这样的代码外 UML。 (反之亦然) 我更喜欢一个简单的 UML 工具,它易于安装并且没有任何依
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我是一名优秀的程序员,十分优秀!