Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

labels on barX barY #82

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions src/marks/bar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ascending} from "d3-array";
import {create} from "d3-selection";
import {filter} from "../defined.js";
import {filter, nonempty} from "../defined.js";
import {Mark, number, maybeColor, maybeZero, indexOf, title} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles, applyTransform} from "../style.js";

Expand All @@ -13,6 +13,7 @@ export class AbstractBar extends Mark {
title,
fill,
stroke,
label,
insetTop = 0,
insetRight = 0,
insetBottom = 0,
Expand All @@ -29,6 +30,7 @@ export class AbstractBar extends Mark {
...channels,
{name: "z", value: z, optional: true},
{name: "title", value: title, optional: true},
{name: "label", value: label, optional: true},
{name: "fill", value: vfill, scale: "color", optional: true},
{name: "stroke", value: vstroke, scale: "color", optional: true}
],
Expand All @@ -42,23 +44,29 @@ export class AbstractBar extends Mark {
}
render(I, scales, channels, options) {
const {color} = scales;
const {z: Z, title: L, fill: F, stroke: S} = channels;
const {z: Z, title: T, label: L, fill: F, stroke: S} = channels;
const index = filter(I, ...this._positions(channels), F, S);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
const x = this._x(scales, channels, options);
const width = this._width(scales, channels, options);
const y = this._y(scales, channels, options);
const height = this._height(scales, channels, options);

return create("svg:g")
.call(applyIndirectStyles, this)
.call(this._transform, scales)
.call(g => g.selectAll()
.data(index)
.join("rect")
.call(applyDirectStyles, this)
.attr("x", this._x(scales, channels, options))
.attr("width", this._width(scales, channels, options))
.attr("y", this._y(scales, channels, options))
.attr("height", this._height(scales, channels, options))
.attr("x", x)
.attr("width", width)
.attr("y", y)
.attr("height", height)
.attr("fill", F && (i => color(F[i])))
.attr("stroke", S && (i => color(S[i])))
.call(title(L)))
.call(title(T)))
.call(this._label(L, index, {x, y, width, height}))
.node();
}
_x({x}, {x: X}, {marginLeft}) {
Expand All @@ -79,6 +87,7 @@ export class AbstractBar extends Mark {
const bandwidth = Y ? y.bandwidth() : height - marginTop - marginBottom;
return Math.max(0, bandwidth - insetTop - insetBottom);
}
_label() { return () => {}; }
}

export class BarX extends AbstractBar {
Expand Down Expand Up @@ -107,6 +116,23 @@ export class BarX extends AbstractBar {
const {insetLeft, insetRight} = this;
return i => Math.max(0, Math.abs(x(X2[i]) - x(X1[i])) - insetLeft - insetRight);
}
_label(L, index, {x, y, width, height}) {
const h = (typeof height === "function") ? height : () => height;
const w = (typeof width === "function") ? width : () => width;

return L ? g => {
g.selectAll("text")
.data(index.filter(i => nonempty(L[i])))
.join("text")
.text(i => L[i])
.attr("x", i => x(i) + w(i))
.attr("dx", i => w(i) < 20 ? 4 : -4)
.attr("text-anchor", i => w(i) < 20 ? "start" : "end")
.attr("y", i => y(i) + h(i) / 2)
.attr("dominant-baseline", "central")
.style("fill", i => w(i) < 20 ? "black" : "white");
} : () => {};
}
}

export class BarY extends AbstractBar {
Expand Down Expand Up @@ -135,6 +161,22 @@ export class BarY extends AbstractBar {
const {insetTop, insetBottom} = this;
return i => Math.max(0, Math.abs(y(Y2[i]) - y(Y1[i])) - insetTop - insetBottom);
}
_label(L, index, {x, y, width, height}) {
const h = (typeof height === "function") ? height : () => height;
const w = (typeof width === "function") ? width : () => width;

return L ? g => {
g.selectAll("text")
.data(index.filter(i => nonempty(L[i])))
.join("text")
.text(i => L[i])
.attr("x", i => x(i) + w(i) / 2)
.attr("text-anchor", "center")
.attr("y", i => y(i))
.attr("dy", i => h(i) < 20 ? -4 : 12)
.style("fill", i => h(i) < 20 ? "black" : "white");
} : () => {};
}
}

export function barX(data, {x, x1, x2, y = indexOf, ...options} = {}) {
Expand Down