纽布在这里。我有这个数据的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
我想要一个直方图,其中x轴是按日期而y轴是按出现次数。
因此1月1日的列高为3,而1月2的列高为1.时间与时间无关。
我是否需要解析日期,或以某种方式设置time interval“天”或创建数组?似乎有两个步骤:将数据“过滤”为块,并计算块,但我不知道该怎么做。
我找到this example,但日期已经很好地“舍入”到日期,数据在文件中,而不是外部。
感谢您的帮助!
答案 0 :(得分:4)
您需要做的就是遍历数据并记录频率。我修改了此bar chart example以使用您的数据。您需要使用D3提供的Time Formatting方法。
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"
完整示例:
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>
&#13;
<强> JSFiddle Demo 强>
从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);
});