Skip to content

Commit

Permalink
Fixed #223, pie chart data labels were not truncated to chart area wh…
Browse files Browse the repository at this point in the history
…en pie size was explicitly set.
  • Loading branch information
TorsteinHonsi committed May 9, 2017
1 parent 3a2a4d2 commit 38b2e69
Showing 1 changed file with 82 additions and 69 deletions.
151 changes: 82 additions & 69 deletions js/parts/DataLabels.js
Expand Up @@ -632,41 +632,39 @@ if (seriesTypes.pie) {


// Detect overflowing data labels
if (series.options.size === null) {
dataLabelWidth = dataLabel.getBBox().width;

sideOverflow = null;
// Overflow left
if (x - dataLabelWidth < connectorPadding) {
sideOverflow = Math.round(
dataLabelWidth - x + connectorPadding
);
overflow[3] = Math.max(sideOverflow, overflow[3]);
dataLabelWidth = dataLabel.getBBox().width;

// Overflow right
} else if (x + dataLabelWidth > plotWidth - connectorPadding) {
sideOverflow = Math.round(
x + dataLabelWidth - plotWidth + connectorPadding
);
overflow[1] = Math.max(sideOverflow, overflow[1]);
}
sideOverflow = null;
// Overflow left
if (x - dataLabelWidth < connectorPadding) {
sideOverflow = Math.round(
dataLabelWidth - x + connectorPadding
);
overflow[3] = Math.max(sideOverflow, overflow[3]);

// Overflow top
if (y - labelHeight / 2 < 0) {
overflow[0] = Math.max(
Math.round(-y + labelHeight / 2),
overflow[0]
);
// Overflow right
} else if (x + dataLabelWidth > plotWidth - connectorPadding) {
sideOverflow = Math.round(
x + dataLabelWidth - plotWidth + connectorPadding
);
overflow[1] = Math.max(sideOverflow, overflow[1]);
}

// Overflow left
} else if (y + labelHeight / 2 > plotHeight) {
overflow[2] = Math.max(
Math.round(y + labelHeight / 2 - plotHeight),
overflow[2]
);
}
dataLabel.sideOverflow = sideOverflow;
// Overflow top
if (y - labelHeight / 2 < 0) {
overflow[0] = Math.max(
Math.round(-y + labelHeight / 2),
overflow[0]
);

// Overflow left
} else if (y + labelHeight / 2 > plotHeight) {
overflow[2] = Math.max(
Math.round(y + labelHeight / 2 - plotHeight),
overflow[2]
);
}
dataLabel.sideOverflow = sideOverflow;
} // for each point
}); // for each half

Expand Down Expand Up @@ -794,47 +792,62 @@ if (seriesTypes.pie) {
centerOption = options.center,
minSize = options.minSize || 80,
newSize = minSize,
ret;

// Handle horizontal size and center
if (centerOption[0] !== null) { // Fixed center
newSize = Math.max(center[2] - Math.max(overflow[1], overflow[3]), minSize);

} else { // Auto center
newSize = Math.max(
center[2] - overflow[1] - overflow[3], // horizontal overflow
minSize
);
center[0] += (overflow[3] - overflow[1]) / 2; // horizontal center
}
// If a size is set, return true and don't try to shrink the pie
// to fit the labels.
ret = options.size !== null;

if (!ret) {
// Handle horizontal size and center
if (centerOption[0] !== null) { // Fixed center
newSize = Math.max(center[2] -
Math.max(overflow[1], overflow[3]), minSize);

} else { // Auto center
newSize = Math.max(
// horizontal overflow
center[2] - overflow[1] - overflow[3],
minSize
);
// horizontal center
center[0] += (overflow[3] - overflow[1]) / 2;
}

// Handle vertical size and center
if (centerOption[1] !== null) { // Fixed center
newSize = Math.max(Math.min(newSize, center[2] - Math.max(overflow[0], overflow[2])), minSize);

} else { // Auto center
newSize = Math.max(
Math.min(
newSize,
center[2] - overflow[0] - overflow[2] // vertical overflow
),
minSize
);
center[1] += (overflow[0] - overflow[2]) / 2; // vertical center
}
// Handle vertical size and center
if (centerOption[1] !== null) { // Fixed center
newSize = Math.max(Math.min(newSize, center[2] -
Math.max(overflow[0], overflow[2])), minSize);

} else { // Auto center
newSize = Math.max(
Math.min(
newSize,
// vertical overflow
center[2] - overflow[0] - overflow[2]
),
minSize
);
// vertical center
center[1] += (overflow[0] - overflow[2]) / 2;
}

// If the size must be decreased, we need to run translate and drawDataLabels again
if (newSize < center[2]) {
center[2] = newSize;
center[3] = Math.min(relativeLength(options.innerSize || 0, newSize), newSize); // #3632
this.translate(center);

if (this.drawDataLabels) {
this.drawDataLabels();
// If the size must be decreased, we need to run translate and
// drawDataLabels again
if (newSize < center[2]) {
center[2] = newSize;
center[3] = Math.min( // #3632
relativeLength(options.innerSize || 0, newSize),
newSize
);
this.translate(center);

if (this.drawDataLabels) {
this.drawDataLabels();
}
// Else, return true to indicate that the pie and its labels is
// within the plot area
} else {
ret = true;
}
// Else, return true to indicate that the pie and its labels is within the plot area
} else {
ret = true;
}
return ret;
};
Expand Down

0 comments on commit 38b2e69

Please sign in to comment.