Ovilia commented on code in PR #19941:
URL: https://github.com/apache/echarts/pull/19941#discussion_r2030708008
##########
src/util/jitter.ts:
##########
@@ -0,0 +1,129 @@
+import type Axis from '../coord/Axis';
+import type { AxisBaseModel } from '../coord/AxisBaseModel';
+import Axis2D from '../coord/cartesian/Axis2D';
+import type SingleAxis from '../coord/single/SingleAxis';
+import type SeriesModel from '../model/Series';
+import { makeInner } from './model';
+
+export function needFixJitter(seriesModel: SeriesModel, axis: Axis): boolean {
+ const { coordinateSystem } = seriesModel;
+ const { type: coordType } = coordinateSystem;
+ const baseAxis = coordinateSystem.getBaseAxis();
+ const { type: scaleType } = baseAxis.scale;
+ const seriesValid = coordType === 'cartesian2d'
+ && (scaleType === 'category' || scaleType === 'ordinal')
+ || coordType === 'single';
+
+ const axisValid = (axis.model as AxisBaseModel).get('jitter') > 0;
+ return seriesValid && axisValid;
+}
+
+export type JitterData = {
+ fixedCoord: number,
+ floatCoord: number,
+ r: number
+};
+
+const inner = makeInner<{ items: JitterData[] }, Axis2D | SingleAxis>();
+
+/**
+ * Fix jitter for overlapping data points.
+ *
+ * @param fixedAxis The axis whose coord doesn't change with jitter.
+ * @param fixedCoord The coord of fixedAxis.
+ * @param floatCoord The coord of the other axis, which should be changed with
jittering.
+ * @param radius The radius of the data point, considering the symbol is a
circle.
+ * @returns updated floatCoord.
+ */
+export function fixJitter(
+ fixedAxis: Axis2D | SingleAxis,
+ fixedCoord: number,
+ floatCoord: number,
+ radius: number
+): number {
+ if (fixedAxis instanceof Axis2D) {
+ const scaleType = fixedAxis.scale.type;
+ if (scaleType !== 'category' && scaleType !== 'ordinal') {
+ return floatCoord;
+ }
+ }
+ const axisModel = fixedAxis.model as AxisBaseModel;
+ const jitter = axisModel.get('jitter');
+ const jitterOverlap = axisModel.get('jitterOverlap');
+ const jitterMargin = axisModel.get('jitterMargin') || 0;
+ if (jitter > 0) {
+ if (jitterOverlap) {
+ return fixJitterIgnoreOverlaps(floatCoord, jitter);
+ }
+ else {
+ return fixJitterAvoidOverlaps(fixedAxis, fixedCoord, floatCoord,
radius, jitter, jitterMargin);
+ }
+ }
+ return floatCoord;
+}
+
+function fixJitterIgnoreOverlaps(floatCoord: number, jitter: number): number {
+ return floatCoord + (Math.random() - 0.5) * jitter;
Review Comment:
Fixed.
<img width="1252" alt="image"
src="https://github.com/user-attachments/assets/ca72d2c1-bdc5-4064-9fcf-12d612bda033"
/>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]