使用d3.js和dc.js将记录拆分和分组为每日集 [英] Splitting and grouping records into daily sets using d3.js and dc.js
问题描述
我是 d3.js 和 dc.js ,我花了一周的最好的时间阅读通过教程和 API 。它有一个相对陡峭的学习曲线,但我(慢慢)熟悉个人操纵。这说我仍然缺乏构建我需要的实践经验。
I am new to d3.js and dc.js and I have spend the best part of a week reading through the tutorials and API. It has a relatively steep learning curve however I am (slowly) becoming familiar with the individual manipulations. That said I still lack the practical experience to construct what I need.
我有一个JSON文件,包含以下数据结构(记录集相对较大〜2百万对象):
I have a JSON file that contains the following data structure (The record set is relatively large ~2 million objects):
[
{
"index": "device_1",
"state": -1,
"frequencies": [
"800PS"
],
"events": [
{
"start": "04/07/2014 04:24:19",
"end": "07/21/2014 08:53:19",
"name": "event_1234"
}
]
},
{
"index": "device_2",
"state": 1,
"frequencies": [
"2100AWS",
"1900PCS"
],
"events": [
{
"start": "02/20/2014 04:03:20",
"end": "04/30/2014 07:24:35",
"name": "event_3456"
},
{
"start": "04/30/2014 07:25:37",
"end": "07/01/2014 06:35:44",
"name": "event_766"
},
{
"start": "06/02/2014 00:02:16",
"end": "06/02/2014 00:04:25",
"name": "event_8967"
},
{
"start": "06/11/2014 15:38:59",
"end": "06/11/2014 15:41:15",
"name": "event_385"
},
{
"start": "06/28/2014 07:37:00",
"end": "06/28/2014 07:39:34",
"name": "event_8959"
},
{
"start": "07/01/2014 07:06:06",
"end": "07/03/2014 03:27:55",
"name": "event_2654"
},
{
"start": "07/03/2014 04:16:55",
"end": "07/21/2014 08:53:19",
"name": "event_94768"
}
]
},
...
]
我想要实现的是组织数据,以便我可以创建每日正常运行时间报告每个设备,其中我收集每个设备每天的累积事件时间。
What I am trying to achieve is to organise the data so I can create a daily uptime report per device where I gather a cumulative event time per day per device.
有效地,我试图将原始数据(上面)转换为一个新的数据集,看起来像这样:
Effectively I am trying to convert the original data (above) into a new dataset that looks something like this:
[
{"device":"device_1", "date": "01/01/2014", "cumulative": 2530},
{"device":"device_2", "date": "01/01/2014", "cumulative": 1234},
{"device":"device_1", "date": "01/02/2014", "cumulative": 456},
{"device":"device_2", "date": "01/02/2014", "cumulative": 198},
...
]
* 其中 * cumulative *
一旦我进入那个阶段,我可以使用类似: d3.nest()。key()。rollup()。entries()
来对数据进行排序和分组,以便显示。
Once I get to that stage I can use something like: d3.nest().key().rollup().entries()
to sort and group the data ready for display.
I怀疑d3有一个内置的机制来处理这种情况,但我目前的方法如下:
I suspect that d3 has a built in mechanism to handle this situation but my current approach is as follows:
-
导入数据集
Import the data set
d3.json("data.json", function(error, json_data) {
if (error)return console.warn(error);
...
}
将字符串转换为日期对象
Convert the Strings to date objects
var dateFormat = d3.time.format("%m/%d/%Y %H:%M:%S");
json_data.forEach(function(d) {
d.dstart = d.events.map(function(x) {
return dateFormat.parse(x.start);
});
d.dend = d.events.map(function(x) {
return dateFormat.parse(x.end);
});
});
指定开始
(N.B. I do have control over the JSON data format! I could technically create the final dataset directly. However, the current format is very useful for other reports and I am keen to avoid having two data files as they are <20MB each so ideally I need to avoid changing the JSON design.)
推荐答案
想到的数据结构是间隔树。我没有尝试过这个库,但可能会有帮助 - 间隔树。
The data structure that comes to mind is an interval tree. I haven't tried this library but it might help - interval tree.
否则,至少你可以跳过最后一步,只是按天分割事件。积累是什么crossfilter是伟大的 - 使用 reduceSum
。
Otherwise, at least you could skip the last step and just break events by day. Accumulation is what crossfilter is great at - use reduceSum
.
这篇关于使用d3.js和dc.js将记录拆分和分组为每日集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!