utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. 'use strict';
  2. window.chartColors = {
  3. red: 'rgb(250, 98, 107)',
  4. orange: 'rgb(255, 159, 64)',
  5. yellow: 'rgb(255, 205, 86)',
  6. green: 'rgb(94, 216, 79)',
  7. blue: 'rgb(50, 202, 254)',
  8. purple: 'rgb(159, 120, 255)',
  9. grey: 'rgb(201, 203, 207)'
  10. };
  11. (function(global) {
  12. var Months = [
  13. 'January',
  14. 'February',
  15. 'March',
  16. 'April',
  17. 'May',
  18. 'June',
  19. 'July',
  20. 'August',
  21. 'September',
  22. 'October',
  23. 'November',
  24. 'December'
  25. ];
  26. var COLORS = [
  27. '#4dc9f6',
  28. '#f67019',
  29. '#f53794',
  30. '#537bc4',
  31. '#acc236',
  32. '#166a8f',
  33. '#00a950',
  34. '#58595b',
  35. '#8549ba'
  36. ];
  37. var Samples = global.Samples || (global.Samples = {});
  38. var Color = global.Color;
  39. Samples.utils = {
  40. // Adapted from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
  41. srand: function(seed) {
  42. this._seed = seed;
  43. },
  44. rand: function(min, max) {
  45. var seed = this._seed;
  46. min = min === undefined ? 0 : min;
  47. max = max === undefined ? 1 : max;
  48. this._seed = (seed * 9301 + 49297) % 233280;
  49. return min + (this._seed / 233280) * (max - min);
  50. },
  51. numbers: function(config) {
  52. var cfg = config || {};
  53. var min = cfg.min || 0;
  54. var max = cfg.max || 1;
  55. var from = cfg.from || [];
  56. var count = cfg.count || 8;
  57. var decimals = cfg.decimals || 8;
  58. var continuity = cfg.continuity || 1;
  59. var dfactor = Math.pow(10, decimals) || 0;
  60. var data = [];
  61. var i, value;
  62. for (i = 0; i < count; ++i) {
  63. value = (from[i] || 0) + this.rand(min, max);
  64. if (this.rand() <= continuity) {
  65. data.push(Math.round(dfactor * value) / dfactor);
  66. } else {
  67. data.push(null);
  68. }
  69. }
  70. return data;
  71. },
  72. labels: function(config) {
  73. var cfg = config || {};
  74. var min = cfg.min || 0;
  75. var max = cfg.max || 100;
  76. var count = cfg.count || 8;
  77. var step = (max - min) / count;
  78. var decimals = cfg.decimals || 8;
  79. var dfactor = Math.pow(10, decimals) || 0;
  80. var prefix = cfg.prefix || '';
  81. var values = [];
  82. var i;
  83. for (i = min; i < max; i += step) {
  84. values.push(prefix + Math.round(dfactor * i) / dfactor);
  85. }
  86. return values;
  87. },
  88. months: function(config) {
  89. var cfg = config || {};
  90. var count = cfg.count || 12;
  91. var section = cfg.section;
  92. var values = [];
  93. var i, value;
  94. for (i = 0; i < count; ++i) {
  95. value = Months[Math.ceil(i) % 12];
  96. values.push(value.substring(0, section));
  97. }
  98. return values;
  99. },
  100. color: function(index) {
  101. return COLORS[index % COLORS.length];
  102. },
  103. transparentize: function(color, opacity) {
  104. var alpha = opacity === undefined ? 0.5 : 1 - opacity;
  105. return Color(color).alpha(alpha).rgbString();
  106. }
  107. };
  108. // DEPRECATED
  109. window.randomScalingFactor = function() {
  110. return Math.round(Samples.utils.rand(-100, 100));
  111. };
  112. // INITIALIZATION
  113. Samples.utils.srand(Date.now());
  114. }(this));