vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
6.9 KiB

  1. /**
  2. * Created by ludo on 25-1-16.
  3. */
  4. function DataScale(start, end, autoScaleStart, autoScaleEnd, containerHeight, majorCharHeight, zeroAlign = false, formattingFunction=false) {
  5. this.majorSteps = [1, 2, 5, 10];
  6. this.minorSteps = [0.25, 0.5, 1, 2];
  7. this.customLines = null;
  8. this.containerHeight = containerHeight;
  9. this.majorCharHeight = majorCharHeight;
  10. this._start = start;
  11. this._end = end;
  12. this.scale = 1;
  13. this.minorStepIdx = -1;
  14. this.magnitudefactor = 1;
  15. this.determineScale();
  16. this.zeroAlign = zeroAlign;
  17. this.autoScaleStart = autoScaleStart;
  18. this.autoScaleEnd = autoScaleEnd;
  19. this.formattingFunction = formattingFunction;
  20. if (autoScaleStart || autoScaleEnd) {
  21. var me = this;
  22. var roundToMinor = function (value) {
  23. var rounded = value - (value % (me.magnitudefactor * me.minorSteps[me.minorStepIdx]));
  24. if (value % (me.magnitudefactor * me.minorSteps[me.minorStepIdx]) > 0.5 * (me.magnitudefactor * me.minorSteps[me.minorStepIdx])) {
  25. return rounded + (me.magnitudefactor * me.minorSteps[me.minorStepIdx]);
  26. }
  27. else {
  28. return rounded;
  29. }
  30. };
  31. if (autoScaleStart) {
  32. this._start -= this.magnitudefactor * 2 * this.minorSteps[this.minorStepIdx];
  33. this._start = roundToMinor(this._start);
  34. }
  35. if (autoScaleEnd) {
  36. this._end += this.magnitudefactor * this.minorSteps[this.minorStepIdx];
  37. this._end = roundToMinor(this._end);
  38. }
  39. this.determineScale();
  40. }
  41. }
  42. DataScale.prototype.setCharHeight = function (majorCharHeight) {
  43. this.majorCharHeight = majorCharHeight;
  44. };
  45. DataScale.prototype.setHeight = function (containerHeight) {
  46. this.containerHeight = containerHeight;
  47. };
  48. DataScale.prototype.determineScale = function () {
  49. var range = this._end - this._start;
  50. this.scale = this.containerHeight / range;
  51. var minimumStepValue = this.majorCharHeight / this.scale;
  52. var orderOfMagnitude = Math.round(Math.log(range) / Math.LN10);
  53. this.minorStepIdx = -1;
  54. this.magnitudefactor = Math.pow(10, orderOfMagnitude);
  55. var start = 0;
  56. if (orderOfMagnitude < 0) {
  57. start = orderOfMagnitude;
  58. }
  59. var solutionFound = false;
  60. for (var l = start; Math.abs(l) <= Math.abs(orderOfMagnitude); l++) {
  61. this.magnitudefactor = Math.pow(10, l);
  62. for (var j = 0; j < this.minorSteps.length; j++) {
  63. var stepSize = this.magnitudefactor * this.minorSteps[j];
  64. if (stepSize >= minimumStepValue) {
  65. solutionFound = true;
  66. this.minorStepIdx = j;
  67. break;
  68. }
  69. }
  70. if (solutionFound === true) {
  71. break;
  72. }
  73. }
  74. };
  75. DataScale.prototype.is_major = function (value) {
  76. return (value % (this.magnitudefactor * this.majorSteps[this.minorStepIdx]) === 0);
  77. };
  78. DataScale.prototype.getStep = function(){
  79. return this.magnitudefactor * this.minorSteps[this.minorStepIdx];
  80. };
  81. DataScale.prototype.getFirstMajor = function(){
  82. var majorStep = this.magnitudefactor * this.majorSteps[this.minorStepIdx];
  83. return this.convertValue(this._start + ((majorStep - (this._start % majorStep)) % majorStep));
  84. };
  85. DataScale.prototype.formatValue = function(current) {
  86. var returnValue = current.toPrecision(5);
  87. if (typeof this.formattingFunction === 'function') {
  88. returnValue = this.formattingFunction(current);
  89. }
  90. if (typeof returnValue === 'number') {
  91. return '' + returnValue;
  92. }
  93. else if (typeof returnValue === 'string') {
  94. return returnValue;
  95. }
  96. else {
  97. return current.toPrecision(5);
  98. }
  99. };
  100. DataScale.prototype.getLines = function () {
  101. var lines = [];
  102. var step = this.getStep();
  103. var bottomOffset = (step - (this._start % step)) % step;
  104. for (var i = (this._start + bottomOffset); this._end-i > 0.00001; i += step) {
  105. if (i != this._start) { //Skip the bottom line
  106. lines.push({major: this.is_major(i), y: this.convertValue(i), val: this.formatValue(i)});
  107. }
  108. }
  109. return lines;
  110. };
  111. DataScale.prototype.followScale = function (other) {
  112. var oldStepIdx = this.minorStepIdx;
  113. var oldStart = this._start;
  114. var oldEnd = this._end;
  115. var me = this;
  116. var increaseMagnitude = function () {
  117. me.magnitudefactor *= 2;
  118. };
  119. var decreaseMagnitude = function () {
  120. me.magnitudefactor /= 2;
  121. };
  122. if ((other.minorStepIdx <= 1 && this.minorStepIdx <= 1) || (other.minorStepIdx > 1 && this.minorStepIdx > 1)) {
  123. //easy, no need to change stepIdx nor multiplication factor
  124. } else if (other.minorStepIdx < this.minorStepIdx) {
  125. //I'm 5, they are 4 per major.
  126. this.minorStepIdx = 1;
  127. if (oldStepIdx == 2) {
  128. increaseMagnitude();
  129. } else {
  130. increaseMagnitude();
  131. increaseMagnitude();
  132. }
  133. } else {
  134. //I'm 4, they are 5 per major
  135. this.minorStepIdx = 2;
  136. if (oldStepIdx == 1) {
  137. decreaseMagnitude();
  138. } else {
  139. decreaseMagnitude();
  140. decreaseMagnitude();
  141. }
  142. }
  143. //Get masters stats:
  144. var lines = other.getLines();
  145. var otherZero = other.convertValue(0);
  146. var otherStep = other.getStep() * other.scale;
  147. var done = false;
  148. var count = 0;
  149. //Loop until magnitude is correct for given constrains.
  150. while (!done && count++ <5) {
  151. //Get my stats:
  152. this.scale = otherStep / (this.minorSteps[this.minorStepIdx] * this.magnitudefactor);
  153. var newRange = this.containerHeight / this.scale;
  154. //For the case the magnitudefactor has changed:
  155. this._start = oldStart;
  156. this._end = this._start + newRange;
  157. var myOriginalZero = this._end * this.scale;
  158. var majorStep = this.magnitudefactor * this.majorSteps[this.minorStepIdx];
  159. var majorOffset = this.getFirstMajor() - other.getFirstMajor();
  160. if (this.zeroAlign) {
  161. var zeroOffset = otherZero - myOriginalZero;
  162. this._end += (zeroOffset / this.scale);
  163. this._start = this._end - newRange;
  164. } else {
  165. if (!this.autoScaleStart) {
  166. this._start += majorStep - (majorOffset / this.scale);
  167. this._end = this._start + newRange;
  168. } else {
  169. this._start -= majorOffset / this.scale;
  170. this._end = this._start + newRange;
  171. }
  172. }
  173. if (!this.autoScaleEnd && this._end > oldEnd+0.00001) {
  174. //Need to decrease magnitude to prevent scale overshoot! (end)
  175. decreaseMagnitude();
  176. done = false;
  177. continue;
  178. }
  179. if (!this.autoScaleStart && this._start < oldStart-0.00001) {
  180. if (this.zeroAlign && oldStart >= 0) {
  181. console.warn("Can't adhere to given 'min' range, due to zeroalign");
  182. } else {
  183. //Need to decrease magnitude to prevent scale overshoot! (start)
  184. decreaseMagnitude();
  185. done = false;
  186. continue;
  187. }
  188. }
  189. if (this.autoScaleStart && this.autoScaleEnd && newRange < (oldEnd-oldStart)){
  190. increaseMagnitude();
  191. done = false;
  192. continue;
  193. }
  194. done = true;
  195. }
  196. };
  197. DataScale.prototype.convertValue = function (value) {
  198. return this.containerHeight - ((value - this._start) * this.scale);
  199. };
  200. DataScale.prototype.screenToValue = function (pixels) {
  201. return ((this.containerHeight - pixels) / this.scale) + this._start;
  202. };
  203. module.exports = DataScale;