From 3afc1585fe3171efc17d3bdebd401ff68051127a Mon Sep 17 00:00:00 2001 From: Dave Landry Date: Mon, 30 Oct 2023 11:47:46 -0400 Subject: [PATCH] adds buffer method for toggling axis behavior --- src/Plot.js | 41 +++++++++++++++++++++++++++++++++-------- src/buffers/Bar.js | 3 --- src/buffers/Box.js | 3 --- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/Plot.js b/src/Plot.js index e983c1d..5e3e00d 100644 --- a/src/Plot.js +++ b/src/Plot.js @@ -14,11 +14,19 @@ const testLineShape = new shapes.Line(); const testTextBox = new TextBox(); import {Viz} from "d3plus-viz"; +import {default as discreteBuffer} from "./buffers/discreteBuffer.js"; import {default as BarBuffer} from "./buffers/Bar.js"; import {default as BoxBuffer} from "./buffers/Box.js"; import {default as CircleBuffer} from "./buffers/Circle.js"; import {default as LineBuffer} from "./buffers/Line.js"; import {default as RectBuffer} from "./buffers/Rect.js"; +const defaultBuffers = { + Bar: BarBuffer, + Box: BoxBuffer, + Circle: CircleBuffer, + Line: LineBuffer, + Rect: RectBuffer +}; /** @desc Logic for determining default sizes of shapes using the sizeScaleD3 internal function. @@ -135,13 +143,7 @@ export default class Plot extends Viz { fill: "transparent" }; this._barPadding = 0; - this._buffer = { - Bar: BarBuffer, - Box: BoxBuffer, - Circle: CircleBuffer, - Line: LineBuffer, - Rect: RectBuffer - }; + this._buffer = assign({}, defaultBuffers, {Bar: false, Line: false}); this._confidenceConfig = { fill: (d, i) => { const c = typeof this._shapeConfig.Line.stroke === "function" @@ -667,6 +669,9 @@ export default class Plot extends Viz { .entries(axisData); allShapeData.forEach(d => { + if (["Bar", "Box"].includes(d.key)) { + discreteBuffer(this._discrete === "x" ? x : y, data, this._discrete); + } if (this._buffer[d.key]) { const res = this._buffer[d.key].bind(this)({data: d.values, x, y, yScale: yConfigScale, xScale: xConfigScale, config: this._shapeConfig[d.key]}); x = res[0]; @@ -734,7 +739,6 @@ export default class Plot extends Viz { ? typeof barConfig.label === "function" ? barConfig.label : constant(barConfig.label) : this._drawLabel; const barLabels = axisData.map(d => barLabelFunction(d.data, d.i)).filter(d => typeof d === "number" || d).map(String); - console.log(axisData.map(d => this._y(d.data)), barLabels, yScale); // sets an axis' ticks to [] if the axis scale is "Point" (discrete) and every tick String // is also in the barLabels Array @@ -1351,6 +1355,27 @@ export default class Plot extends Viz { return arguments.length ? (this._baseline = _, this) : this._baseline; } + /** + @memberof Plot + @desc Determines whether or not to add additional padding at the ends of x or y scales. The most commone use for this is in Scatter Plots, so that the shapes do not appear directly on the axis itself. The value provided can either be `true` or `false` to toggle the behavior for all shape types, or a keyed Object for each shape type (ie. `{Bar: false, Circle: true, Line: false}`). + @param {Object|Boolean} [*value*] + @chainable + */ + buffer(_) { + if (arguments.length) { + if (!_) this._buffer = {}; + else if (_ === true) this._buffer = defaultBuffers; + else { + this._buffer = assign({}, this._buffer, _); + for (let key in this._buffer) { + if (this._buffer[key] === true) this._buffer[key] = defaultBuffers[key]; + } + } + return this; + } + return this._buffer; + } + /** @memberof Plot @desc Sets the confidence to the specified array of lower and upper bounds. diff --git a/src/buffers/Bar.js b/src/buffers/Bar.js index a5db3e4..10e0389 100644 --- a/src/buffers/Bar.js +++ b/src/buffers/Bar.js @@ -1,6 +1,5 @@ import {max, min, sum} from "d3-array"; import {nest} from "d3-collection"; -import discreteBuffer from "./discreteBuffer.js"; /** Adds a buffer to either side of the non-discrete axis. @@ -53,8 +52,6 @@ export default function({data, x, y, x2, y2, buffer = 10}) { oppScale.domain(oppDomain); - discreteBuffer(isDiscreteX ? x : y, data, this._discrete); - return [x, y]; } diff --git a/src/buffers/Box.js b/src/buffers/Box.js index 0109d58..0b19240 100644 --- a/src/buffers/Box.js +++ b/src/buffers/Box.js @@ -1,6 +1,5 @@ import {max, min, sum} from "d3-array"; import {nest} from "d3-collection"; -import discreteBuffer from "./discreteBuffer.js"; /** Adds a buffer to either side of the non-discrete axis. @@ -52,8 +51,6 @@ export default function({data, x, y, x2, y2, buffer = 10}) { oppScale.domain(oppDomain); - discreteBuffer(isDiscreteX ? x : y, data, this._discrete); - return [x, y]; }