Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/axis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {axisTop, axisBottom, axisRight, axisLeft, create, format, utcFormat} from "d3";
import {boolean, take, number, string, keyword, maybeKeyword, constant, isTemporal} from "./options.js";
import {formatIsoDate} from "./format.js";
import {boolean, take, number, string, keyword, maybeKeyword, constant, isTemporal} from "./mark.js";
import {radians} from "./math.js";
import {impliedString} from "./style.js";

Expand Down
60 changes: 60 additions & 0 deletions src/channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {ascending, descending, rollup, sort} from "d3";
import {first, labelof, maybeValue, range, valueof} from "./options.js";
import {registry} from "./scales/index.js";
import {maybeReduce} from "./transforms/group.js";

// TODO Type coercion?
export function Channel(data, {scale, type, value, hint}) {
return {
scale,
type,
value: valueof(data, value),
label: labelof(value),
hint
};
}

export function channelSort(channels, facetChannels, data, options) {
const {reverse: defaultReverse, reduce: defaultReduce = true, limit: defaultLimit} = options;
for (const x in options) {
if (!registry.has(x)) continue; // ignore unknown scale keys
const {value: y, reverse = defaultReverse, reduce = defaultReduce, limit = defaultLimit} = maybeValue(options[x]);
if (reduce == null || reduce === false) continue; // disabled reducer
const X = channels.find(([, {scale}]) => scale === x) || facetChannels && facetChannels.find(([, {scale}]) => scale === x);
if (!X) throw new Error(`missing channel for scale: ${x}`);
const XV = X[1].value;
const [lo = 0, hi = Infinity] = limit && typeof limit[Symbol.iterator] === "function" ? limit : limit < 0 ? [limit] : [0, limit];
if (y == null) {
X[1].domain = () => {
let domain = XV;
if (reverse) domain = domain.slice().reverse();
if (lo !== 0 || hi !== Infinity) domain = domain.slice(lo, hi);
return domain;
};
} else {
let YV;
if (y === "data") {
YV = data;
} else {
const Y = channels.find(([name]) => name === y);
if (!Y) throw new Error(`missing channel: ${y}`);
YV = Y[1].value;
}
const reducer = maybeReduce(reduce === true ? "max" : reduce, YV);
X[1].domain = () => {
let domain = rollup(range(XV), I => reducer.reduce(I, YV), i => XV[i]);
domain = sort(domain, reverse ? descendingGroup : ascendingGroup);
if (lo !== 0 || hi !== Infinity) domain = domain.slice(lo, hi);
return domain.map(first);
};
}
}
}

function ascendingGroup([ak, av], [bk, bv]) {
return ascending(av, bv) || ascending(ak, bk);
}

function descendingGroup([ak, av], [bk, bv]) {
return descending(av, bv) || ascending(ak, bk);
}
47 changes: 47 additions & 0 deletions src/dimensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {isOrdinalScale} from "./scales.js";
import {offset} from "./style.js";

export function Dimensions(
scales,
{
x: {axis: xAxis} = {},
y: {axis: yAxis} = {},
fx: {axis: fxAxis} = {},
fy: {axis: fyAxis} = {}
},
{
width = 640,
height = autoHeight(scales),
facet: {
margin: facetMargin,
marginTop: facetMarginTop = facetMargin !== undefined ? facetMargin : fxAxis === "top" ? 30 : 0,
marginRight: facetMarginRight = facetMargin !== undefined ? facetMargin : fyAxis === "right" ? 40 : 0,
marginBottom: facetMarginBottom = facetMargin !== undefined ? facetMargin : fxAxis === "bottom" ? 30 : 0,
marginLeft: facetMarginLeft = facetMargin !== undefined ? facetMargin : fyAxis === "left" ? 40 : 0
} = {},
margin,
marginTop = margin !== undefined ? margin : Math.max((xAxis === "top" ? 30 : 0) + facetMarginTop, yAxis || fyAxis ? 20 : 0.5 - offset),
marginRight = margin !== undefined ? margin : Math.max((yAxis === "right" ? 40 : 0) + facetMarginRight, xAxis || fxAxis ? 20 : 0.5 + offset),
marginBottom = margin !== undefined ? margin : Math.max((xAxis === "bottom" ? 30 : 0) + facetMarginBottom, yAxis || fyAxis ? 20 : 0.5 + offset),
marginLeft = margin !== undefined ? margin : Math.max((yAxis === "left" ? 40 : 0) + facetMarginLeft, xAxis || fxAxis ? 20 : 0.5 - offset)
} = {}
) {
return {
width,
height,
marginTop,
marginRight,
marginBottom,
marginLeft,
facetMarginTop,
facetMarginRight,
facetMarginBottom,
facetMarginLeft
};
}

function autoHeight({y, fy, fx}) {
const nfy = fy ? fy.scale.domain().length : 1;
const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length : Math.max(7, 17 / nfy)) : 1;
return !!(y || fy) * Math.max(1, Math.min(60, ny * nfy)) * 20 + !!fx * 30 + 60;
}
210 changes: 0 additions & 210 deletions src/facet.js

This file was deleted.

8 changes: 3 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export {plot} from "./plot.js";
export {Mark, marks, valueof} from "./mark.js";
export {plot, Mark, marks} from "./plot.js";
export {Area, area, areaX, areaY} from "./marks/area.js";
export {BarX, BarY, barX, barY} from "./marks/bar.js";
export {Cell, cell, cellX, cellY} from "./marks/cell.js";
Expand All @@ -13,9 +12,8 @@ export {RuleX, RuleY, ruleX, ruleY} from "./marks/rule.js";
export {Text, text, textX, textY} from "./marks/text.js";
export {TickX, TickY, tickX, tickY} from "./marks/tick.js";
export {Vector, vector} from "./marks/vector.js";
export {filter} from "./transforms/filter.js";
export {reverse} from "./transforms/reverse.js";
export {sort, shuffle} from "./transforms/sort.js";
export {valueof} from "./options.js";
export {filter, reverse, sort, shuffle} from "./transforms/basic.js";
export {bin, binX, binY} from "./transforms/bin.js";
export {group, groupX, groupY, groupZ} from "./transforms/group.js";
export {normalize, normalizeX, normalizeY} from "./transforms/normalize.js";
Expand Down
2 changes: 1 addition & 1 deletion src/legends.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {rgb} from "d3";
import {isObject} from "./options.js";
import {normalizeScale} from "./scales.js";
import {legendRamp} from "./legends/ramp.js";
import {legendSwatches, legendSymbols} from "./legends/swatches.js";
import {isObject} from "./mark.js";

const legendRegistry = new Map([
["color", legendColor],
Expand Down
2 changes: 1 addition & 1 deletion src/legends/swatches.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {create, path} from "d3";
import {inferFontVariant} from "../axes.js";
import {maybeTickFormat} from "../axis.js";
import {maybeColorChannel, maybeNumberChannel} from "../mark.js";
import {maybeColorChannel, maybeNumberChannel} from "../options.js";
import {applyInlineStyles, impliedString, maybeClassName, none} from "../style.js";

function maybeScale(scale, key) {
Expand Down
3 changes: 2 additions & 1 deletion src/marks/area.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {area as shapeArea, create, group} from "d3";
import {Curve} from "../curve.js";
import {defined} from "../defined.js";
import {Mark, indexOf, maybeZ} from "../mark.js";
import {Mark} from "../plot.js";
import {indexOf, maybeZ} from "../options.js";
import {applyDirectStyles, applyIndirectStyles, applyTransform, applyGroupedChannelStyles} from "../style.js";
import {maybeIdentityX, maybeIdentityY} from "../transforms/identity.js";
import {maybeStackX, maybeStackY} from "../transforms/stack.js";
Expand Down
3 changes: 2 additions & 1 deletion src/marks/bar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {create} from "d3";
import {filter} from "../defined.js";
import {Mark, number} from "../mark.js";
import {Mark} from "../plot.js";
import {number} from "../options.js";
import {isCollapsed} from "../scales.js";
import {applyDirectStyles, applyIndirectStyles, applyTransform, impliedString, applyAttr, applyChannelStyles} from "../style.js";
import {maybeIdentityX, maybeIdentityY} from "../transforms/identity.js";
Expand Down
2 changes: 1 addition & 1 deletion src/marks/cell.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {identity, indexOf, maybeColorChannel, maybeTuple} from "../mark.js";
import {identity, indexOf, maybeColorChannel, maybeTuple} from "../options.js";
import {AbstractBar} from "./bar.js";

export class Cell extends AbstractBar {
Expand Down
3 changes: 2 additions & 1 deletion src/marks/dot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {create, path, symbolCircle} from "d3";
import {filter, positive} from "../defined.js";
import {Mark, identity, maybeNumberChannel, maybeTuple} from "../mark.js";
import {Mark} from "../plot.js";
import {identity, maybeNumberChannel, maybeTuple} from "../options.js";
import {maybeSymbolChannel} from "../scales/symbol.js";
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransform, offset} from "../style.js";

Expand Down
Loading