Skip to content

Commit

Permalink
Dynamic positioning (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekis authored and SpaceK33z committed Nov 14, 2016
1 parent 3b3df7b commit 93cacda
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ new Chartist.Bar('.ct-chart', data, {
| `onClick` | Accepts a function that gets invoked if `clickable` is true. The function has the `chart`, and the click event (`e`), as arguments. | `mixed` | `false` |
| `classNames` | Accepts a array of strings as long as the chart's series, those will be added as classes to the `li` elements. | `mixed` | `false` |
| `removeAll` | Allow all series to be removed at once. | `bool` | `false` |
| `position` | Sets the position of the legend element. `top`and `bottom`are currently accepted. | `'top'|'bottom'` | `'top'` |
| `position` | Sets the position of the legend element. `top`, `bottom` or any DOM2 Element are currently accepted. If a DOM Element is given, the legend will be appended as it's last child. | `'top'|'bottom'|HTMLElement` | `'top'` |
35 changes: 26 additions & 9 deletions chartist-plugin-legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@

// Catch invalid options
if (options && options.position) {
if (!(options.position === 'top' || options.position === 'bottom')) {
if (!(options.position === 'top' || options.position === 'bottom' || options.position instanceof HTMLElement)) {
throw Error('The position you entered is not a valid position');
}
if(options.position instanceof HTMLElement){
// Detatch DOM element from options object, because Chartist.extend currently chokes on circular references present in HTMLElements
var cachedDOMPosition = options.position;
delete options.position;
}
}

options = Chartist.extend({}, defaultOptions, options);

if(cachedDOMPosition){
// Reattatch the DOM Element position if it was removed before
options.position = cachedDOMPosition
}

return function legend(chart) {
var existingLegendElement = chart.container.querySelector('.ct-legend');
if (existingLegendElement) {
Expand Down Expand Up @@ -109,14 +119,21 @@

chart.on('created', function (data) {
// Append the legend element to the DOM
switch (options.position) {
case 'top':
chart.container.insertBefore(legendElement, chart.container.childNodes[0]);
break;

case 'bottom':
chart.container.insertBefore(legendElement, null);
break;
if(!(options.position instanceof HTMLElement))
{
switch (options.position) {
case 'top':
chart.container.insertBefore(legendElement, chart.container.childNodes[0]);
break;

case 'bottom':
chart.container.insertBefore(legendElement, null);
break;
}
}
else {
// Appends the legend element as the last child of a given HTMLElement
options.position.insertBefore(legendElement, null);
}
});

Expand Down
20 changes: 20 additions & 0 deletions test/test.legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,26 @@ describe('Chartist plugin legend', function() {
}, 10)
});
});

it('should allow positioning to any DOM2 element', function (done) {
var testDOMElement = document.createElement('div');
document.body.insertBefore(testDOMElement , null);

chart = generateChart('Line', chartDataLine, { position: testDOMElement });

// Set a delay on the test to ensure it doesn't overlap with the plugin native 'created' handler
chart.on('created', function () {
setTimeout(function () {
expect(testDOMElement.childNodes.length).to.equal(1);
var listElement = testDOMElement.querySelector("ul");
expect(testDOMElement.childNodes[0]).to.equal(listElement);

// Clean up
document.body.removeChild(testDOMElement);
done();
}, 100)
});
});
});

it('should allow to remove all series at once', function () {
Expand Down

0 comments on commit 93cacda

Please sign in to comment.