diff --git a/src/controllers/controller.doughnut.js b/src/controllers/controller.doughnut.js
index de418e7edbc..7cee5444457 100644
--- a/src/controllers/controller.doughnut.js
+++ b/src/controllers/controller.doughnut.js
@@ -22,25 +22,25 @@ defaults._set('doughnut', {
mode: 'single'
},
legendCallback: function(chart) {
- var text = [];
- text.push('
');
-
+ 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('- ');
+ 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('
');
}
}
- text.push('
');
- return text.join('');
+ return list.outerHTML;
},
legend: {
labels: {
diff --git a/src/controllers/controller.polarArea.js b/src/controllers/controller.polarArea.js
index 1e0e30ba2dd..76ca3b70cae 100644
--- a/src/controllers/controller.polarArea.js
+++ b/src/controllers/controller.polarArea.js
@@ -32,25 +32,25 @@ defaults._set('polarArea', {
startAngle: -0.5 * Math.PI,
legendCallback: function(chart) {
- var text = [];
- text.push('');
-
+ 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('- ');
+ 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('
');
}
}
- text.push('
');
- return text.join('');
+ return list.outerHTML;
},
legend: {
labels: {
diff --git a/src/plugins/plugin.legend.js b/src/plugins/plugin.legend.js
index 91ff07a20bd..dc5b9325d6f 100644
--- a/src/plugins/plugin.legend.js
+++ b/src/plugins/plugin.legend.js
@@ -78,17 +78,22 @@ defaults._set('global', {
},
legendCallback: function(chart) {
- var text = [];
- text.push('');
- for (var i = 0; i < chart.data.datasets.length; i++) {
- text.push('- ');
- 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;
+
+ 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('
');
}
- text.push('
');
- return text.join('');
+
+ return list.outerHTML;
}
});
diff --git a/test/specs/global.defaults.tests.js b/test/specs/global.defaults.tests.js
index a01284c1a0b..afef1aabf2a 100644
--- a/test/specs/global.defaults.tests.js
+++ b/test/specs/global.defaults.tests.js
@@ -102,7 +102,7 @@ describe('Default Configs', function() {
options: config
});
- var expectedLegend = '';
+ var expectedLegend = '';
expect(chart.generateLegend()).toBe(expectedLegend);
});
@@ -218,7 +218,7 @@ describe('Default Configs', function() {
options: config
});
- var expectedLegend = '';
+ var expectedLegend = '';
expect(chart.generateLegend()).toBe(expectedLegend);
});