Skip to content

Commit 8186887

Browse files
committed
arc mark and pie transform
ref. #80
1 parent 375cdc8 commit 8186887

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export {plot} from "./plot.js";
22
export {Mark} from "./mark.js";
3+
export {Arc, arc} from "./marks/arc.js";
34
export {Area, area, areaX, areaY} from "./marks/area.js";
45
export {AxisX, AxisY} from "./marks/axis.js";
56
export {BarX, BarY, barX, barY} from "./marks/bar.js";

src/marks/arc.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {ascending} from "d3-array";
2+
import {create} from "d3-selection";
3+
import {arc as shapeArc} from "d3-shape";
4+
import {filter} from "../defined.js";
5+
import {Mark, number, maybeColor, maybeNumber, title} from "../mark.js";
6+
import {Style, applyDirectStyles, applyIndirectStyles, applyTransform} from "../style.js";
7+
8+
const constant = x => () => x;
9+
10+
export class Arc extends Mark {
11+
constructor(
12+
data,
13+
{
14+
startAngle = d => d.startAngle,
15+
endAngle = d => d.endAngle,
16+
x = constant(0),
17+
y = constant(0),
18+
innerRadius = 0,
19+
outerRadius = 140,
20+
z,
21+
title,
22+
fill,
23+
stroke,
24+
insetTop = 0,
25+
insetRight = 0,
26+
insetBottom = 0,
27+
insetLeft = 0,
28+
transform,
29+
...style
30+
} = {}
31+
) {
32+
const [vfill, cfill] = maybeColor(fill);
33+
const [vstroke, cstroke] = maybeColor(stroke);
34+
const [vx, cx = vx == null ? 0 : undefined] = maybeNumber(x);
35+
const [vy, cy = vy == null ? 0 : undefined] = maybeNumber(y);
36+
const [vri, cri = vri == null ? 0 : undefined] = maybeNumber(innerRadius);
37+
const [vro, cro = vro == null ? 0 : undefined] = maybeNumber(outerRadius);
38+
39+
super(
40+
data,
41+
[
42+
{name: "x", value: vx, scale: "x"},
43+
{name: "y", value: vy, scale: "y"},
44+
{name: "startAngle", value: startAngle},
45+
{name: "endAngle", value: endAngle},
46+
{name: "innerRadius", value: innerRadius, optional: true},
47+
{name: "outerRadius", value: outerRadius, optional: true},
48+
{name: "z", value: z, optional: true},
49+
{name: "title", value: title, optional: true},
50+
{name: "fill", value: vfill, scale: "color", optional: true},
51+
{name: "stroke", value: vstroke, scale: "color", optional: true}
52+
],
53+
transform
54+
);
55+
Style(this, {fill: cfill, stroke: cstroke, ...style});
56+
this.x = cx;
57+
this.y = cy;
58+
this.ri = cri;
59+
this.ro = cro;
60+
this.insetTop = number(insetTop);
61+
this.insetRight = number(insetRight);
62+
this.insetBottom = number(insetBottom);
63+
this.insetLeft = number(insetLeft);
64+
}
65+
render(
66+
I,
67+
{x, y, color},
68+
{startAngle: A, endAngle: B, innerRadius: RI, outerRadius: RO, x: X, y: Y, z: Z, title: L, fill: F, stroke: S}
69+
) {
70+
const index = filter(I, A, B, F, S);
71+
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
72+
73+
const arc = shapeArc()
74+
.startAngle(i => A[i])
75+
.endAngle(i => B[i])
76+
.innerRadius(i => RI[i] || this.ri)
77+
.outerRadius(i => RO[i] || this.ro);
78+
79+
return create("svg:g")
80+
.call(applyIndirectStyles, this)
81+
.call(applyTransform, x, y)
82+
.call(g => g.selectAll()
83+
.data(index)
84+
.join("path")
85+
.call(applyDirectStyles, this)
86+
.attr("d", arc)
87+
.attr("transform", i => `translate(${x(X[i])},${y(Y[i])})`)
88+
.attr("fill", F && (i => color(F[i])))
89+
.attr("stroke", S && (i => color(S[i])))
90+
.call(title(L)))
91+
.node();
92+
}
93+
}
94+
95+
export function arc(data, options) {
96+
return new Arc(data, options);
97+
}

test/arc-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const Plot = require("../");
2+
//const d3 = require("d3");
3+
//const tape = require("tape-await");
4+
const {JSDOM} = require("jsdom");
5+
global.document = new JSDOM("").window.document;
6+
7+
const array = [[0,0], [1,1], [2,-1], [3,2]];
8+
const xy = array.map(([x,y]) => ({x,y}));
9+
10+
const A = Plot.plot({marks: [Plot.arc(xy, {startAngle: "x", endAngle: "y" })]}).outerHTML;
11+
12+
console.warn(A);
13+

0 commit comments

Comments
 (0)