Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ChartInternal,Options): Add before/after initialization callbacks #126

Merged
merged 4 commits into from
Aug 29, 2017
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
39 changes: 39 additions & 0 deletions spec/core-spec.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,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 @@ -232,6 +245,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 @@ -2257,4 +2283,3 @@ export default class Options {
};
}
}

12 changes: 11 additions & 1 deletion src/internals/ChartInternal.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "d3";
import Axis from "../axis/Axis";
import CLASS from "../config/classes";
import {addEvent, notEmpty, asHalfPixel, isValue, getOption} from "./util";
import {addEvent, notEmpty, asHalfPixel, isValue, getOption, isFunction} from "./util";

/**
* Internal chart class.
Expand All @@ -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