Skip to content

Commit

Permalink
feat(Chart): Expose primary node elements (#511)
Browse files Browse the repository at this point in the history
- Intent to ship primary node elements
- Build api doc without entire building

Fix #423
Close #511
  • Loading branch information
netil authored Jul 27, 2018
1 parent a3b2fb1 commit de3f60c
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ COMMIT_MESSAGE="skip:$VERSION-release"
rm -rf release && mkdir release && cd release && mkdir $VERSION latest && cd ..

# build & copy to release path
npm run jsdoc && cp -r doc dist release/latest/ && cp -r doc dist release/$VERSION/
npm run build && npm run jsdoc && cp -r doc dist release/latest/ && cp -r doc dist release/$VERSION/

# push
npx gh-pages --dist $DIST_FOLDER --dest $DEST_FOLDER --add --remote $DEST_BRANCH --message $COMMIT_MESSAGE
3 changes: 2 additions & 1 deletion jsdoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
},
"source": {
"include": [
"dist/billboard.js",
"src",
"./README.md"
],
"includePattern": ".+\\.js$",
"excludePattern": "(node_modules/|doc/|theme/)"
},
"plugins": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test:chrome": "npm test -- --chrome",
"coverage": "npm test -- --coverage",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"jsdoc": "npm run build && jsdoc -c jsdoc.json",
"jsdoc": "jsdoc -c jsdoc.json",
"changelog": "node ./config/changelog.js",
"commitmsg": "node ./config/validate-commit-msg.js",
"prepush": "npm run lint"
Expand Down
24 changes: 21 additions & 3 deletions spec/internals/bb-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import bb from "../../src/core";
import util from "../assets/util";

describe("Interface & initialization", () => {
let chart;

it("Check for billboard.js object", () => {
expect(bb).not.to.be.null;
expect(typeof bb).to.be.equal("object");
expect(typeof bb.generate).to.be.equal("function");
});

it("Check for initialization", () => {
const chart = util.generate({
chart = util.generate({
data: {
columns: [
["data1", 30]
Expand All @@ -38,9 +40,25 @@ describe("Interface & initialization", () => {
expect(bb.version.length > 0).to.be.ok;
});

it("should be accessing node elements", () => {
const isD3Node = v => v && "node" in v || typeof v === "undefined";

Object.values(chart.$).forEach(v1 => {
const isNode = isD3Node(v1);

if (isNode) {
expect(isNode).to.be.true;
} else {
Object.values(v1).forEach(v2 => {
expect(isD3Node(v2)).to.be.true;
});
}
});
});

it("instantiate with different classname on wrapper element", () => {
const bindtoClassName = "billboard-js";
const chart = bb.generate({
chart = bb.generate({
bindto: {
element: "#chart",
classname: bindtoClassName
Expand Down Expand Up @@ -108,7 +126,7 @@ describe("Interface & initialization", () => {

container.innerHTML = '<div id="chartResize"></div>';

const chart = util.generate({
chart = util.generate({
bindto: "#chartResize",
data: {
columns: [
Expand Down
1 change: 1 addition & 0 deletions src/api/api.axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const getMinMax = ($$, type) => {

/**
* Define axis
* @ignore
*/
const axis = extend(() => {}, {
/**
Expand Down
1 change: 1 addition & 0 deletions src/api/api.legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {extend} from "../internals/util";

/**
* Define legend
* @ignore
*/
const legend = extend(() => {}, {
/**
Expand Down
1 change: 1 addition & 0 deletions src/api/api.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {isValue, isDefined, extend} from "../internals/util";

/**
* Define tooltip
* @ignore
*/
const tooltip = extend(() => {}, {
/**
Expand Down
6 changes: 4 additions & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ const bb = {
* @memberOf bb
*/
version: "#__VERSION__#",

/**
* generate charts
* Generate chart
* @param {Options} options chart options
* @memberOf bb
* @return {Chart}
Expand Down Expand Up @@ -51,11 +52,11 @@ const bb = {
const inst = new Chart(config);

inst.internal.charts = this.instance;

this.instance.push(inst);

return inst;
},

/**
* An array containing instance created
* @property {Array} instance instance array
Expand All @@ -68,6 +69,7 @@ const bb = {
* @memberOf bb
*/
instance: [],

/**
* Internal chart object
* @private
Expand Down
33 changes: 33 additions & 0 deletions src/internals/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ export default class Chart {
$$.loadConfig(config);
$$.beforeInit(config);
$$.init();

/**
* Access primary node elements
* @name Chart#$
* @type Object
* @property {Object} $
* @property {d3.selection} $.chart Wrapper element
* @property {d3.selection} $.svg Main svg element
* @property {d3.selection} $.defs Definition element
* @property {d3.selection} $.main Main grouping element
* @property {d3.selection} $.tooltip Tooltip element
* @property {d3.selection} $.legend Legend element
* @property {d3.selection} $.title Title element
* @property {d3.selection} $.grid Grid element
* @property {d3.selection} $.arc Arc element
* @property {Object} $.bar
* @property {d3.selection} $.bar.bars Bar elements
* @property {Object} $.line
* @property {d3.selection} $.line.lines Line elements
* @property {d3.selection} $.line.areas Areas elements
* @property {d3.selection} $.line.circles Data point circle elements
* @property {Object} $.text
* @property {d3.selection} $.text.texts Data label text elements
* @instance
* @memberOf Chart
* @example
* var chart = bb.generate({ ... });
*
* chart.$.chart; // wrapper element
* chart.$.line.circles; // all data point circle elements
*/
this.$ = $$.getChartElements();

$$.afterInit(config);

// bind "this" to nested API
Expand Down
27 changes: 27 additions & 0 deletions src/internals/ChartInternal.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,33 @@ export default class ChartInternal {
});
}

getChartElements() {
const $$ = this;

return {
chart: $$.selectChart,
svg: $$.svg,
defs: $$.defs,
main: $$.main,
tooltip: $$.tooltip,
legend: $$.legend,
title: $$.title,
grid: $$.grid,
arc: $$.arcs,
bar: {
bars: $$.mainBar
},
line: {
lines: $$.mainLine,
areas: $$.mainArea,
circles: $$.mainCircle
},
text: {
texts: $$.texts
}
};
}

smoothLines(el, type) {
if (type === "grid") {
el.each(function() {
Expand Down

0 comments on commit de3f60c

Please sign in to comment.