numeric-scale.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. jvm.NumericScale = function(scale, normalizeFunction, minValue, maxValue) {
  2. this.scale = [];
  3. normalizeFunction = normalizeFunction || 'linear';
  4. if (scale) this.setScale(scale);
  5. if (normalizeFunction) this.setNormalizeFunction(normalizeFunction);
  6. if (minValue) this.setMin(minValue);
  7. if (maxValue) this.setMax(maxValue);
  8. };
  9. jvm.NumericScale.prototype = {
  10. setMin: function(min) {
  11. this.clearMinValue = min;
  12. if (typeof this.normalize === 'function') {
  13. this.minValue = this.normalize(min);
  14. } else {
  15. this.minValue = min;
  16. }
  17. },
  18. setMax: function(max) {
  19. this.clearMaxValue = max;
  20. if (typeof this.normalize === 'function') {
  21. this.maxValue = this.normalize(max);
  22. } else {
  23. this.maxValue = max;
  24. }
  25. },
  26. setScale: function(scale) {
  27. var i;
  28. for (i = 0; i < scale.length; i++) {
  29. this.scale[i] = [scale[i]];
  30. }
  31. },
  32. setNormalizeFunction: function(f) {
  33. if (f === 'polynomial') {
  34. this.normalize = function(value) {
  35. return Math.pow(value, 0.2);
  36. }
  37. } else if (f === 'linear') {
  38. delete this.normalize;
  39. } else {
  40. this.normalize = f;
  41. }
  42. this.setMin(this.clearMinValue);
  43. this.setMax(this.clearMaxValue);
  44. },
  45. getValue: function(value) {
  46. var lengthes = [],
  47. fullLength = 0,
  48. l,
  49. i = 0,
  50. c;
  51. if (typeof this.normalize === 'function') {
  52. value = this.normalize(value);
  53. }
  54. for (i = 0; i < this.scale.length-1; i++) {
  55. l = this.vectorLength(this.vectorSubtract(this.scale[i+1], this.scale[i]));
  56. lengthes.push(l);
  57. fullLength += l;
  58. }
  59. c = (this.maxValue - this.minValue) / fullLength;
  60. for (i=0; i<lengthes.length; i++) {
  61. lengthes[i] *= c;
  62. }
  63. i = 0;
  64. value -= this.minValue;
  65. while (value - lengthes[i] >= 0) {
  66. value -= lengthes[i];
  67. i++;
  68. }
  69. if (i == this.scale.length - 1) {
  70. value = this.vectorToNum(this.scale[i])
  71. } else {
  72. value = (
  73. this.vectorToNum(
  74. this.vectorAdd(this.scale[i],
  75. this.vectorMult(
  76. this.vectorSubtract(this.scale[i+1], this.scale[i]),
  77. (value) / (lengthes[i])
  78. )
  79. )
  80. )
  81. );
  82. }
  83. return value;
  84. },
  85. vectorToNum: function(vector) {
  86. var num = 0,
  87. i;
  88. for (i = 0; i < vector.length; i++) {
  89. num += Math.round(vector[i])*Math.pow(256, vector.length-i-1);
  90. }
  91. return num;
  92. },
  93. vectorSubtract: function(vector1, vector2) {
  94. var vector = [],
  95. i;
  96. for (i = 0; i < vector1.length; i++) {
  97. vector[i] = vector1[i] - vector2[i];
  98. }
  99. return vector;
  100. },
  101. vectorAdd: function(vector1, vector2) {
  102. var vector = [],
  103. i;
  104. for (i = 0; i < vector1.length; i++) {
  105. vector[i] = vector1[i] + vector2[i];
  106. }
  107. return vector;
  108. },
  109. vectorMult: function(vector, num) {
  110. var result = [],
  111. i;
  112. for (i = 0; i < vector.length; i++) {
  113. result[i] = vector[i] * num;
  114. }
  115. return result;
  116. },
  117. vectorLength: function(vector) {
  118. var result = 0,
  119. i;
  120. for (i = 0; i < vector.length; i++) {
  121. result += vector[i] * vector[i];
  122. }
  123. return Math.sqrt(result);
  124. }
  125. };