progressBar.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { round, typeset } from './utilities';
  2. export function createProgressBar(options) {
  3. // dimensions
  4. let width, height;
  5. let x, y;
  6. // common options
  7. let colours;
  8. let lang; // needed for number formatting
  9. let controller;
  10. let max;
  11. // svg element
  12. let progressGroup;
  13. function progressBar(selection) {
  14. progressGroup = selection.append('g')
  15. .attr('class', 'progress');
  16. progressGroup.append('rect')
  17. .attr('class', 'pbbg')
  18. .attr('fill', colours.background);
  19. progressGroup.append('rect')
  20. .attr('class', 'pbindirect')
  21. .attr('opacity', 0.8)
  22. .attr('fill', colours.text);
  23. let indi = progressGroup.append('g')
  24. .attr('class', 'indicator');
  25. indi.append('path')
  26. .attr('stroke', colours.text);
  27. indi.append('text')
  28. .attr('x', (-width * 0.125))
  29. .attr('fill', colours.text)
  30. .attr('text-anchor', 'end');
  31. progressGroup.attr('transform', 'translate(' + x + ',' + y + ')');
  32. progressGroup.select('rect.pbbg')
  33. .attr('height', height)
  34. .attr('width', width);
  35. progressGroup.select('rect.pbindirect')
  36. .attr('width', width);
  37. progressGroup.select('.indicator path')
  38. .attr('d', 'M ' + (-width * 0.125) + ' 0 L 0 0'); // line progress indicator
  39. }
  40. progressBar.draw = function(cProgress) {
  41. let indi = progressGroup.select('.indicator');
  42. indi.attr('transform', 'translate(0,' + height * cProgress + ')');
  43. progressGroup.select('.pbindirect')
  44. .attr('height', height * cProgress);
  45. indi.select('text')
  46. .text(typeset(round(max * cProgress), lang));
  47. };
  48. progressBar.lang = function(val) {
  49. if(!arguments.length) {
  50. return lang;
  51. }
  52. lang = val;
  53. return progressBar;
  54. };
  55. progressBar.max = function(val) {
  56. if(!arguments.length) {
  57. return max;
  58. }
  59. max = val;
  60. return progressBar;
  61. };
  62. progressBar.controller = function(val) {
  63. if(!arguments.length) {
  64. return controller;
  65. }
  66. controller = val;
  67. return progressBar;
  68. };
  69. progressBar.width = function(val) {
  70. if(!arguments.length) {
  71. return width;
  72. }
  73. width = val;
  74. return progressBar;
  75. };
  76. progressBar.height = function(val) {
  77. if(!arguments.length) {
  78. return height;
  79. }
  80. height = val;
  81. return progressBar;
  82. };
  83. progressBar.x = function(val) {
  84. if(!arguments.length) {
  85. return x;
  86. }
  87. x = val;
  88. return progressBar;
  89. };
  90. progressBar.y = function(val) {
  91. if(!arguments.length) {
  92. return y;
  93. }
  94. y = val;
  95. return progressBar;
  96. };
  97. progressBar.colours = function(val) {
  98. if(!arguments.length) {
  99. return colours;
  100. }
  101. colours = val;
  102. return progressBar;
  103. };
  104. progressBar.controller(options.common.controller);
  105. progressBar.lang(options.common.lang);
  106. progressBar.width(options.width);
  107. progressBar.height(options.height);
  108. progressBar.x(options.x);
  109. progressBar.y(options.y);
  110. progressBar.colours(options.common.colours);
  111. progressBar.max(options.max);
  112. return progressBar;
  113. }