How to filter precisely on date?

懵懂的女人 提交于 2019-12-02 02:26:41
Gordon

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.

dc.js - add drop down to select date range

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.

I added a feature to be able to select the current day, current week or current month:

  d3.select('#date_select').on('change', function(){ 
    var nd = new Date(), now = new Date();
    switch (this.value) {
      case "today":
        nd = d3.time.day(now);
        break;
      case "week":
        nd = d3.time.monday(now);
        break;
      case "month":
        nd = d3.time.month(now);
        break;
      default:
        nd.setDate(nd.getDate() - +this.value);
    }
    dim.filterAll();
    dim.filterRange([nd, now]);
    //did not work graph.replaceFilter(dc.RangedFilter(nd, now));
    graph.redrawGroup();
  });

and the html being:

<select id="date_select">
  <option value="Infinity">All</option>
  <option value="today">Today</option>
  <option value='1'>last 24 hours</option>
  <option value="week">This week</option>
  <option value="month">This month</option>
  <option value='30'>last 30 days</option>
  <option value='90'>last 90 days</option>
</select>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!