Guest User

Untitled

a guest
Jun 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. const byDate = o => o.assessment_date;
  2.  
  3. const data = [
  4. {
  5. assessment_date: "2018-04-19T00:31:03.153000Z",
  6. id: 1
  7. }, {
  8. assessment_date: "2017-11-20T09:51:36.035983Z",
  9. id: 2,
  10. }, {
  11. assessment_date: "2018-02-15T09:51:36.035983Z",
  12. id: 3,
  13. }, {
  14. assessment_date: "2018-02-20T09:51:36.035983Z",
  15. id: 4,
  16. },
  17. {
  18. assessment_date: "2018-03-19T17:48:44.820000Z",
  19. id: 5,
  20. }
  21. ];
  22.  
  23. const sortedData = data.map(o => Object.assign({}, o, {
  24. "assessment_date": new Date(o.assessment_date)
  25. })).sort(byDate);
  26.  
  27. const WIDTH = 600;
  28. const HEIGHT = 30;
  29.  
  30. const xScale = d3.time.scale()
  31. .domain(d3.extent(sortedData.map(byDate)))
  32. .range([0, WIDTH])
  33. .nice();
  34.  
  35. const xAxis = d3.svg.axis()
  36. .scale(xScale)
  37. .orient('bottom');
  38.  
  39. const histogram = d3.layout.histogram()
  40. .value(o => o.assessment_date)
  41. .range(xScale.domain())
  42. .bins(xScale.ticks(10))
  43.  
  44. const bins = histogram(sortedData);
  45.  
  46. const zoom = d3.behavior
  47. .zoom()
  48. .x(xScale)
  49. .scaleExtent([1, 10])
  50. .on("zoom", function() {
  51. axisSelector.call(xAxis);
  52. update(histogram(sortedData));
  53. });
  54.  
  55. const svg = d3.select("#timeline")
  56. .append("svg")
  57. .attr("width", WIDTH)
  58. .attr("height", HEIGHT)
  59. .attr("padding-top", "10px")
  60. .attr("transform", "translate(0," + (HEIGHT) + ")")
  61. .call(zoom);
  62.  
  63. const axisSelector = svg.append('g')
  64. .attr("class", "x axis")
  65. .call(xAxis);
  66.  
  67. function update(data) {
  68. const node = svg.selectAll(".node").data(data);
  69.  
  70. node.enter()
  71. .append("circle")
  72. .attr("class", "node")
  73. .attr("r", 6)
  74. .attr("style", o => !o.length && 'display: none')
  75. // ^ this seems inelegant. why are some bins empty?
  76. .attr("cx", o => xScale(o.x))
  77.  
  78. node.attr("cx", o => xScale(o.x))
  79.  
  80. return node;
  81. }
  82.  
  83. const nodeSelector = update(bins);
Add Comment
Please, Sign In to add comment