#1027 Updated Pie chart to render percentages and labels separately as legends

This commit is contained in:
Ashish Jain
2019-10-23 21:00:33 +02:00
parent 4a1eb55127
commit 76c8737485

View File

@@ -50,6 +50,8 @@ export const draw = (txt, id, ver) => {
var width = w; // 450 var width = w; // 450
var height = 450; var height = 450;
var margin = 40; var margin = 40;
var legendRectSize = 18;
var legendSpacing = 4;
var radius = Math.min(width, height) / 2 - margin; var radius = Math.min(width, height) / 2 - margin;
@@ -62,6 +64,10 @@ export const draw = (txt, id, ver) => {
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')'); .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');
var data = pieData.getSections(); var data = pieData.getSections();
var sum = 0;
Object.keys(data).forEach(function(key) {
sum += data[key];
});
logger.info(data); logger.info(data);
// set the color scale // set the color scale
@@ -75,7 +81,6 @@ export const draw = (txt, id, ver) => {
return d.value; return d.value;
}); });
var dataReady = pie(d3.entries(data)); var dataReady = pie(d3.entries(data));
// Now I know that group A goes from 0 degrees to x degrees and so on.
// shape helper to build arcs: // shape helper to build arcs:
var arcGenerator = d3 var arcGenerator = d3
@@ -97,14 +102,14 @@ export const draw = (txt, id, ver) => {
.style('stroke-width', '2px') .style('stroke-width', '2px')
.style('opacity', 0.7); .style('opacity', 0.7);
// Now add the annotation. Use the centroid method to get the best coordinates // Now add the Percentage. Use the centroid method to get the best coordinates
svg svg
.selectAll('mySlices') .selectAll('mySlices')
.data(dataReady) .data(dataReady)
.enter() .enter()
.append('text') .append('text')
.text(function(d) { .text(function(d) {
return d.data.key; return ((d.data.value / sum) * 100).toFixed(0) + '%';
}) })
.attr('transform', function(d) { .attr('transform', function(d) {
return 'translate(' + arcGenerator.centroid(d) + ')'; return 'translate(' + arcGenerator.centroid(d) + ')';
@@ -119,6 +124,36 @@ export const draw = (txt, id, ver) => {
.attr('x', 0) .attr('x', 0)
.attr('y', -(h - 50) / 2) .attr('y', -(h - 50) / 2)
.attr('class', 'pieTitleText'); .attr('class', 'pieTitleText');
//Add the slegend/annotations for each section
var legend = svg
.selectAll('.legend')
.data(color.domain())
.enter()
.append('g')
.attr('class', 'legend')
.attr('transform', function(d, i) {
var height = legendRectSize + legendSpacing;
var offset = (height * color.domain().length) / 2;
var horz = 12 * legendRectSize;
var vert = i * height - offset;
return 'translate(' + horz + ',' + vert + ')';
});
legend
.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill', color)
.style('stroke', color);
legend
.append('text')
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize - legendSpacing)
.text(function(d) {
return d;
});
} catch (e) { } catch (e) {
logger.error('Error while rendering info diagram'); logger.error('Error while rendering info diagram');
logger.error(e.message); logger.error(e.message);