rawgit.com/CodeYellowBV/chartist-plugin-legend/master/chartist-plugin-legend.js VS unpkg.com/chartist-plugin-legend@0.6.2/chartist-plugin-legend.js

Créé Le diff n'expire jamais
158 suppressions
237 lignes
100 ajouts
217 lignes
(function (root, factory) {
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
// AMD. Register as an anonymous module.
define(['chartist'], function (chartist) {
define(['chartist'], function (chartist) {
return (root.returnExportsGlobal = factory(chartist));
return (root.returnExportsGlobal = factory(chartist));
});
});
} else if (typeof exports === 'object') {
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// only CommonJS-like enviroments that support module.exports,
// like Node.
// like Node.
module.exports = factory(require('chartist'));
module.exports = factory(require('chartist'));
} else {
} else {
root['Chartist.plugins.legend'] = factory(root.Chartist);
root['Chartist.plugins.legend'] = factory(root.Chartist);
}
}
}(this, function (Chartist) {
}(this, function (Chartist) {
/**
/**
* This Chartist plugin creates a legend to show next to the chart.
* This Chartist plugin creates a legend to show next to the chart.
*
*
*/
*/
'use strict';
'use strict';


var defaultOptions = {
var defaultOptions = {
className: '',
className: '',
classNames: false,
classNames: false,
removeAll: false,
removeAll: false,
legendNames: false,
legendNames: false,
clickable: true,
clickable: true,
onClick: null,
onClick: null,
position: 'top'
position: 'top'
};
};


Chartist.plugins = Chartist.plugins || {};
Chartist.plugins = Chartist.plugins || {};


Chartist.plugins.legend = function (options) {
Chartist.plugins.legend = function (options) {

function compareNumbers(a, b) {
return a - b;
}


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


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


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


return function legend(chart) {
return function legend(chart) {

function removeLegendElement() {
var legendElement = chart.container.querySelector('.ct-legend');
var existingLegendElement = chart.container.querySelector('.ct-legend');
if (legendElement) {
if (existingLegendElement) {
// Clear legend if already existing.
legendElement.parentNode.removeChild(legendElement);
existingLegendElement.parentNode.removeChild(existingLegendElement);
}
}
}


// Set a unique className for each series so that when a series is removed,
// Set a unique className for each series so that when a series is removed,
// the other series still have the same color.
// the other series still have the same color.
function setSeriesClassNames() {
if (options.clickable) {
chart.data.series = chart.data.series.map(function (series, seriesIndex) {
var newSeries = chart.data.series.map(function (series, seriesIndex) {
if (typeof series !== 'object') {
if (typeof series !== 'object') {
series = {
series = {
value: series
value: series
};
};
}
}

series.className = series.className || chart.options.classNames.series + '-' + Chartist.alphaNumerate(seriesIndex);
series.className = series.className || chart.options.classNames.series + '-' + Chartist.alphaNumerate(seriesIndex);
return series;
return series;
});
});
chart.data.series = newSeries;
}
}


function createLegendElement() {
var legendElement = document.createElement('ul');
var legendElement = document.createElement('ul'),
isPieChart = chart instanceof Chartist.Pie;
legendElement.className = 'ct-legend';
legendElement.className = 'ct-legend';
if (chart instanceof Chartist.Pie) {
if (chart instanceof Chartist.Pie) {
legendElement.classList.add('ct-legend-inside');
legendElement.classList.add('ct-legend-inside');
}
}
if (typeof options.className === 'string' && options.className.length > 0) {
if (typeof options.className === 'string' && options.className.length > 0) {
legendElement.classList.add(options.className);
legendElement.classList.add(options.className);
}
}

if (chart.options.width) {
if (chart.options.width) {
legendElement.style.cssText = 'width: ' + chart.options.width + 'px;margin: 0 auto;';
legendElement.style.cssText = 'width: ' + chart.options.width + 'px;margin: 0 auto;';
}
}