axes.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import * as d3 from 'd3';
  2. // d3js module: add axes
  3. // call it: addAxes(group, { xScale, yScale }, options);
  4. export default (group, scales, options) => {
  5. // add stroke styles
  6. const addStyles = axis => {
  7. axis.selectAll('path')
  8. .attr('stroke-width', '2')
  9. .attr('stroke', options.colors.axis);
  10. axis.selectAll('line')
  11. .attr('stroke', options.colors.axis);
  12. };
  13. // rotate labels to make them readable
  14. const rotateLabels = axis => {
  15. axis.selectAll('text')
  16. .style('text-anchor', 'end')
  17. .style('font', '11px sans-serif')
  18. .attr('dx', '-.8em')
  19. .attr('dy', '.15em')
  20. .attr('fill', options.colors.units)
  21. .attr('transform', 'rotate(-48)');
  22. };
  23. // size labels
  24. const sizeLabels = axis => {
  25. axis.selectAll('text')
  26. .attr('fill', options.colors.units)
  27. .style('font', '11px sans-serif');
  28. };
  29. // append x axis unit
  30. const addUnitX = axis => {
  31. axis.append('text')
  32. .attr('transform', `translate(${options.widthWithoutMargin / 2}, ${options.margin.bottom / 2 + 5})`)
  33. .attr('fill', options.colors.units)
  34. .style('font', '16px sans-serif')
  35. .style('text-anchor', 'middle')
  36. .text(options.xAxisUnit);
  37. };
  38. // append y axis unit
  39. const addUnitY = axis => {
  40. axis.append('text')
  41. .attr('transform', 'rotate(-90)')
  42. .attr('x', 0 - options.heightWithoutMargin / 2)
  43. .attr('y', 0 - options.margin.left)
  44. .attr('dy', '1em')
  45. .attr('fill', options.colors.units)
  46. .style('font', '16px sans-serif')
  47. .style('text-anchor', 'middle')
  48. .text(options.yAxisUnit);
  49. };
  50. // add the X Axis
  51. let addClass = `x-axis mod${options.module}__x-axis`;
  52. // module02, grap part
  53. if (Object.prototype.hasOwnProperty.call(options, 'currentQuestion')) {
  54. addClass += ` mod${options.module}__x-axis--${options.currentQuestion + 1}`;
  55. }
  56. // append x axis
  57. const xAxis = group.append('g')
  58. .attr('transform', `translate(0, ${options.heightWithoutMargin})`)
  59. .attr('class', addClass)
  60. .call(d3.axisBottom(scales.xScale));
  61. // add clip path for module 2
  62. if (options.module === 2) xAxis.attr('clip-path', 'url(#polygonmask)');
  63. // depending on number of child notes, rotate labels
  64. if (xAxis.node().childNodes.length > 20) {
  65. rotateLabels(xAxis);
  66. } else {
  67. sizeLabels(xAxis);
  68. }
  69. // call methods
  70. addStyles(xAxis);
  71. addUnitX(xAxis);
  72. // add the Y Axis
  73. const yAxis = group.append('g')
  74. .attr('class', 'y-axis')
  75. .call(d3.axisLeft(scales.yScale));
  76. // call remaining methods
  77. sizeLabels(yAxis);
  78. addStyles(yAxis);
  79. addUnitY(yAxis);
  80. return { xAxis, yAxis };
  81. };