javascript - 在 D3.js 中查找两个日期之间的中间日期
问题描述
我正在使用 D3.js 散点图根据时间段显示作品数量。我想在每个时期的开始日期和结束日期之间的中间日期放置点。我拥有的数据如下所示:
period, amount, startDate, endDate
1, 5, 26/11/1982, 2/6/1989
2, 6, 2/6/1989,2/1/1990
3, 11, 2/01/1990,20/6/1999
我尝试使用以下方法在图表上映射点:
.attr("cx", function(d) { return xScale(new Date(d.endDate)) - xScale(new Date(d.startDate)) * 0.5 })
但这不起作用。我错过了什么?
解决方案
两个给定点之间的中点是:
middleX = (x1 + x2) / 2;
在你的情况下:
d => {
const xFrom = xScale(new Date(d.startDate));
const xTo = xScale(new Date(d.endDate));
return (xFrom + xTo) / 2;
}
推荐阅读
- haskell - 如何处理字符串haskell中以'\'结尾的文件
- unity3d - Unity ARCore XR Plugin包初始化未完成添加包后警告,安卓黑屏
- c++ - 执行算术时也需要 std::launder 吗?
- postgresql - psql:/usr/pgsql-11/lib/libpq.so.5:没有可用的版本信息
- c# - SonarCloud 不显示测试结果
- java - 我可以让 Cassandra 触发器在更新时触发,并获取更新的记录吗
- android - 是否可以在 Chrome 自定义选项卡上显示自定义对话框?
- javascript - 类不变量:一个对象需要不为空的,反之亦然。提供什么优雅的方式?
- python - 模块pygame没有成员退出?
- spring-boot - Keycloak - 带有签名 Jwt 的授权请求