Skip to content

Commit

Permalink
add implementation for #28
Browse files Browse the repository at this point in the history
as of #07e57b4e9feba18d586cde124e7a1d76a0841aab
  • Loading branch information
tarekis committed Nov 8, 2016
1 parent 07e57b4 commit 6ec9d18
Showing 1 changed file with 26 additions and 9 deletions.
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, as circular references will cause breaking memory leaks
var cachedDOMPosition = options.position;
delete options.position;
}
}

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

if(cachedDOMPosition){
// Reattatch the DOM position
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

0 comments on commit 6ec9d18

Please sign in to comment.