Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. |-------|-------|-------|-------|-------|-------|
  2. nov dec jan feb mar apr may
  3.  
  4. |-------|-------|-------|-------|-------|-------|
  5. nov dec jan feb mar apr
  6.  
  7. // scale is your usual x scale. change dates on the domain by 15 days
  8. var d0 = scale.domain()[0];
  9. d0.setDate(d0.getDate() - 15);
  10. var d1 = scale.domain()[1];
  11. d1.setDate(d1.getDate() - 15);
  12.  
  13. // use this new x scale for the axis only
  14. new_scale = d3.time.scale().domain([d0, d1]).range(scale.range());
  15. d3.svg.axis().scale(new_scale);
  16.  
  17. // ticksDistance is constant for a specific x_scale
  18. const getTicksDistance = (scale, ticks) => {
  19. const tcks = scale.ticks(ticks);
  20. const spaces = []
  21. for(let i=0; i < tcks.length - 1; i++){
  22. spaces.push(scale(tcks[i+1]) - scale(tcks[i]))
  23. }
  24. return spaces;
  25. };
  26. const ticksSpacingTime = getTicksDistance(x_scale_time, d3.timeMonth.every(1));
  27.  
  28. svg.append("g")
  29. .attr("class", "x-axis-shifted")
  30. .attr("transform", "translate(0,100)")
  31. .call(x_axis)
  32. .selectAll("text")
  33. .attr("x", (d,i) => ticksSpacingTime[i]/2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement