Skip to content

Commit d3840cc

Browse files
authored
indexed bar shorthand (#785)
1 parent 8cb7815 commit d3840cc

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ Returns a new horizontal bar↔︎ with the given *data* and *options*. The foll
770770
* **x1** - the starting horizontal position; bound to the *x* scale
771771
* **x2** - the ending horizontal position; bound to the *x* scale
772772

773-
If neither the **x1** nor **x2** option is specified, the **x** option may be specified as shorthand to apply an implicit [stackX transform](#plotstackxstack-options); this is the typical configuration for a horizontal bar chart with bars aligned at *x* = 0. If the **x** option is not specified, it defaults to the identity function.
773+
If neither the **x1** nor **x2** option is specified, the **x** option may be specified as shorthand to apply an implicit [stackX transform](#plotstackxstack-options); this is the typical configuration for a horizontal bar chart with bars aligned at *x* = 0. If the **x** option is not specified, it defaults to the identity function. If *options* is undefined, then it defaults to **x2** as the identity function and **y** as the index of data; this allows an array of numbers to be passed to Plot.barX to make a quick sequential bar chart.
774774

775775
If an **interval** is specified, such as d3.utcDay, **x1** and **x2** can be derived from **x**: *interval*.floor(*x*) is invoked for each *x* to produce *x1*, and *interval*.offset(*x1*) is invoked for each *x1* to produce *x2*. If the interval is specified as a number *n*, *x1* and *x2* are taken as the two consecutive multiples of *n* that bracket *x*.
776776

@@ -791,7 +791,7 @@ Returns a new vertical bar↕︎ with the given *data* and *options*. The follow
791791
* **y1** - the starting vertical position; bound to the *y* scale
792792
* **y2** - the ending vertical position; bound to the *y* scale
793793

794-
If neither the **y1** nor **y2** option is specified, the **y** option may be specified as shorthand to apply an implicit [stackY transform](#plotstackystack-options); this is the typical configuration for a vertical bar chart with bars aligned at *y* = 0. If the **y** option is not specified, it defaults to the identity function.
794+
If neither the **y1** nor **y2** option is specified, the **y** option may be specified as shorthand to apply an implicit [stackY transform](#plotstackystack-options); this is the typical configuration for a vertical bar chart with bars aligned at *y* = 0. If the **y** option is not specified, it defaults to the identity function. If *options* is undefined, then it defaults to **y2** as the identity function and **x** as the index of data; this allows an array of numbers to be passed to Plot.barY to make a quick sequential bar chart.
795795

796796
If an **interval** is specified, such as d3.utcDay, **y1** and **y2** can be derived from **y**: *interval*.floor(*y*) is invoked for each *y* to produce *y1*, and *interval*.offset(*y1*) is invoked for each *y1* to produce *y2*. If the interval is specified as a number *n*, *y1* and *y2* are taken as the two consecutive multiples of *n* that bracket *y*.
797797

src/marks/bar.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {create} from "d3";
22
import {Mark} from "../plot.js";
3-
import {number} from "../options.js";
3+
import {identity, indexOf, number} from "../options.js";
44
import {isCollapsed} from "../scales.js";
55
import {applyDirectStyles, applyIndirectStyles, applyTransform, impliedString, applyAttr, applyChannelStyles} from "../style.js";
66
import {maybeIdentityX, maybeIdentityY} from "../transforms/identity.js";
@@ -114,10 +114,10 @@ export class BarY extends AbstractBar {
114114
}
115115
}
116116

117-
export function barX(data, options) {
117+
export function barX(data, options = {y: indexOf, x2: identity}) {
118118
return new BarX(data, maybeStackX(maybeIntervalX(maybeIdentityX(options))));
119119
}
120120

121-
export function barY(data, options) {
121+
export function barY(data, options = {x: indexOf, y2: identity}) {
122122
return new BarY(data, maybeStackY(maybeIntervalY(maybeIdentityY(options))));
123123
}

test/marks/bar-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ it("barX() has the expected defaults", () => {
55
const bar = Plot.barX();
66
assert.strictEqual(bar.data, undefined);
77
// assert.strictEqual(bar.transform, undefined);
8-
assert.deepStrictEqual(bar.channels.map(c => c.name), ["x1", "x2"]);
8+
assert.deepStrictEqual(bar.channels.map(c => c.name), ["x1", "x2", "y"]);
99
// assert.deepStrictEqual(bar.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]);
10-
assert.deepStrictEqual(bar.channels.map(c => c.scale), ["x", "x"]);
10+
assert.deepStrictEqual(bar.channels.map(c => c.scale), ["x", "x", "y"]);
1111
assert.strictEqual(bar.fill, undefined);
1212
assert.strictEqual(bar.fillOpacity, undefined);
1313
assert.strictEqual(bar.stroke, undefined);
@@ -99,9 +99,9 @@ it("barY() has the expected defaults", () => {
9999
const bar = Plot.barY();
100100
assert.strictEqual(bar.data, undefined);
101101
// assert.strictEqual(bar.transform, undefined);
102-
assert.deepStrictEqual(bar.channels.map(c => c.name), ["y1", "y2"]);
102+
assert.deepStrictEqual(bar.channels.map(c => c.name), ["y1", "y2", "x"]);
103103
// assert.deepStrictEqual(bar.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]);
104-
assert.deepStrictEqual(bar.channels.map(c => c.scale), ["y", "y"]);
104+
assert.deepStrictEqual(bar.channels.map(c => c.scale), ["y", "y", "x"]);
105105
assert.strictEqual(bar.fill, undefined);
106106
assert.strictEqual(bar.fillOpacity, undefined);
107107
assert.strictEqual(bar.stroke, undefined);

test/plots/ordinal-bar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default async function() {
66
grid: true
77
},
88
marks: [
9-
Plot.barY("ABCDEF", {x: (d, i) => i, y2: d => d}),
9+
Plot.barY("ABCDEF"),
1010
Plot.ruleY([0])
1111
]
1212
});

0 commit comments

Comments
 (0)