dc.js LineChart Aggregated by Month/Year

廉价感情. 提交于 2019-12-11 06:48:11

问题


I trying to plot a lineChart and I want to aggregate data by Month. I am using dataset from crossfilter documentation with slight modification in date.

This is how my dataset looks like:

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"}
];

I want to group total by Month-Year. My Data Group is returning total based on date instead of Month.

In my graph, July 10, 15 and 17 are plotted as different point. I want to combine them all into July-2017 points. How do I acheive that ? Any pointers? Thanks All!

here is a fiddle: https://jsfiddle.net/usingh2buffaloedu/8u0etnwd/


回答1:


I've had issues with time rendering in dc.js, but in your example the key is to get the month values in your dimension.

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

See the updated fiddle (https://jsfiddle.net/ury1k1bs/1/)

I think the .round() chart method is only useful for brushing, not for rendering.

For more see the time interval example provided by dc.js... http://dc-js.github.io/dc.js/examples/switching-time-intervals.html



来源:https://stackoverflow.com/questions/47294588/dc-js-linechart-aggregated-by-month-year

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