简体   繁体   English

从时间戳数组创建D3条形图

[英]Create D3 Bar Chart from array of timestamps

I am looking for some insight on best practices/how to get started here. 我正在寻找有关最佳做法/如何开始的见解。 I have a json object with 1000 timestamps from a given day. 我有给定日期的json对象,带有1000个时间戳。 I want to build the x-axis as a 24 hour time frame with a tick for 4 hour periods...12am - 4am, 4am - 8am, etc. I then want to build the y-axis based on volume. 我想将x轴构建为一个24小时的时间框架,并在4个小时内滴答滴答... 12am-4 am、4am-8am等。然后,我想基于体积构建y轴。 So that each bar for the 4-hour periods will be populated based on the number of timestamps in that period. 因此,将根据该时间段中的时间戳数填充4小时时间段中的每个条形。 My json looks like this (except with many more entries): 我的json看起来像这样(除了有更多条目):

[
{"Time":"2017-02-07 16:14:06"},
{"Time":"2017-02-07 16:58:49"},
{"Time":"2017-02-07 17:07:11"},
{"Time":"2017-02-07 18:13:19"},
{"Time":"2017-02-07 13:56:06"},
{"Time":"2017-02-07 19:07:57"},
{"Time":"2017-02-07 12:08:58"},
{"Time":"2017-02-07 01:41:00"},
{"Time":"2017-02-07 11:56:49"},
{"Time":"2017-02-07 02:45:29"}
]

I have been doing a lot of reading on D3, specifically about how to use the built in 'time' method but I am hoping to get some pointers on how to get started. 我已经在D3上做了很多阅读,特别是关于如何使用内置的“时间”方法的内容,但是我希望能找到一些入门的指导。 The end result that I would want would look something like the image attached here (disregard the black bars as those will come from another source). 我想要的最终结果看起来像这里的图像(忽略黑条,因为它们将来自其他来源)。 Any help/pointers are much appreciated. 任何帮助/指针将不胜感激。 在此处输入图片说明

This is not a pure D3 solution but what I would do is parse that JSON object and then create a new one that has a totals count for each time interval and then feed that into the d3 graph. 这不是一个纯粹的D3解决方案,但我要做的是解析该JSON对象,然后创建一个新的,具有每个时间间隔总计数的对象,然后将其输入到d3图中。

Not sure what you have tried yet, but this should get you on the right path. 不确定您尝试过什么,但这应该可以使您走上正确的道路。

https://jsfiddle.net/9f2vp8gp/ https://jsfiddle.net/9f2vp8gp/

var margin = {
    top: 20,
    right: 20,
    bottom: 70,
    left: 40
  },
  width = 600 - margin.left - margin.right,
  height = 300 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%m").parse;

var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);

var y = d3.scale.linear().range([height, 0]);

var xAxis = d3.svg.axis()
  .scale(x)
  .orient("bottom")
  .tickFormat(d3.time.format("%m"));

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 data = [{
  "date": "1",
  "value": 44
}, {
  "date": "2",
  "value": 24
} ,{
  "date": "3",
  "value": 31
},{
  "date": "4",
  "value": 38
},{
  "date": "5",
  "value": 46
},{
  "date": "6",
  "value": 23
}];

data.forEach(function(d) {
  d.date = parseDate(d.date);
  d.value = +d.value;
});

x.domain(data.map(function(d) {
  return d.date;
}));
y.domain([0, d3.max(data, function(d) {
  return d.value;
})]);

svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis)
  .selectAll("text")
  .style("text-anchor", "end")
  .attr("dx", "-.8em")
  .attr("dy", "-.55em")
  .attr("transform", "rotate(-90)");

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("Value ($)");

svg.selectAll("bar")
  .data(data)
  .enter().append("rect")
  .style("fill", "steelblue")
  .attr("x", function(d) {
    return x(d.date);
  })
  .attr("width", x.rangeBand())
  .attr("y", function(d) {
    return y(d.value);
  })
  .attr("height", function(d) {
    return height - y(d.value);
  });

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

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