D3 histogram, number of occurrences of date from time stamp
Newb here. I have a csv with this data:
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.
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.
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.
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>
链接地址: http://www.djcxy.com/p/84362.html
上一篇: 使用JWT实现的最小WebAPI2 + OAuth:总是返回401
下一篇: D3直方图,来自时间戳的日期出现次数