Skip to content

Commit

Permalink
Merge 45f6c97 into bc14a5c
Browse files Browse the repository at this point in the history
  • Loading branch information
matteoraf committed May 16, 2020
2 parents bc14a5c + 45f6c97 commit 9dbb4dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
9 changes: 7 additions & 2 deletions chartist-plugin-legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,17 @@
return seriesMetadata;
}

function createNameElement(i, legendText, classNamesViable) {
function createNameElement(i, legendText, classNamesViable, seriesClassName) {
var li = document.createElement('li');
li.classList.add('ct-series-' + i);
// Append specific class to a legend element, if viable classes are given
if (classNamesViable) {
li.classList.add(options.classNames[i]);
}
// Append the series className too, if given
if (seriesClassName) {
li.classList.add(seriesClassName);
}
li.setAttribute('data-legend', i);
li.textContent = legendText;
return li;
Expand Down Expand Up @@ -206,8 +210,9 @@
legendNames.forEach(function (legend, i) {
var legendText = legend.name || legend;
var legendSeries = legend.series || [i];
var legendClassName = legend.className ? legend.className : null;

var li = createNameElement(i, legendText, classNamesViable);
var li = createNameElement(i, legendText, classNamesViable, legendClassName);
legendElement.appendChild(li);

legendSeries.forEach(function(seriesIndex) {
Expand Down
25 changes: 14 additions & 11 deletions test/test.legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,10 @@ describe('Chartist plugin legend', function() {
// The first click should hide the corresponding series.
click(seriesB);
var legendItems = chart.container.querySelectorAll('ul.ct-legend > li');
expect(legendItems[0].className).to.equal('ct-series-0');
expect(legendItems[1].className).to.equal('ct-series-1 inactive');
expect(legendItems[2].className).to.equal('ct-series-2');
expect(legendItems[0].className).to.contain('ct-series-0');
expect(legendItems[1].className).to.contain('ct-series-1');
expect(legendItems[1].className).to.contain('inactive');
expect(legendItems[2].className).to.contain('ct-series-2');

// A second click should show the corresponding series again.
click(seriesB);
Expand All @@ -421,9 +422,11 @@ describe('Chartist plugin legend', function() {
click(seriesA);
click(seriesB);
var legendItems = chart.container.querySelectorAll('ul.ct-legend > li');
expect(legendItems[0].className).to.equal('ct-series-0 inactive');
expect(legendItems[1].className).to.equal('ct-series-1 inactive');
expect(legendItems[2].className).to.equal('ct-series-2');
expect(legendItems[0].className).to.contain('ct-series-0');
expect(legendItems[0].className).to.contain('inactive');
expect(legendItems[1].className).to.contain('ct-series-1');
expect(legendItems[1].className).to.contain('inactive');
expect(legendItems[2].className).to.contain('ct-series-2');
click(seriesC);
var inactiveItem = chart.container.querySelectorAll('ul.ct-legend > li.inactive');
expect(inactiveItem.length).to.equal(0);
Expand All @@ -435,14 +438,14 @@ describe('Chartist plugin legend', function() {

click(seriesB);
expect(chart.legendClicked).to.equal(true);

//Clicking on an inactive series should also call the function.
chart.legendClicked = false;
click(seriesB);
expect(chart.legendClicked).to.equal(true);
});
});

describe('clickable with multiple series per legend item', function() {
before(function(done) {
chart = generateChart('Line', chartDataLine, {
Expand Down Expand Up @@ -490,7 +493,7 @@ describe('Chartist plugin legend', function() {
expect(svgSeries2[0].className.baseVal).to.contain('ct-series-a');
expect(svgSeries2[1].className.baseVal).to.contain('ct-series-b');
expect(svgSeries2[2].className.baseVal).to.contain('ct-series-c');

// Clicking on the first legend item should hide the two first series:
click(seriesA);
expect(chart.data.series.length).to.equal(1);
Expand Down Expand Up @@ -549,14 +552,14 @@ describe('Chartist plugin legend', function() {

click(seriesB);
expect(chart.legendClicked).to.equal(true);

//Clicking on an inactive series should also call the function.
chart.legendClicked = false;
click(seriesB);
expect(chart.legendClicked).to.equal(true);
});
});

describe('clickable for a pie', function() {
before(function(done) {
chart = generateChart('Pie', chartDataPie, {
Expand Down

0 comments on commit 9dbb4dd

Please sign in to comment.