jitter.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import Axis2D from '../coord/cartesian/Axis2D.js';
  41. import { makeInner } from './model.js';
  42. export function needFixJitter(seriesModel, axis) {
  43. var coordinateSystem = seriesModel.coordinateSystem;
  44. var coordType = coordinateSystem && coordinateSystem.type;
  45. var baseAxis = coordinateSystem && coordinateSystem.getBaseAxis && coordinateSystem.getBaseAxis();
  46. var scaleType = baseAxis && baseAxis.scale && baseAxis.scale.type;
  47. var seriesValid = coordType === 'cartesian2d' && scaleType === 'ordinal' || coordType === 'single';
  48. var axisValid = axis.model.get('jitter') > 0;
  49. return seriesValid && axisValid;
  50. }
  51. var inner = makeInner();
  52. /**
  53. * Fix jitter for overlapping data points.
  54. *
  55. * @param fixedAxis The axis whose coord doesn't change with jitter.
  56. * @param fixedCoord The coord of fixedAxis.
  57. * @param floatCoord The coord of the other axis, which should be changed with jittering.
  58. * @param radius The radius of the data point, considering the symbol is a circle.
  59. * @returns updated floatCoord.
  60. */
  61. export function fixJitter(fixedAxis, fixedCoord, floatCoord, radius) {
  62. if (fixedAxis instanceof Axis2D) {
  63. var scaleType = fixedAxis.scale.type;
  64. if (scaleType !== 'category' && scaleType !== 'ordinal') {
  65. return floatCoord;
  66. }
  67. }
  68. var axisModel = fixedAxis.model;
  69. var jitter = axisModel.get('jitter');
  70. var jitterOverlap = axisModel.get('jitterOverlap');
  71. var jitterMargin = axisModel.get('jitterMargin') || 0;
  72. // Get band width to limit jitter range
  73. var bandWidth = fixedAxis.scale.type === 'ordinal' ? fixedAxis.getBandWidth() : null;
  74. if (jitter > 0) {
  75. if (jitterOverlap) {
  76. return fixJitterIgnoreOverlaps(floatCoord, jitter, bandWidth, radius);
  77. } else {
  78. return fixJitterAvoidOverlaps(fixedAxis, fixedCoord, floatCoord, radius, jitter, jitterMargin);
  79. }
  80. }
  81. return floatCoord;
  82. }
  83. function fixJitterIgnoreOverlaps(floatCoord, jitter, bandWidth, radius) {
  84. // Don't clamp single axis
  85. if (bandWidth === null) {
  86. return floatCoord + (Math.random() - 0.5) * jitter;
  87. }
  88. var maxJitter = bandWidth - radius * 2;
  89. var actualJitter = Math.min(Math.max(0, jitter), maxJitter);
  90. return floatCoord + (Math.random() - 0.5) * actualJitter;
  91. }
  92. function fixJitterAvoidOverlaps(fixedAxis, fixedCoord, floatCoord, radius, jitter, margin) {
  93. var store = inner(fixedAxis);
  94. if (!store.items) {
  95. store.items = [];
  96. }
  97. var items = store.items;
  98. // Try both positive and negative directions, choose the one with smaller movement
  99. var overlapA = placeJitterOnDirection(items, fixedCoord, floatCoord, radius, jitter, margin, 1);
  100. var overlapB = placeJitterOnDirection(items, fixedCoord, floatCoord, radius, jitter, margin, -1);
  101. var minFloat = Math.abs(overlapA - floatCoord) < Math.abs(overlapB - floatCoord) ? overlapA : overlapB;
  102. // Clamp only category axis
  103. var bandWidth = fixedAxis.scale.type === 'ordinal' ? fixedAxis.getBandWidth() : null;
  104. var distance = Math.abs(minFloat - floatCoord);
  105. if (distance > jitter / 2 || bandWidth && distance > bandWidth / 2 - radius) {
  106. // If the new item is moved too far, then give up.
  107. // Fall back to random jitter.
  108. return fixJitterIgnoreOverlaps(floatCoord, jitter, bandWidth, radius);
  109. }
  110. // Add new point to array
  111. items.push({
  112. fixedCoord: fixedCoord,
  113. floatCoord: minFloat,
  114. r: radius
  115. });
  116. return minFloat;
  117. }
  118. function placeJitterOnDirection(items, fixedCoord, floatCoord, radius, jitter, margin, direction) {
  119. var y = floatCoord;
  120. // Check all existing items for overlap and find the maximum adjustment needed
  121. for (var i = 0; i < items.length; i++) {
  122. var item = items[i];
  123. var dx = fixedCoord - item.fixedCoord;
  124. var dy = y - item.floatCoord;
  125. var d2 = dx * dx + dy * dy;
  126. var r = radius + item.r + margin;
  127. if (d2 < r * r) {
  128. // Has overlap, calculate required adjustment
  129. var requiredY = item.floatCoord + Math.sqrt(r * r - dx * dx) * direction;
  130. // Check if this adjustment would move too far
  131. if (Math.abs(requiredY - floatCoord) > jitter / 2) {
  132. return Number.MAX_VALUE; // Give up
  133. }
  134. // Update y only when it's larger to the center
  135. if (direction === 1 && requiredY > y || direction === -1 && requiredY < y) {
  136. y = requiredY;
  137. // Loop from the start again
  138. i = -1; // Reset index to recheck all items
  139. continue; // Recalculate with the new y position
  140. }
  141. }
  142. }
  143. return y;
  144. }