FIX: Render HTML for pie chart options (#8912)
This commit is contained in:
parent
b34f09f8d7
commit
daaa0a657f
|
@ -518,8 +518,7 @@ createWidget("discourse-poll-grouped-pies", {
|
||||||
const data = result.grouped_results[chartIdx].options.mapBy("votes");
|
const data = result.grouped_results[chartIdx].options.mapBy("votes");
|
||||||
const labels = result.grouped_results[chartIdx].options.mapBy("html");
|
const labels = result.grouped_results[chartIdx].options.mapBy("html");
|
||||||
const chartConfig = pieChartConfig(data, labels, {
|
const chartConfig = pieChartConfig(data, labels, {
|
||||||
aspectRatio: 1.2,
|
aspectRatio: 1.2
|
||||||
displayLegend: false
|
|
||||||
});
|
});
|
||||||
const canvasId = `pie-${attrs.id}-${chartIdx}`;
|
const canvasId = `pie-${attrs.id}-${chartIdx}`;
|
||||||
let el = document.querySelector(`#${canvasId}`);
|
let el = document.querySelector(`#${canvasId}`);
|
||||||
|
@ -579,7 +578,10 @@ createWidget("discourse-poll-pie-canvas", {
|
||||||
|
|
||||||
const el = document.getElementById(`poll-results-chart-${attrs.id}`);
|
const el = document.getElementById(`poll-results-chart-${attrs.id}`);
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
new Chart(el.getContext("2d"), config);
|
let chart = new Chart(el.getContext("2d"), config);
|
||||||
|
document.getElementById(
|
||||||
|
`poll-results-legend-${attrs.id}`
|
||||||
|
).innerHTML = chart.generateLegend();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -620,13 +622,14 @@ createWidget("discourse-poll-pie-chart", {
|
||||||
}
|
}
|
||||||
contents.push(btn);
|
contents.push(btn);
|
||||||
contents.push(chart);
|
contents.push(chart);
|
||||||
|
contents.push(h(`div#poll-results-legend-${attrs.id}.pie-chart-legends`));
|
||||||
return contents;
|
return contents;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function pieChartConfig(data, labels, opts = {}) {
|
function pieChartConfig(data, labels, opts = {}) {
|
||||||
const aspectRatio = "aspectRatio" in opts ? opts.aspectRatio : 2.0;
|
const aspectRatio = "aspectRatio" in opts ? opts.aspectRatio : 2.2;
|
||||||
const displayLegend = "displayLegend" in opts ? opts.displayLegend : true;
|
const strippedLabels = labels.map(l => stripHtml(l));
|
||||||
return {
|
return {
|
||||||
type: PIE_CHART_TYPE,
|
type: PIE_CHART_TYPE,
|
||||||
data: {
|
data: {
|
||||||
|
@ -636,17 +639,30 @@ function pieChartConfig(data, labels, opts = {}) {
|
||||||
backgroundColor: getColors(data.length)
|
backgroundColor: getColors(data.length)
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
labels
|
labels: strippedLabels
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
responsive: true,
|
responsive: true,
|
||||||
aspectRatio,
|
aspectRatio,
|
||||||
animation: { duration: 400 },
|
animation: { duration: 400 },
|
||||||
legend: { display: displayLegend }
|
legend: { display: false },
|
||||||
|
legendCallback: function(chart) {
|
||||||
|
let legends = "";
|
||||||
|
for (let i = 0; i < labels.length; i++) {
|
||||||
|
legends += `<div class="legend"><span class="swatch" style="background-color:
|
||||||
|
${chart.data.datasets[0].backgroundColor[i]}"></span>${labels[i]}</div>`;
|
||||||
|
}
|
||||||
|
return legends;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function stripHtml(html) {
|
||||||
|
var doc = new DOMParser().parseFromString(html, "text/html");
|
||||||
|
return doc.body.textContent || "";
|
||||||
|
}
|
||||||
|
|
||||||
createWidget("discourse-poll-buttons", {
|
createWidget("discourse-poll-buttons", {
|
||||||
tagName: "div.poll-buttons",
|
tagName: "div.poll-buttons",
|
||||||
|
|
||||||
|
|
|
@ -120,6 +120,23 @@ div.poll {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.pie-chart-legends {
|
||||||
|
text-align: center;
|
||||||
|
margin: 10px 0;
|
||||||
|
|
||||||
|
.legend {
|
||||||
|
display: inline-block;
|
||||||
|
margin: 4px 0 4px 14px;
|
||||||
|
|
||||||
|
.swatch {
|
||||||
|
display: inline-block;
|
||||||
|
width: 24px;
|
||||||
|
height: 12px;
|
||||||
|
margin-right: 4px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.poll-grouped-pies-controls {
|
.poll-grouped-pies-controls {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
|
|
Loading…
Reference in New Issue