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

Html dom building #6490

Merged
merged 12 commits into from
Sep 7, 2019
Merged
18 changes: 9 additions & 9 deletions src/controllers/controller.doughnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ defaults._set('doughnut', {
mode: 'single'
},
legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');

var list = document.createElement('ul');
var data = chart.data;
var datasets = data.datasets;
var labels = data.labels;
var i, ilen, listItem, listItemSpan;

list.setAttribute('class', chart.id + '-legend');
if (datasets.length) {
for (var i = 0; i < datasets[0].data.length; ++i) {
text.push('<li><span style="background-color:' + datasets[0].backgroundColor[i] + '"></span>');
for (i = 0, ilen = datasets[0].data.length; i < ilen; ++i) {
listItem = list.appendChild(document.createElement('li'));
listItemSpan = listItem.appendChild(document.createElement('span'));
listItemSpan.style.backgroundColor = datasets[0].backgroundColor[i];
if (labels[i]) {
text.push(labels[i]);
listItem.appendChild(document.createTextNode(labels[i]));
}
text.push('</li>');
}
}

text.push('</ul>');
return text.join('');
return list.outerHTML;
},
legend: {
labels: {
Expand Down
18 changes: 9 additions & 9 deletions src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ defaults._set('polarArea', {

startAngle: -0.5 * Math.PI,
legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');

var list = document.createElement('ul');
var data = chart.data;
var datasets = data.datasets;
var labels = data.labels;
var i, ilen, listItem, listItemSpan;

list.setAttribute('class', chart.id + '-legend');
if (datasets.length) {
for (var i = 0; i < datasets[0].data.length; ++i) {
text.push('<li><span style="background-color:' + datasets[0].backgroundColor[i] + '"></span>');
for (i = 0, ilen = datasets[0].data.length; i < ilen; ++i) {
listItem = list.appendChild(document.createElement('li'));
listItemSpan = listItem.appendChild(document.createElement('span'));
listItemSpan.style.backgroundColor = datasets[0].backgroundColor[i];
if (labels[i]) {
text.push(labels[i]);
listItem.appendChild(document.createTextNode(labels[i]));
}
text.push('</li>');
}
}

text.push('</ul>');
return text.join('');
return list.outerHTML;
},
legend: {
labels: {
Expand Down
23 changes: 14 additions & 9 deletions src/plugins/plugin.legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,22 @@ defaults._set('global', {
},

legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');
for (var i = 0; i < chart.data.datasets.length; i++) {
text.push('<li><span style="background-color:' + chart.data.datasets[i].backgroundColor + '"></span>');
if (chart.data.datasets[i].label) {
text.push(chart.data.datasets[i].label);
var list = document.createElement('ul');
var datasets = chart.data.datasets;
var i, ilen, listItem, listItemSpan;
benmccann marked this conversation as resolved.
Show resolved Hide resolved

list.setAttribute('class', chart.id + '-legend');

for (i = 0, ilen = datasets.length; i < ilen; i++) {
listItem = list.appendChild(document.createElement('li'));
listItemSpan = listItem.appendChild(document.createElement('span'));
listItemSpan.style.backgroundColor = datasets[i].backgroundColor;
if (datasets[i].label) {
listItem.appendChild(document.createTextNode(datasets[i].label));
}
text.push('</li>');
}
text.push('</ul>');
return text.join('');

return list.outerHTML;
}
});

Expand Down
4 changes: 2 additions & 2 deletions test/specs/global.defaults.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('Default Configs', function() {
options: config
});

var expectedLegend = '<ul class="' + chart.id + '-legend"><li><span style="background-color:red"></span>label1</li><li><span style="background-color:green"></span>label2</li></ul>';
var expectedLegend = '<ul class="' + chart.id + '-legend"><li><span style="background-color: red;"></span>label1</li><li><span style="background-color: green;"></span>label2</li></ul>';
expect(chart.generateLegend()).toBe(expectedLegend);
});

Expand Down Expand Up @@ -218,7 +218,7 @@ describe('Default Configs', function() {
options: config
});

var expectedLegend = '<ul class="' + chart.id + '-legend"><li><span style="background-color:red"></span>label1</li><li><span style="background-color:green"></span>label2</li></ul>';
var expectedLegend = '<ul class="' + chart.id + '-legend"><li><span style="background-color: red;"></span>label1</li><li><span style="background-color: green;"></span>label2</li></ul>';
expect(chart.generateLegend()).toBe(expectedLegend);
});

Expand Down