dc.js - add drop down to select date range

橙三吉。 提交于 2019-12-11 02:19:14

问题


I'm using dc.js to display some info, I would like to add a drop down to select the date range as - All, last 7 days, last 30 days, last 90 days.

How would I add that to the following?

var dataTable = dc.dataTable("#dc-table-graph");

var data = [
 {date: "2014-08-15T06:30:43Z", amount: 10},
 {date: "2014-08-12T10:30:43Z", amount: 10 },
 {date: "2014-08-1T16:28:54Z", amount: 100 },
 {date: "2014-07-28T18:48:46Z", amount: 100 },
 {date: "2014-07-26T20:53:41Z", amount: 11 },
 {date: "2014-07-16T10:53:41Z", amount: 5 },
 {date: "2014-05-1T22:54:06Z", amount: 10 },
 {date: "2013-11-1T10:30:43Z", amount: 2 },
 ];

var dtgFormat = d3.time.format("%Y-%m-%dT%H:%M:%SZ");

data.forEach(function (d) {
  d.date   = dtgFormat.parse(d.date.substr(0,20));
  d.amount  = +d.amount;
  d.type = d.type;
});


var ndx = crossfilter(data);
var timeDimension = ndx.dimension(
  function (d) {return d.date;});

dataTable.width(960).height(800)
  .dimension(timeDimension)
  .group(function(d) { return "" })
  .size(20)
  .columns([
    function(d) { return d.date; },
    function(d) { return d.amount; },
  ])
  .sortBy(function(d){ return d.date; })
  .order(d3.ascending);

dc.renderAll();

http://jsfiddle.net/northside45/uvxzo785/


回答1:


Add another dimension on time so that it affects the dimension for your data table. (In crossfilter, filtering a dimension will only affect other dimensions, not itself.)

Then use filterRange on that other dimension in response to your dropdown.

https://github.com/square/crossfilter/wiki/API-Reference#wiki-dimension_filterRange



来源:https://stackoverflow.com/questions/25246070/dc-js-add-drop-down-to-select-date-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!