如何精确过滤日期? [英] How to filter precisely on date?
问题描述
我正在使用lineChart显示创建日期(数据中的一个字段),它可以正常工作并且可以选择,但是我需要一个更精确的过滤器:仅适用于今天,本周,上周,上个月的一个月
I'm using a lineChart to show created date (one field in my data), it works fine and I can select, but I need a more precise filter: only for items created today, this week, last week, this month, last month
是否已经有一个示例?我正在努力应用日期范围过滤器
Is there already an example of how it's done? I'm struggling on how to apply a daterange filter
推荐答案
因为这个问题有时会出现,所以很高兴有一个不错的例子,我从前回想起NorthSide的问题并修正了小提琴。
Since this question comes up now and then and it would be nice to have a decent example, I went back to NorthSide's question from a while back and fixed up the fiddle.
我们将定义第二时间维度,以使图表受其影响。
We'll define a second time dimension so that the charts are affected by it.
在此示例中,我们将天数设置为选项值,然后使用该天数来计算日期:
In this example we set the number of days as the option value, and then use that to calculate the date:
<select id="myDropDown">
<option value="Infinity">All</option>
<option value='30'>30 days</option>
<option value='7'>7 days</option>
<option value="2">Top 2 day</option>
<option value="5">Top 5 day</option>
</select>
d3.select('#myDropDown').on('change', function(){
var nd = new Date(now);
nd.setDate(nd.getDate() - +this.value);
filterDimension.filterRange([nd, now]);
dc.redrawAll();
});
http://jsfiddle.net/gordonwoodhull/400wd2nd/16/
还有更智能的方法来计算一个月等等,但希望这足以让您入门。
There are more intelligent ways to calculate a month back and so on, but hopefully this is enough to get you started.
这篇关于如何精确过滤日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!