Skip to content

Commit

Permalink
feat(ChartInternal,Options): Add before/after initialization callbacks
Browse files Browse the repository at this point in the history
Add two new callbacks for before and after initialization.

Fix #55
Close #126
  • Loading branch information
markwinter authored and netil committed Aug 29, 2017
1 parent dfd2f7d commit 354d7e2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
39 changes: 39 additions & 0 deletions spec/core-spec.js
Expand Up @@ -50,6 +50,45 @@ describe("CORE", function() {
});
});

describe("init callbacks", () => {
let oninit = false;
let onbeforeinit = false;
let onafterinit = false;

before(() => {
args = {
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250],
["data2", 50, 20, 10, 40, 15, 25],
["data3", 150, 120, 110, 140, 115, 125]
]
},
oninit: () => {
oninit = true;
},
onbeforeinit: () => {
onbeforeinit = true;
},
onafterinit: () => {
onafterinit = true;
}
}
});

it("check for oninit callback", () => {
expect(oninit).to.be.true;
});

it("check for onbeforeinit callback", () => {
expect(onbeforeinit).to.be.true;
});

it("check for onafterinit callback", () => {
expect(onafterinit).to.be.true;
});
});

describe("size", () => {
it("should have same width", () => {
const svg = d3.select("#chart svg");
Expand Down
27 changes: 26 additions & 1 deletion src/config/Options.js
Expand Up @@ -233,6 +233,19 @@ export default class Options {
*/
onresized: () => {},

/**
* Set a callback to execute before the chart is initialized
* @name onbeforeinit
* @memberof Options
* @type {Function}
* @default function(){}
* @example
* onbeforeinit: function() {
* ...
* }
*/
onbeforeinit: undefined,

/**
* Set a callback to execute when the chart is initialized.
* @name oninit
Expand All @@ -246,6 +259,19 @@ export default class Options {
*/
oninit: () => {},

/**
* Set a callback to execute after the chart is initialized
* @name onafterinit
* @memberof Options
* @type {Function}
* @default function(){}
* @example
* onafterinit: function() {
* ...
* }
*/
onafterinit: undefined,

/**
* Set a callback which is executed when the chart is rendered. Basically, this callback will be called in each time when the chart is redrawed.
* @name onrendered
Expand Down Expand Up @@ -2299,4 +2325,3 @@ export default class Options {
};
}
}

10 changes: 10 additions & 0 deletions src/internals/ChartInternal.js
Expand Up @@ -35,11 +35,21 @@ export default class ChartInternal {
}

beforeInit() {
const $$ = this;
const config = $$.config;

// can do something

isFunction(config.onbeforeinit) && config.onbeforeinit.call($$);
}

afterInit() {
const $$ = this;
const config = $$.config;

// can do something

isFunction(config.onafterinit) && config.onafterinit.call($$);
}

init() {
Expand Down

0 comments on commit 354d7e2

Please sign in to comment.