Skip to content

Commit bd5b388

Browse files
committed
size is a linear scale denoting (square pixel) area
The default value is 27, and the formula to compute the radius is sqrt(area/3), corresponding to a circle radius of 3.
1 parent 4c5dbdc commit bd5b388

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

src/marks/dot.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class Dot extends Mark {
1919
...style
2020
} = {}
2121
) {
22-
const [vsize, csize = vsize == null ? 3 : undefined] = maybeNumber(size);
22+
const [vsize, csize = vsize == null ? 27 : undefined] = maybeNumber(size);
2323
const [vfill, cfill = vfill == null ? "none" : undefined] = maybeColor(fill);
2424
const [vstroke, cstroke = vstroke == null && cfill === "none" ? "currentColor" : undefined] = maybeColor(stroke);
2525
super(
@@ -46,10 +46,10 @@ export class Dot extends Mark {
4646
render(
4747
I,
4848
{x, y, size, color},
49-
{x: X, y: Y, z: Z, size: R, title: L, fill: F, stroke: S}
49+
{x: X, y: Y, z: Z, size: A, title: L, fill: F, stroke: S}
5050
) {
5151
let index = filter(I, X, Y, F, S);
52-
if (R) index = index.filter(i => positive(R[i]));
52+
if (A) index = index.filter(i => positive(A[i]));
5353
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
5454
return create("svg:g")
5555
.call(applyIndirectStyles, this)
@@ -60,7 +60,7 @@ export class Dot extends Mark {
6060
.call(applyDirectStyles, this)
6161
.attr("cx", i => x(X[i]))
6262
.attr("cy", i => y(Y[i]))
63-
.attr("r", R ? i => size(R[i]) : this.size)
63+
.attr("r", A ? i => radius(size(A[i])) : radius(this.size))
6464
.attr("fill", F && (i => color(F[i])))
6565
.attr("stroke", S && (i => color(S[i])))
6666
.call(L ? text => text
@@ -71,6 +71,10 @@ export class Dot extends Mark {
7171
}
7272
}
7373

74+
function radius(area) {
75+
return Math.sqrt(area / 3);
76+
}
77+
7478
export function dot(data, options) {
7579
return new Dot(data, options);
7680
}

src/scales.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {registry, position, radius} from "./scales/index.js";
1+
import {registry, position, size} from "./scales/index.js";
22
import {ScaleDiverging, ScaleLinear, ScalePow, ScaleLog, ScaleSymlog} from "./scales/quantitative.js";
33
import {ScaleTime, ScaleUtc} from "./scales/temporal.js";
44
import {ScaleOrdinal, ScalePoint, ScaleBand} from "./scales/ordinal.js";
@@ -57,7 +57,7 @@ function inferScaleType(key, channels, {type, domain, range}) {
5757
}
5858
return type;
5959
}
60-
if (registry.get(key) === radius) return "sqrt";
60+
if (registry.get(key) === size) return "linear";
6161
for (const {type} of channels) if (type !== undefined) return type;
6262
if ((domain || range || []).length > 2) return inferOrdinalType(key);
6363
if (domain !== undefined) {

src/scales/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export const position = Symbol("position");
77
// have an associated legend.
88
export const color = Symbol("color");
99

10-
// Radius scales default to the sqrt type, have a default range of [0, 3], and a
10+
// Size scales default to the linear type, have a default range of [0, 27], and a
1111
// default domain from 0 to the median first quartile of associated channels.
12-
export const radius = Symbol("radius");
12+
export const size = Symbol("size");
1313

1414
// TODO Rather than hard-coding the list of known scale names, collect the names
1515
// and categories for each plot specification, so that custom marks can register
@@ -19,7 +19,7 @@ export const registry = new Map([
1919
["y", position],
2020
["fx", position],
2121
["fy", position],
22-
["size", radius],
22+
["size", size],
2323
["z", null],
2424
["color", color]
2525
]);

src/scales/quantitative.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
interpolateYlOrRd
5050
} from "d3-scale-chromatic";
5151
import {scaleDiverging, scaleLinear, scaleLog, scalePow, scaleSymlog} from "d3-scale";
52-
import {registry, radius, color} from "./index.js";
52+
import {registry, size, color} from "./index.js";
5353
import {positive} from "../defined.js";
5454

5555
const constant = x => () => x;
@@ -135,9 +135,9 @@ function Scheme(scheme) {
135135
export function ScaleQ(key, scale, channels, {
136136
nice,
137137
clamp,
138-
domain = (registry.get(key) === radius ? inferRadialDomain : inferDomain)(channels),
138+
domain = (registry.get(key) === size ? inferRadialDomain : inferDomain)(channels),
139139
round,
140-
range = registry.get(key) === radius ? [0, 3] : undefined, // see inferRadialDomain
140+
range = registry.get(key) === size ? [0, 27] : undefined, // see inferRadialDomain
141141
scheme,
142142
interpolate = registry.get(key) === color ? (range !== undefined ? interpolateRgb : scheme !== undefined ? Scheme(scheme) : interpolateTurbo) : round ? interpolateRound : undefined,
143143
invert,

0 commit comments

Comments
 (0)