gradient.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const createGradient = (scales, options) => {
  2. let x1;
  3. let x2;
  4. let y1;
  5. let y2;
  6. let m;
  7. let group;
  8. let graphGroup;
  9. let textGroup;
  10. let leftCircle;
  11. let rightCircle;
  12. let gradientLine;
  13. let textBoxOffset;
  14. let state = 'inactive';
  15. const gradient = (selection) => {
  16. group = selection.append('g');
  17. graphGroup = group
  18. .append('g')
  19. .attr('clip-path', 'url(#mask)')
  20. .attr('class', 'mod2-gradientangle__connector'); // .attr('class', 'mod2-gradientangle__graph')
  21. leftCircle = graphGroup.append('circle')
  22. .attr('class', 'mod2-gradientangle--circle mod2-gradientangle--circle--left')
  23. .attr('r', options.radius)
  24. .attr('fill', options.colors.light);
  25. rightCircle = graphGroup.append('circle')
  26. .attr('class', 'mod2-gradientangle--circle mod2-gradientangle--circle--right')
  27. .attr('r', options.radius)
  28. .attr('fill', options.colors.light);
  29. gradientLine = graphGroup.append('line')
  30. .attr('class', 'mod2-gradientangle--line')
  31. .attr('stroke', options.colors.light)
  32. .attr('stroke-dasharray', '15 3 2 3')
  33. .attr('stroke-width', 3);
  34. textGroup = group
  35. .append('g')
  36. .attr('class', 'mod2-gradientangle__box');
  37. textGroup.append('rect')
  38. .attr('class', 'mod2-gradientangle__rect')
  39. .attr('y', 12)
  40. .attr('stroke', options.colors.light)
  41. .attr('stroke-width', 2)
  42. .attr('width', options.box.width)
  43. .attr('height', options.box.height)
  44. .attr('fill', '#ffffff');
  45. textGroup.append('text')
  46. .attr('class', 'mod2-gradientangle__text')
  47. .attr('y', options.margin.top * 1.5)
  48. .attr('text-anchor', 'middle')
  49. .attr('fill', options.colors.light)
  50. .style('font-weight', 500)
  51. .attr('transform', `translate(${options.box.width / 2}, 0)`);
  52. gradient.update(selection);
  53. };
  54. gradient.update = () => {
  55. gradientLine
  56. .attr('x1', x1)
  57. .attr('y1', y1)
  58. .attr('x2', x2)
  59. .attr('y2', y2);
  60. leftCircle
  61. .attr('cx', x1)
  62. .attr('cy', y1);
  63. rightCircle
  64. .attr('cx', x2)
  65. .attr('cy', y2);
  66. textBoxOffset = (x2 - x1) / 2 - options.box.width / 2 + x1;
  67. textGroup
  68. .attr('transform', `translate(${textBoxOffset}, ${options.box.height / 1.5})`);
  69. textGroup.select('text')
  70. .text(m);
  71. group
  72. .attr('class', `mod2-gradientangle mod2-gradientangle--${state}`);
  73. };
  74. gradient.x1 = (val) => {
  75. if (val === undefined) {
  76. return x1;
  77. }
  78. x1 = val;
  79. return gradient;
  80. };
  81. gradient.x2 = (val) => {
  82. if (val === undefined) {
  83. return x2;
  84. }
  85. x2 = val;
  86. return gradient;
  87. };
  88. gradient.y1 = (val) => {
  89. if (val === undefined) {
  90. return y1;
  91. }
  92. y1 = val;
  93. return gradient;
  94. };
  95. gradient.y2 = (val) => {
  96. if (val === undefined) {
  97. return y2;
  98. }
  99. y2 = val;
  100. return gradient;
  101. };
  102. gradient.m = (val) => {
  103. if (val === undefined) {
  104. return m;
  105. }
  106. m = val;
  107. return gradient;
  108. };
  109. gradient.state = (val) => {
  110. if (val === undefined) {
  111. return state;
  112. }
  113. state = val;
  114. return gradient;
  115. };
  116. return gradient;
  117. };
  118. export default createGradient;