Skip to content

Commit 05005b5

Browse files
committed
working arc mark with external pie
1 parent a6b039d commit 05005b5

File tree

4 files changed

+4042
-4006
lines changed

4 files changed

+4042
-4006
lines changed

src/marks/arc.js

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,50 @@ import {ascending} from "d3-array";
22
import {create} from "d3-selection";
33
import {arc as shapeArc} from "d3-shape";
44
import {filter, nonempty} from "../defined.js";
5-
import {Mark, maybeColor, maybeNumber, title} from "../mark.js";
6-
import {Style, applyDirectStyles, applyIndirectStyles, applyTransform} from "../style.js";
5+
import {Mark, maybeNumber} from "../mark.js";
6+
import {applyDirectStyles, applyIndirectStyles, applyTransform, applyChannelStyles, offset} from "../style.js";
77

8-
const constant = x => () => x;
8+
const defaults = {
9+
fill: "currentColor",
10+
stroke: "none"
11+
};
912

1013
export class Arc extends Mark {
11-
constructor(
12-
data,
13-
{
14+
constructor(data, options = {}) {
15+
const {
16+
x,
17+
y,
1418
startAngle = d => d.startAngle,
1519
endAngle = d => d.endAngle,
16-
x = constant(0),
17-
y = constant(0),
1820
innerRadius,
1921
outerRadius,
20-
padAngle,
22+
padAngle,
2123
z,
2224
title,
23-
label,
24-
fill,
25-
stroke,
26-
transform,
27-
...style
28-
} = {}
29-
) {
30-
const [vfill, cfill] = maybeColor(fill, "currentColor");
31-
const [vstroke, cstroke] = maybeColor(stroke, cfill === "none" ? "currentColor" : cfill === "currentColor" ? "white" : "none");
25+
label
26+
} = options;
3227
const [vsa, csa] = maybeNumber(startAngle, 0);
3328
const [vea, cea] = maybeNumber(endAngle, 2 * Math.PI);
3429
const [vpa, cpa] = maybeNumber(padAngle, 0);
35-
const [vx, cx] = maybeNumber(x, 0);
36-
const [vy, cy] = maybeNumber(y, 0);
3730
const [vri, cri] = maybeNumber(innerRadius, 0);
3831
const [vro, cro] = maybeNumber(outerRadius, 100);
39-
4032
super(
4133
data,
4234
[
43-
{name: "x", value: vx, scale: "x", optional: true},
44-
{name: "y", value: vy, scale: "y", optional: true},
35+
{name: "x", value: x, scale: "x", optional: true},
36+
{name: "y", value: y, scale: "y", optional: true},
4537
{name: "startAngle", value: vsa, optional: true},
4638
{name: "endAngle", value: vea, optional: true},
47-
{name: "innerRadius", value: vri, optional: true},
48-
{name: "outerRadius", value: vro, optional: true},
39+
{name: "innerRadius", value: vri, scale: "r", optional: true},
40+
{name: "outerRadius", value: vro, scale : "r", optional: true},
4941
{name: "padAngle", value: vpa, optional: true},
5042
{name: "z", value: z, optional: true},
5143
{name: "title", value: title, optional: true},
52-
{name: "label", value: label, optional: true},
53-
{name: "fill", value: vfill, scale: "color", optional: true},
54-
{name: "stroke", value: vstroke, scale: "color", optional: true}
44+
{name: "label", value: label, optional: true}
5545
],
56-
transform
46+
options,
47+
defaults
5748
);
58-
Style(this, {fill: cfill, stroke: cstroke, ...style});
59-
this.x = cx;
60-
this.y = cy;
6149
this.sa = csa;
6250
this.ea = cea;
6351
this.ri = cri;
@@ -66,14 +54,25 @@ export class Arc extends Mark {
6654
}
6755
render(
6856
I,
69-
{x, y, color},
70-
{startAngle: SA, endAngle: EA, innerRadius: RI, outerRadius: RO, padAngle: PA, x: X, y: Y, z: Z, title: T, label: L, fill: F, stroke: S},
71-
{marginTop, marginRight, marginBottom, marginLeft, width, height}
57+
{x, y},
58+
channels,
59+
{width, height, marginTop, marginRight, marginBottom, marginLeft}
7260
) {
73-
const index = filter(I, SA, EA, F, S);
61+
const {
62+
startAngle: SA,
63+
endAngle: EA,
64+
innerRadius: RI,
65+
outerRadius: RO,
66+
padAngle: PA,
67+
x: X, y: Y, z: Z,
68+
label: L
69+
} = channels;
70+
const {dx, dy} = this;
71+
let index = filter(I, SA, EA);
7472
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
7573

76-
const r0 = Math.min(width - marginLeft - marginRight, height - marginTop - marginBottom) / 200;
74+
// const r0 = Math.min(width - marginLeft - marginRight, height - marginTop - marginBottom) / 200;
75+
const r0 = 1;
7776

7877
const arc = shapeArc()
7978
.startAngle(SA ? (i => SA[i]) : this.sa)
@@ -83,19 +82,17 @@ export class Arc extends Mark {
8382
.padAngle(PA ? (i => PA[i]) : this.pa);
8483

8584
const wrapper = create("svg:g")
86-
.call(applyTransform, x, y);
87-
wrapper
88-
.append("g")
85+
// wrapper
86+
// .append("g")
8987
.call(applyIndirectStyles, this)
88+
.call(applyTransform, x, y, offset + dx, offset + dy)
9089
.call(g => g.selectAll()
9190
.data(index)
9291
.join("path")
9392
.call(applyDirectStyles, this)
9493
.attr("d", arc)
95-
.attr("transform", i => `translate(${x(X[i])},${y(Y[i])})`)
96-
.attr("fill", F && (i => color(F[i])))
97-
.attr("stroke", S && (i => color(S[i])))
98-
.call(title(T)));
94+
.attr("transform", i => `translate(${X ? X[i] : x},${Y ? Y[i] : y})`)
95+
.call(applyChannelStyles, channels));
9996
if (L) wrapper.append("g").call(_label(L, index, arc));
10097
return wrapper.node();
10198

test/plots/arc.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
export default async function() {
5+
const data = d3.pie()([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
6+
console.log(data);
7+
return Plot.plot({
8+
marks: [
9+
Plot.arc(data, {
10+
x: 0, y: 0,
11+
innerRadius: d => 10 * d.value,
12+
outerRadius: d => 30 * d.value,
13+
fill: d => `a${d.value}`
14+
})
15+
]
16+
});
17+
}

test/plots/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export {default as athletesSportWeight} from "./athletes-sport-weight.js";
1919
export {default as athletesWeight} from "./athletes-weight.js";
2020
export {default as athletesWeightCumulative} from "./athletes-weight-cumulative.js";
2121
export {default as availability} from "./availability.js";
22+
export {default as arc} from "./arc.js";
2223
export {default as ballotStatusRace} from "./ballot-status-race.js";
2324
export {default as beckerBarley} from "./becker-barley.js";
2425
export {default as caltrain} from "./caltrain.js";

0 commit comments

Comments
 (0)