Skip to content

Commit

Permalink
feat(api): make return values for axis.labels()
Browse files Browse the repository at this point in the history
The API description states return values, but it wasn't.
Make return axis' label text object when .axis.labels() is called.

Fix #1865
  • Loading branch information
netil authored Jan 14, 2021
1 parent 865224e commit efa5174
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
24 changes: 22 additions & 2 deletions src/Chart/api/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,44 @@ const axis = {
* @param {string} [labels.x] x Axis string
* @param {string} [labels.y] y Axis string
* @param {string} [labels.y2] y2 Axis string
* @returns {object|undefined} axis labels text object
* @example
* // Update axis' label
* chart.axis.labels({
* x: "New X Axis Label",
* y: "New Y Axis Label",
* y2: "New Y2 Axis Label"
* });
*
* chart.axis.labels();
* // --> {
* // x: "New X Axis Label",
* // y: "New Y Axis Label",
* // y2: "New Y2 Axis Label"
* // }
*/
labels: function(labels: {x?: string, y?: string, y2?: string}): void {
labels: function<T>(labels?: {x?: string, y?: string, y2?: string}): T | undefined {
const $$ = this.internal;
let labelText;

if (arguments.length) {
if (labels) {
Object.keys(labels).forEach(axisId => {
$$.axis.setLabelText(axisId, labels[axisId]);
});

$$.axis.updateLabels();
}

["x", "y", "y2"].forEach(v => {
const text = $$.axis.getLabelText(v);

if (text) {
!labelText && (labelText = {});
labelText[v] = text;
}
});

return <T>labelText;
},

/**
Expand Down
22 changes: 19 additions & 3 deletions test/api/axis-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {expect} from "chai";
import {select as d3Select} from "d3-selection";
import util from "../assets/util";
import CLASS from "../../src/config/classes";
import axis from "../../src/Chart/api/axis";

describe("API axis", function() {
const chart = util.generate({
Expand All @@ -22,7 +23,6 @@ describe("API axis", function() {
},
axis: {
y: {
label: "Y Axis Label",
padding: {
bottom: 0
}
Expand All @@ -38,9 +38,12 @@ describe("API axis", function() {

describe("axis.labels()", () => {
it("should update y axis label", done => {
chart.axis.labels({
const axisLabel = {
y: "New Y Axis Label"
});
};

// when
const labels = chart.axis.labels(axisLabel);

setTimeout(() => {
const label = main.select(`.${CLASS.axisYLabel}`);
Expand All @@ -49,11 +52,17 @@ describe("API axis", function() {
expect(label.attr("dx")).to.be.equal("-0.5em");
expect(label.attr("dy")).to.be.equal("1.2em");

expect(labels).to.be.deep.equal({
y: axisLabel.y,
y2: "Y2 Axis Label"
});

done();
}, 500);
});

it("should update y axis label", done => {
// when
chart.axis.labels({
y2: "New Y2 Axis Label"
});
Expand All @@ -68,6 +77,13 @@ describe("API axis", function() {
done();
}, 500);
});

it("should return axis labels", () => {
expect(chart.axis.labels()).to.be.deep.equal({
y: 'New Y Axis Label',
y2: 'New Y2 Axis Label'
});
});
});

describe("axis.min/max()", () => {
Expand Down

0 comments on commit efa5174

Please sign in to comment.