简体   繁体   English

D3直方图,来自时间戳的日期出现次数

[英]D3 histogram, number of occurrences of date from time stamp

Newb here. 纽布在这里。 I have a csv with this data: 我有这个数据的csv:

date, lat, lon
01/01/2015 09:38:14 AM,37.973424,-87.575423
01/01/2015 09:48:27 PM,37.385218,-122.11413
01/01/2015 10:17:34 AM,39.081712,-76.554603
01/02/2015 01:27:17 PM,40.216204,-74.619533

I want to have a histogram where the x axis is by date and the y axis is by number of occurrences. 我想要一个直方图,其中x轴是按日期而y轴是按出现次数。

So Jan 1 would have a column height of 3 and Jan 2 would have a column height of 1. The time of day is irrelevant. 因此,1月1日的列高为3,而1月2的列高为1.时间与时间无关。

Do I need to parse the dates, or set a time interval of "day" somehow, or create an array? 我是否需要解析日期,或者以某种方式设置“日”的时间间隔 ,或者创建一个数组? It seems there are two steps: "filter" the data into chunks, and count the chunks, but I'm not sure how to do it. 似乎有两个步骤:将数据“过滤”成块,并计算块,但我不知道该怎么做。

I found this example , but the dates are already "rounded" nicely to dates, and the data is in the file, not external. 我找到了这个例子 ,但日期已经很好地“舍入”到日期,数据在文件中,而不是外部。

Thanks for any help! 谢谢你的帮助!

All you need to do is iterate through your data and keep a tally of the frequencies. 您需要做的就是遍历数据并记录频率。 I modified this bar chart example to use your data. 我修改了这个条形图示例以使用您的数据。 You need to use the Time Formatting methods D3 provides. 您需要使用D3提供的时间格式方法。

var formatDate = d3.time.format("%m/%d/%Y %I:%M:%S %p");

// line = "01/01/2015 09:38:14 AM,37.973424,-87.575423"

var parts = line.split(',');
var datetime = formatDate.parse(parts[0]);
var date = formatDate(datetime).split(' ')[0]; // "01/01/2015"

Full example: 完整示例:

 var margin = { top: 20, right: 20, bottom: 30, left: 40 }, width = 250 - margin.left - margin.right, height = 200 - margin.top - margin.bottom; var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .ticks(10); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var csv = [ '01/01/2015 09:38:14 AM,37.973424,-87.575423', '01/01/2015 09:48:27 PM,37.385218,-122.11413', '01/01/2015 10:17:34 AM,39.081712,-76.554603', '01/02/2015 01:27:17 PM,40.216204,-74.619533' ]; var formatDate = d3.time.format("%m/%d/%Y %I:%M:%S %p"); var tally = {}; csv.forEach(function(line) { var parts = line.split(','); var datetime = formatDate.parse(parts[0]); var date = formatDate(datetime).split(' ')[0]; tally[date] = (tally[date]||0) + 1; }); var data = []; for (var date in tally) { if (tally.hasOwnProperty(date)) { data.push({ date: date, frequency: tally[date] }); } } x.domain(data.map(function (d) { return d.date; })); y.domain([0, d3.max(data, function (d) { return d.frequency; })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Frequency"); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function (d) { return x(d.date); }) .attr("width", x.rangeBand()) .attr("y", function (d) { return y(d.frequency); }) .attr("height", function (d) { return height - y(d.frequency); }); function type(d) { d.frequency = +d.frequency; return d; } 
 .bar { fill: steelblue; } .bar:hover { fill: brown; } .axis { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .x.axis path { display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 

JSFiddle Demo JSFiddle演示

Loading from CSV file: 从CSV文件加载:

var formatDate = d3.time.format("%m/%d/%Y %I:%M:%S %p");

var tally = {};
var data = [];

d3.csv('./data.csv')
.get(function(error, rows) {

  rows.forEach(function(obj, i) {
    var datetime = formatDate.parse(obj.date);
    var date = formatDate(datetime).split(' ')[0];
    tally[date] = (tally[date]||0) + 1;
  });

  for (var date in tally) {
    if (tally.hasOwnProperty(date)) {
        data.push({
            date: date,
            frequency: tally[date]
        });
    }
  }

  console.log(data);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM