如何从一天和一天创建时间维度? crossfilter

时间:2016-08-24 10:19:51

标签: d3.js time dc.js crossfilter

我使用crossfilter和dc.js,让我们考虑一下这个数据集:

var dataSet = [{Day:"1", Time:"1230", Score:"30"}, // Monday at 12:30
               {Day:"1", Time:"0830", Score:"20"},// Monday at 08:30
               {Day:"1", Time:"0900", Score:"35"},
               {Day:"1", Time:"2330", Score:"40"},
               {Day:"1", Time:"0450", Score:"15"},
               {Day:"1", Time:"1830", Score:"35"},
               {Day:"2", Time:"0630", Score:"30"},// Tuesday at 06:30
               {Day:"2", Time:"0230", Score:"40"},//Tuesday at 02:30
               {Day:"2", Time:"1910", Score:"35"},
               {Day:"2", Time:"2130", Score:"35"},
               {Day:"2", Time:"2000", Score:"20"},
               {Day:"2", Time:"0725", Score:"15"},
               {Day:"3", Time:"2210", Score:"30"}, // Wenesday at 22:10
               {Day:"3", Time:"1450", Score:"40"},// Wenesday at 14:50
               {Day:"3", Time:"0515", Score:"45"},
               {Day:"3", Time:"0930", Score:"30"},
               {Day:"3", Time:"1100", Score:"40"},
               {Day:"3", Time:"1230", Score:"35"},
               {Day:"4", Time:"1810", Score:"30"}, // Thursday at 18:10
               {Day:"4", Time:"1050", Score:"40"},// Thursday at 10:50
               {Day:"4", Time:"0515", Score:"45"},
               {Day:"4", Time:"0930", Score:"30"},
               {Day:"4", Time:"1100", Score:"40"},
               {Day:"4", Time:"1230", Score:"35"},
               {Day:"5", Time:"2210", Score:"30"}, // Friday at 22:10
               {Day:"5", Time:"1450", Score:"40"},// Friday at 14:50
               {Day:"5", Time:"0515", Score:"40"},
               {Day:"5", Time:"0930", Score:"45"},
               {Day:"5", Time:"1100", Score:"40"},
               {Day:"5", Time:"1230", Score:"35"},
               {Day:"6", Time:"2100", Score:"30"}, // Saturday at 21:00
               {Day:"6", Time:"0550", Score:"40"},// Saturday at 05:50
               {Day:"6", Time:"0515", Score:"45"},
               {Day:"6", Time:"0930", Score:"30"},
               {Day:"6", Time:"1100", Score:"40"},
               {Day:"6", Time:"1230", Score:"35"},
               {Day:"7", Time:"2210", Score:"40"}, // Sunday at 22:10
               {Day:"7", Time:"1150", Score:"35"},// Sunday at 11:50
               {Day:"7", Time:"0515", Score:"45"},
               {Day:"7", Time:"0930", Score:"30"},
               {Day:"7", Time:"1100", Score:"40"},
               {Day:"7", Time:"1230", Score:"20"}

];

如您所见,我有一个典型的一周,从 1 7 星期一星期日 )。

我想制作一个barChart,我可以在白天和时间看到分数。

X轴:工作日从星期一到星期日,每天都在零碎          从00到24的24小时 Y轴:得分值

为此我必须创建一个时间维度,我尝试了这个但没有很好的结果:

var timeFormatweek = d3.time.format("%w%H%M");
d.timeweek = timeFormatweek.parse(d.Day+Time);

但即使我的d.Day改变,这个回归总是在星期一。 而且对于图表的规模而言,现在还不知道如何继续。

1 个答案:

答案 0 :(得分:2)

尝试将数据格式化为四舍五入以添加新字段,例如' Monday-12:00'并且在星期一到此时将所有低于这个时间的值(即从星期一00点到12点)加起来。通过这种方式,您将得到一个结果数据集     

[{Date:"Monday-12pm", Score:"200"},// sum of all monday scores till that time
 {Date:"Tuesday-12am", Score:"300"},// sum of all monday scores till that time
 ] 
现在在这两个键上创建尺寸并使用dc.js绘制图表。您还可以在维度的构造函数方法中进行上述计算。

制作整周工作日小时值的图表看起来不太好,因为它不适合屏幕。 另一种方法可以是按小时分辨率,6小时分辨率,12小时分辨率和每日分辨率制作多个尺寸。首先在图表上显示每日分辨率,并允许用户放大每小时分辨率。比较每个缩放事件的所选范围的差异并相应地更改分辨率。欢呼声。