Skip to content

Commit

Permalink
feat(api): Enhance .config() to return generation options
Browse files Browse the repository at this point in the history
Get generation options object from the API call

Fix #2864
  • Loading branch information
netil authored Sep 21, 2022
1 parent 49cf854 commit 455944e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
14 changes: 11 additions & 3 deletions src/Chart/api/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ export default {
},

/**
* Get or set single config option value.
* Get or set config option value.
* - **NOTE:** for without parameter occasion
* - will return all specified generation options object only. (will exclude any other options not specified at the initialization)
* @function config
* @instance
* @memberof Chart
Expand All @@ -152,9 +154,13 @@ export default {
* - **NOTE:** Doesn't guarantee work in all circumstances. It can be applied for limited options only.
* @returns {*}
* @example
*
* // Getter
* chart.config("gauge.max");
*
* // without any arguments, it returns generation config object
* chart.config(); // {data: { ... }, axis: { ... }, ...}
*
* // Setter
* chart.config("gauge.max", 100);
*
Expand All @@ -163,11 +169,11 @@ export default {
*/
config(name: string, value?: any, redraw?: boolean): any {
const $$ = this.internal;
const {config} = $$;
const {config, state} = $$;
const key = name?.replace(/\./g, "_");
let res;

if (key in config) {
if (name && key in config) {
if (isDefined(value)) {
config[key] = value;
res = value;
Expand All @@ -176,6 +182,8 @@ export default {
} else {
res = config[key];
}
} else {
res = state.orgConfig;
}

return res;
Expand Down
5 changes: 3 additions & 2 deletions src/ChartInternal/ChartInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,9 @@ export default class ChartInternal {
}

// Define g for chart area
main.append("g").attr("class", $COMMON.chart)
.attr("clip-path", state.clip.path);
main.append("g")
.classed($COMMON.chart, true)
.attr("clip-path", hasAxis ? state.clip.path : null);

$$.callPluginHook("$init");

Expand Down
1 change: 1 addition & 0 deletions src/config/Store/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export default class State {
hasPositiveValue: true,

orgAreaOpacity: "0.2",
orgConfig: {}, // user original genration config

// ID strings
hiddenTargetIds: <string[]> [],
Expand Down
8 changes: 7 additions & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
*/
import {isDefined, isObjectType} from "../module/util";
import Options from "./Options/Options";
import type {ChartOptions} from "../../types/options";

/**
* Load configuration option
* @param {object} config User's generation config value
* @private
*/
export function loadConfig(config: Options): void {
export function loadConfig(config: ChartOptions): void {
const thisConfig: Options = this.config;
let target;
let keys;
Expand Down Expand Up @@ -38,4 +39,9 @@ export function loadConfig(config: Options): void {
thisConfig[key] = read;
}
});

// only should run in the ChartInternal context
if (this.api) {
this.state.orgConfig = config;
}
}
8 changes: 6 additions & 2 deletions test/api/chart-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe("API chart", () => {

expect(bb.instance.indexOf(chart) === -1).to.be.true;

const el = document.getElementById("chart");
const el = <HTMLDivElement>document.getElementById("chart");

// should revert removing className and styles
expect(el.classList.contains("bb")).to.be.false;
Expand Down Expand Up @@ -221,7 +221,7 @@ describe("API chart", () => {

it("check for the axis config update", () => {
const axisYTick = chart.$.main.selectAll(`.${$AXIS.axisY} .tick`);
const expected = [];
const expected: {[key: string]: number}[] = [];

// axis y tick is outer
axisYTick.each(function() {
Expand Down Expand Up @@ -253,5 +253,9 @@ describe("API chart", () => {
expect(+tspan.getAttribute("x")).to.be.equal(Math.abs(expected[i].tspan));
});
});

it("should return generation options object.", () => {
expect(args).to.be.deep.equal(chart.config());
});
});
});
2 changes: 1 addition & 1 deletion test/plugin/sparkline/sparkline-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("PLUGIN: SPARKLINE", () => {

it("check for the dimension & tooltip", () => {
const last = document.querySelectorAll(selector)[2];
const {width, height} = last.querySelector(`.${$AREA.areas} path`).getBoundingClientRect();
const {width, height} = last.querySelector(`.${$AREA.areas} path`)?.getBoundingClientRect() ?? {width:0, height: 0};

// chart element should occupy the whole dimension of given size
expect({width, height}).to.be.deep.equal(args.size);
Expand Down
4 changes: 3 additions & 1 deletion types/chart.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,13 @@ export interface Chart {

/**
* Get or set single config option value.
* - **NOTE:**
* - without parameter, will return all specified generation options object only. (will exclude any other options not specified at the initialization)
* @param optionName The option key name.
* @param value The value accepted for indicated option.
* @param redraw Set to redraw with the new option changes. (NOTE: Doesn't guarantee work in all circumstances. It can be applied for limited options only)
*/
config(optionName: string, value?: any, redraw?: boolean): any;
config(optionName?: string, value?: any, redraw?: boolean): any;
}

export interface GridOperations {
Expand Down

0 comments on commit 455944e

Please sign in to comment.