Skip to content

Commit

Permalink
fix(liquid): fix liquid precision (#5788)
Browse files Browse the repository at this point in the history
  • Loading branch information
ai-qing-hai committed Nov 13, 2023
1 parent 89c06c6 commit 07196d1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
17 changes: 7 additions & 10 deletions site/examples/general/Liquid/demo/liquid-pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ const chart = new Chart({
autoFit: true,
});

chart
.liquid()
.data(0.7)
.style({
shape: 'pin', // Build-in shapes: rect, circle, pin, diamond, triangle.
textFill: '#fff',
outlineBorder: 4,
outlineDistance: 8,
waveLength: 128,
});
chart.liquid().data(0.581).style({
shape: 'pin', // Build-in shapes: rect, circle, pin, diamond, triangle.
textFill: '#fff',
outlineBorder: 4,
outlineDistance: 8,
waveLength: 128,
});

chart.render();
6 changes: 1 addition & 5 deletions src/component/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
isTranspose,
radiusOf,
} from '../utils/coordinate';
import { prettyNumber } from '../utils/number';
import { capitalizeFirst } from '../utils/helper';
import { adaptor, isVertical, titleContent } from './utils';

Expand Down Expand Up @@ -116,11 +117,6 @@ function ticksOf(
return tickMethod(min, max, tickCount);
}

function prettyNumber(n: number) {
if (typeof n !== 'number') return n;
return Math.abs(n) < 1e-15 ? n : parseFloat(n.toFixed(15));
}

// Set inset for axis.
function createInset(position, coordinate) {
if (isPolar(coordinate)) return (d) => d;
Expand Down
3 changes: 2 additions & 1 deletion src/mark/liquid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { deepMix, isNumber } from '@antv/util';
import { subObject } from '../utils/helper';
import { prettyNumber } from '../utils/number';
import { CompositeMarkComponent as CC } from '../runtime';
import { LiquidMark } from '../spec';
import { LiquidShape } from '../shape';
Expand Down Expand Up @@ -89,7 +90,7 @@ export const Liquid: CC<LiquidOptions> = (options) => {
}),
deepMix({}, DEFAULT_TEXT_OPTIONS, {
style: {
text: `${percent * 100} %`,
text: `${prettyNumber(percent * 100)} %`,
...textStyle,
},
animate,
Expand Down
5 changes: 1 addition & 4 deletions src/runtime/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type RadialOptions,
} from '../coordinate';
import { combine } from '../utils/array';
import { prettyNumber } from '../utils/number';
import { capitalizeFirst, defined, subObject } from '../utils/helper';
import { LEGEND_INFER_STRATEGIES } from '../component/constant';
import {
Expand Down Expand Up @@ -1115,7 +1116,3 @@ function normalizeLabel(d: string | DisplayObject): DisplayObject {
if (d instanceof DisplayObject) return d;
return new Text({ style: { text: `${d}` } });
}

function prettyNumber(n: number) {
return Math.abs(n) < 1e-15 ? n : parseFloat(n.toFixed(15));
}
8 changes: 8 additions & 0 deletions src/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@
export function clamp(v: number, lower: number, upper: number): number {
return Math.max(lower, Math.min(v, upper));
}

/**
* Precision conversion
*/
export function prettyNumber(n: number, precision = 10) {
if (typeof n !== 'number') return n;
return Math.abs(n) < 1e-15 ? n : parseFloat(n.toFixed(precision));
}

0 comments on commit 07196d1

Please sign in to comment.