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.

232 lines
6.8 KiB

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