Skip to content

Commit 644d336

Browse files
Filmbostock
andauthored
document box (#1394)
* document box * remove unused accessor * adopt identity transform * edits --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 814d2cd commit 644d336

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ The box mark is a composite mark consisting of four marks:
11851185
11861186
* a [rule](#rule) representing the extreme values (not including outliers)
11871187
* a [bar](#bar) representing the interquartile range (trimmed to the data)
1188-
* a [tick](#tick) represent the median value, and
1188+
* a [tick](#tick) representing the median value, and
11891189
* a [dot](#dot) representing outliers, if any
11901190
11911191
The given *options* are passed through to these underlying marks, with the exception of the following options:

src/marks/box.d.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,50 @@ import type {DotOptions} from "./dot.js";
44
import type {RuleXOptions, RuleYOptions} from "./rule.js";
55
import type {TickXOptions, TickYOptions} from "./tick.js";
66

7+
/** Options for the boxX mark. */
78
export type BoxXOptions = DotOptions & BarXOptions & TickXOptions & RuleXOptions;
89

10+
/** Options for the boxY mark. */
911
export type BoxYOptions = DotOptions & BarYOptions & TickYOptions & RuleYOptions;
1012

13+
/**
14+
* Returns a box mark that draws horizontal boxplots where **x** is quantitative
15+
* or temporal and **y**, if present, is ordinal. The box mark is a compound
16+
* mark consisting of four marks:
17+
*
18+
* - a rule representing the extreme values (not including outliers),
19+
* - a bar representing the interquartile range (trimmed to the data),
20+
* - a tick representing the median value, and
21+
* - a dot representing outliers, if any.
22+
*
23+
* The given *options* are passed through to these underlying marks, with the
24+
* exception of the following options:
25+
*
26+
* - **fill** - the fill color of the bar; defaults to gray
27+
* - **fillOpacity** - the fill opacity of the bar; defaults to 1
28+
* - **stroke** - the stroke color of the rule, tick, and dot; defaults to *currentColor*
29+
* - **strokeOpacity** - the stroke opacity of the rule, tick, and dot; defaults to 1
30+
* - **strokeWidth** - the stroke width of the tick; defaults to 2
31+
*/
1132
export function boxX(data?: Data, options?: BoxXOptions): CompoundMark;
1233

34+
/**
35+
* Returns a box mark that draws vertical boxplots where **y** is quantitative
36+
* or temporal and **x**, if present, is ordinal. The box mark is a compound
37+
* mark consisting of four marks:
38+
*
39+
* - a rule representing the extreme values (not including outliers),
40+
* - a bar representing the interquartile range (trimmed to the data),
41+
* - a tick representing the median value, and
42+
* - a dot representing outliers, if any.
43+
*
44+
* The given *options* are passed through to these underlying marks, with the
45+
* exception of the following options:
46+
*
47+
* - **fill** - the fill color of the bar; defaults to gray
48+
* - **fillOpacity** - the fill opacity of the bar; defaults to 1
49+
* - **stroke** - the stroke color of the rule, tick, and dot; defaults to *currentColor*
50+
* - **strokeOpacity** - the stroke opacity of the rule, tick, and dot; defaults to 1
51+
* - **strokeWidth** - the stroke width of the tick; defaults to 2
52+
*/
1353
export function boxY(data?: Data, options?: BoxYOptions): CompoundMark;

src/marks/box.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {min, max, quantile} from "d3";
1+
import {max, min, quantile} from "d3";
22
import {marks} from "../mark.js";
3+
import {identity} from "../options.js";
34
import {groupX, groupY, groupZ} from "../transforms/group.js";
45
import {map} from "../transforms/map.js";
56
import {barX, barY} from "./bar.js";
@@ -11,7 +12,7 @@ export function boxX(data, options = {}) {
1112
// Returns a composite mark for producing a horizontal box plot, applying the
1213
// necessary statistical transforms. The boxes are grouped by y, if present.
1314
const {
14-
x = {transform: (x) => x},
15+
x = identity,
1516
y = null,
1617
fill = "#ccc",
1718
fillOpacity,
@@ -34,7 +35,7 @@ export function boxY(data, options = {}) {
3435
// Returns a composite mark for producing a vertical box plot, applying the
3536
// necessary statistical transforms. The boxes are grouped by x, if present.
3637
const {
37-
y = {transform: (y) => y},
38+
y = identity,
3839
x = null,
3940
fill = "#ccc",
4041
fillOpacity,
@@ -60,20 +61,20 @@ function oqr(values) {
6061
return values.map((v) => (v < r1 || v > r2 ? v : NaN));
6162
}
6263

63-
function loqr1(values, value) {
64-
const lo = quartile1(values, value) * 2.5 - quartile3(values, value) * 1.5;
64+
function loqr1(values) {
65+
const lo = quartile1(values) * 2.5 - quartile3(values) * 1.5;
6566
return min(values, (d) => (d >= lo ? d : NaN));
6667
}
6768

68-
function hiqr2(values, value) {
69-
const hi = quartile3(values, value) * 2.5 - quartile1(values, value) * 1.5;
69+
function hiqr2(values) {
70+
const hi = quartile3(values) * 2.5 - quartile1(values) * 1.5;
7071
return max(values, (d) => (d <= hi ? d : NaN));
7172
}
7273

73-
function quartile1(values, value) {
74-
return quantile(values, 0.25, value);
74+
function quartile1(values) {
75+
return quantile(values, 0.25);
7576
}
7677

77-
function quartile3(values, value) {
78-
return quantile(values, 0.75, value);
78+
function quartile3(values) {
79+
return quantile(values, 0.75);
7980
}

0 commit comments

Comments
 (0)