Skip to content

Commit

Permalink
shape.digits
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Dec 19, 2022
1 parent c95704e commit 4684531
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"sideEffects": false,
"dependencies": {
"d3-path": "1 - 3"
"d3-path": "github:d3/d3-path#164a15d40b330a4ea6946981835b8d6bba27143f"
},
"devDependencies": {
"d3-polygon": "1 - 3",
Expand Down
5 changes: 3 additions & 2 deletions src/arc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {path} from "d3-path";
import constant from "./constant.js";
import {abs, acos, asin, atan2, cos, epsilon, halfPi, max, min, pi, sin, sqrt, tau} from "./math.js";
import {withPath} from "./path.js";

function arcInnerRadius(d) {
return d.innerRadius;
Expand Down Expand Up @@ -82,7 +82,8 @@ export default function() {
startAngle = arcStartAngle,
endAngle = arcEndAngle,
padAngle = arcPadAngle,
context = null;
context = null,
path = withPath(arc);

function arc() {
var buffer,
Expand Down
5 changes: 3 additions & 2 deletions src/area.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import {path} from "d3-path";
import array from "./array.js";
import constant from "./constant.js";
import curveLinear from "./curve/linear.js";
import line from "./line.js";
import {withPath} from "./path.js";
import {x as pointX, y as pointY} from "./point.js";

export default function(x0, y0, y1) {
var x1 = null,
defined = constant(true),
context = null,
curve = curveLinear,
output = null;
output = null,
path = withPath(area);

x0 = typeof x0 === "function" ? x0 : (x0 === undefined) ? pointX : constant(+x0);
y0 = typeof y0 === "function" ? y0 : (y0 === undefined) ? constant(0) : constant(+y0);
Expand Down
5 changes: 3 additions & 2 deletions src/line.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {path} from "d3-path";
import array from "./array.js";
import constant from "./constant.js";
import curveLinear from "./curve/linear.js";
import {withPath} from "./path.js";
import {x as pointX, y as pointY} from "./point.js";

export default function(x, y) {
var defined = constant(true),
context = null,
curve = curveLinear,
output = null;
output = null,
path = withPath(line);

x = typeof x === "function" ? x : (x === undefined) ? pointX : constant(x);
y = typeof y === "function" ? y : (y === undefined) ? pointY : constant(y);
Expand Down
15 changes: 8 additions & 7 deletions src/link.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {path} from "d3-path";
import {slice} from "./array.js";
import constant from "./constant.js";
import {bumpX, bumpY, bumpRadial} from "./curve/bump.js";
import {withPath} from "./path.js";
import {x as pointX, y as pointY} from "./point.js";

function linkSource(d) {
Expand All @@ -13,12 +13,13 @@ function linkTarget(d) {
}

export function link(curve) {
let source = linkSource;
let target = linkTarget;
let x = pointX;
let y = pointY;
let context = null;
let output = null;
let source = linkSource,
target = linkTarget,
x = pointX,
y = pointY,
context = null,
output = null,
path = withPath(link);

function link() {
let buffer;
Expand Down
19 changes: 19 additions & 0 deletions src/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Path} from "d3-path";

export function withPath(shape) {
let digits = 3;

shape.digits = function(_) {
if (!arguments.length) return digits;
if (_ == null) {
digits = null;
} else {
const d = Math.floor(_);
if (!(d >= 0)) throw new RangeError(`invalid digits: ${_}`);
digits = d;
}
return shape;
};

return () => new Path(digits);
}
5 changes: 3 additions & 2 deletions src/symbol.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {path} from "d3-path";
import constant from "./constant.js";
import {withPath} from "./path.js";
import asterisk from "./symbol/asterisk.js";
import circle from "./symbol/circle.js";
import cross from "./symbol/cross.js";
Expand Down Expand Up @@ -37,7 +37,8 @@ export const symbolsStroke = [
];

export default function Symbol(type, size) {
let context = null;
let context = null,
path = withPath(symbol);

type = typeof type === "function" ? type : constant(type || circle);
size = typeof size === "function" ? size : constant(size === undefined ? 64 : +size);
Expand Down
2 changes: 1 addition & 1 deletion test/asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function normalizePath(path) {
}

function formatNumber(s) {
return Math.abs((s = +s) - Math.round(s)) < 1e-6 ? Math.round(s) : s.toFixed(6);
return Math.abs((s = +s) - Math.round(s)) < 1e-6 ? Math.round(s) : s.toFixed(3);
}

export function assertInDelta(actual, expected, delta) {
Expand Down
3 changes: 2 additions & 1 deletion test/curve/bundle-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import assert from "assert";
import {line, curveBundle} from "../../src/index.js";
import {assertPathEqual} from "../asserts.js";

it("line.curve(curveBundle) uses a default beta of 0.85", () => {
const l = line().curve(curveBundle.beta(0.85));
assert.strictEqual(line().curve(curveBundle)([[0, 1], [1, 3], [2, 1], [3, 3]]), l([[0, 1], [1, 3], [2, 1], [3, 3]]));
});

it("line.curve(curveBundle.beta(beta)) uses the specified beta", () => {
assert.strictEqual(line().curve(curveBundle.beta(0.5))([[0, 1], [1, 3], [2, 1], [3, 3]]), "M0,1L0.16666666666666666,1.222222222222222C0.3333333333333333,1.4444444444444444,0.6666666666666666,1.8888888888888886,1,1.9999999999999998C1.3333333333333333,2.1111111111111107,1.6666666666666667,1.8888888888888886,2,2C2.3333333333333335,2.111111111111111,2.6666666666666665,2.5555555555555554,2.8333333333333335,2.7777777777777772L3,3");
assertPathEqual(line().curve(curveBundle.beta(0.5))([[0, 1], [1, 3], [2, 1], [3, 3]]), "M0,1L0.166667,1.222222C0.333333,1.444444,0.666667,1.888889,1,2C1.333333,2.111111,1.666667,1.888889,2,2C2.333333,2.111111,2.666667,2.555556,2.833333,2.777778L3,3");
});

it("line.curve(curveBundle.beta(beta)) coerces the specified beta to a number", () => {
Expand Down
18 changes: 18 additions & 0 deletions test/line-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ it("line.curve(curve) sets the curve method", () => {
assert.strictEqual(l([]), null);
assertPathEqual(l([[0, 1], [2, 3]]), "M0,1L2,3Z");
});

it("line.digits(digits) sets the maximum fractional digits", () => {
const points = [[0, Math.PI], [Math.E, 4]];
const l = line();
assert.strictEqual(l.digits(), 3);
assert.strictEqual(l(points), "M0,3.142L2.718,4");
assert.strictEqual(l.digits(6), l);
assert.strictEqual(l.digits(), 6);
assert.strictEqual(l(points), "M0,3.141593L2.718282,4");
assert.strictEqual(line()(points), "M0,3.142L2.718,4");
assert.strictEqual(l.digits(null), l);
assert.strictEqual(l.digits(), null);
assert.strictEqual(l(points), "M0,3.141592653589793L2.718281828459045,4");
assert.strictEqual(line()(points), "M0,3.142L2.718,4");
assert.strictEqual(l.digits(3), l);
assert.strictEqual(l.digits(), 3);
assert.strictEqual(l(points), "M0,3.142L2.718,4");
});
8 changes: 4 additions & 4 deletions test/link-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import {path} from "d3-path";
import {pathRound} from "d3-path";
import {link, linkHorizontal, linkVertical} from "../src/index.js";
import {curveLinear, curveBumpX, curveBumpY} from "../src/index.js";
import {assertPathEqual} from "./asserts.js";
Expand Down Expand Up @@ -107,8 +107,8 @@ it("linkVertical() is an alias for link(curveBumpY)", () => {
});

it("link.context(context) sets the context", () => {
const p = path();
const p = pathRound(6);
const l = link(curveLinear).context(p);
assert.strictEqual(l({source: [0, 1], target: [2, 3]}), undefined);
assertPathEqual(p, "M0,1L2,3");
assert.strictEqual(l({source: [0, Math.E], target: [Math.PI, 3]}), undefined);
assert.strictEqual(p + "", "M0,2.718282L3.141593,3");
});
5 changes: 2 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,9 @@ cross-spawn@^7.0.2:
shebang-command "^2.0.0"
which "^2.0.1"

"d3-path@1 - 3":
"d3-path@github:d3/d3-path#164a15d40b330a4ea6946981835b8d6bba27143f":
version "3.0.1"
resolved "https://registry.yarnpkg.com/d3-path/-/d3-path-3.0.1.tgz#f09dec0aaffd770b7995f1a399152bf93052321e"
integrity sha512-gq6gZom9AFZby0YLduxT1qmrp4xpBA1YZr19OI717WIdKE2OM5ETq5qrHLb301IgxhLwcuxvGZVLeeWc/k1I6w==
resolved "https://codeload.github.com/d3/d3-path/tar.gz/164a15d40b330a4ea6946981835b8d6bba27143f"

"d3-polygon@1 - 3":
version "3.0.1"
Expand Down

0 comments on commit 4684531

Please sign in to comment.