Group log file data by time interval in d3

2019-07-17 07:36发布

I have some log file data that I would like to graph. For one log file, every time an event happens a line is added to the log file. I can get it so that the data looks like this:

data = [
    {
        "click": true,
        "date": "2013-06-23T14:37:27.000Z"
    },
    {
        "click": true,
        "date": "2013-06-23T14:36:02.000Z"
    },
    ...
    ...
    ...
]

Now, what I would like to do is view this data on a time line, so I can see how many clicks happen over time. However, I think I need to group this data by intervals (1 minute or 15 minute, day...) so that I can see how many people are clicking over a period of time. Then graph the interval data.

Is there a way to group data by time interval? Does d3 have a way to do this? I am also using rickshaw and coffeescript.

1条回答
孤傲高冷的网名
2楼-- · 2019-07-17 08:21

As suggested by Adam Pearce, you can get all information here: http://bl.ocks.org/mbostock/3048166

The first step is to define x:

var x = d3.scale.linear()
    .domain([0, 120])
    .range([0, width]);

Then, you just define the number of ticks you want:

// Generate a histogram using twenty uniformly-spaced bins.
var data = d3.layout.histogram()
    .bins(x.ticks(20))
    (values);

With values being defined like this:

// Generate a log-normal distribution with a median of 30 minutes.
var values = d3.range(1000).map(d3.random.logNormal(Math.log(30), .4));

So, the secret is behind the d3.layout.histogram().bins and the axis.ticks calls.

查看更多
登录 后发表回答