dc.js LineChart按月/年汇总

时间:2017-11-14 20:25:56

标签: javascript d3.js dc.js crossfilter

我试图绘制一个lineChart,我希望按月汇总数据。我正在使用来自crossfilter documentation的数据集,并在日期稍作修改。

这就是我的数据集的样子:

var data = [
  {date: "2011-11-14T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
  {date: "2011-07-10T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"},
  {date: "2011-07-15T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"},
  {date: "2011-06-14T16:30:43Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-06-14T16:48:46Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-05-14T16:53:41Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-07-17T16:14:06Z", quantity: 1, total: 100, tip: 0, type: "cash"},
  {date: "2011-05-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-12-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
  {date: "2011-11-14T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
];

我想按月 - 月分组。我的数据组根据日期而不是月份返回总计。

enter image description here

在我的图表中,7月10日,15日和17日被绘制为不同点。我希望将它们全部合并到2017年7月至2017年。我如何实现这一目标?有什么指针吗? 谢谢大家!

这是一个小提琴: https://jsfiddle.net/usingh2buffaloedu/8u0etnwd/

enter image description here

1 个答案:

答案 0 :(得分:3)

我在dc.js中遇到时间渲染问题,但在您的示例中,关键是获取维度中的月份值。

var dateDimension = facts.dimension(function (d) {return d3.time.month(d.newDate); });

查看更新的小提琴(https://jsfiddle.net/ury1k1bs/1/

我认为.round()图表方法仅适用于刷涂,而不适用于渲染。

有关更多信息,请参阅dc.js提供的时间间隔示例... http://dc-js.github.io/dc.js/examples/switching-time-intervals.html