Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Series className is appendend to legend class too #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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