Skip to content

Commit

Permalink
shape.digits (#205)
Browse files Browse the repository at this point in the history
* shape.digits

* document _shape_.digits

* Update README

* Update README

* update dependencies

Co-authored-by: Philippe Rivière <fil@rezo.net>
  • Loading branch information
mbostock and Fil committed Dec 20, 2022
1 parent 77fce6d commit 6f05305
Show file tree
Hide file tree
Showing 15 changed files with 401 additions and 283 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2010-2021 Mike Bostock
Copyright 2010-2022 Mike Bostock

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
Expand Down
100 changes: 60 additions & 40 deletions README.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@
},
"sideEffects": false,
"dependencies": {
"d3-path": "1 - 3"
"d3-path": "^3.1.0"
},
"devDependencies": {
"d3-polygon": "1 - 3",
"eslint": "8",
"mocha": "9",
"rollup": "2",
"mocha": "10",
"rollup": "3",
"rollup-plugin-terser": "7"
},
"scripts": {
"test": "mocha 'test/**/*-test.js' && eslint src test",
"prepublishOnly": "rm -rf dist && yarn test && rollup -c",
"prepublishOnly": "rm -rf dist && rollup -c",
"postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../${npm_package_name}/dist/${npm_package_name}.js ${npm_package_name}.v${npm_package_version%%.*}.js && cp ../${npm_package_name}/dist/${npm_package_name}.min.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git add ${npm_package_name}.v${npm_package_version%%.*}.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git commit -m \"${npm_package_name} ${npm_package_version}\" && git push && cd -"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {readFileSync} from "fs";
import {terser} from "rollup-plugin-terser";
import * as meta from "./package.json";
import meta from "./package.json" assert {type: "json"};

// Extract copyrights from the LICENSE.
const copyright = readFileSync("./LICENSE", "utf-8")
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");
});
Loading

0 comments on commit 6f05305

Please sign in to comment.