D3 时间刻度将前后时间范围添加时间间隔的百分比
d3 time scale add before and after time range a percentage of time interval
我正在使用d3的time.scale.domain来渲染一些图形。
var time_range=d3.time.scale().domain([new Date(2008, 1, 1), new Date(2015, 11, 10)]);
如何为其添加 +- 10% 的时间范围?
function add_percentage(start_date, end_date , percentage){
//Add 2015 - 2008 => Around 7 years , if percentage = 10 => around 9-10 months before and after
}
add_percentage(new Date(2008, 1, 1),new Date(2015, 11, 10),10)
//Should return around 9-10 months before and after.
您可以使用下面的代码来执行此操作。我认为最好是研究天数之间的差异,而不仅仅是几个月,因为这会给你带来更高的准确性。但是由于您使用的是百分比,因此我真的认为现阶段的准确性并不那么重要。
function add_percentage(s, e , percentage){
var t2 = e.getTime();
var t1 = s.getTime();
diff= parseInt((t2-t1)/(24*3600*1000))*percentage/100;
d1=new Date(s.getFullYear(),s.getMonth(),s.getDay()-diff);
d2=new Date(e.getFullYear(),e.getMonth(),e.getDay()+diff);
return {start:d1, end:d2}
}
这对你有帮助吗?
相关文章:
- Java脚本时间添加
- Json数据包含日期和时间格式
- 如何根据时间运行不同的脚本
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 在wordpress一定时间后更改自定义字段
- 更改angularjs中的日期-时间格式
- 将当前时间添加到我的页面上的特定部分
- 门户网站:当地时间有多有用
- 显示时间的脚本
- 如何在窗体打开时从javascript倒计时计时器值中节省时间
- 通过JSON&比较时间
- Javascript时间轨迹
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- 将日期时间从json转换为可读格式
- 高库存时期时间与时区不匹配
- Jquery如何检查今天的时间大于使用给定时间
- JavaScript 加权随机或百分比时间
- 如何获取今天的当前时间已用百分比
- D3 时间刻度将前后时间范围添加时间间隔的百分比
- 比较持续时间并返回匹配的百分比