calculateDimensions method
Calculates the dimensions of the Legend. This includes the maximum width and height of a single entry, as well as the total width and height of the Legend.
@param labelpaint
Implementation
void calculateDimensions(
TextPainter? labelpainter, ViewPortHandler? viewPortHandler) {
double? defaultFormSize = Utils.convertDpToPixel(_formSize);
double? stackSpace = Utils.convertDpToPixel(_stackSpace);
double? formToTextSpace = Utils.convertDpToPixel(_formToTextSpace);
double? xEntrySpace = Utils.convertDpToPixel(_xEntrySpace);
double? yEntrySpace = Utils.convertDpToPixel(_yEntrySpace);
bool wordWrapEnabled = _wordWrapEnabled;
List<LegendEntry?> entries = _entries;
int entryCount = entries.length;
_textWidthMax = getMaximumEntryWidth(labelpainter);
_textHeightMax = getMaximumEntryHeight(labelpainter);
switch (_orientation) {
case LegendOrientation.vertical:
{
double maxWidth = 0, maxHeight = 0, width = 0;
double labelLineHeight = Utils.getLineHeight1(labelpainter!);
bool wasStacked = false;
for (int i = 0; i < entryCount; i++) {
LegendEntry e = entries[i]!;
bool drawingForm = e.form != LegendForm.none;
double? formSize = e.formSize.isNaN
? defaultFormSize
: Utils.convertDpToPixel(e.formSize);
String? label = e.label;
if (!wasStacked) width = 0;
if (drawingForm) {
if (wasStacked) width += stackSpace;
width += formSize;
}
// grouped forms have null labels
if (label != null) {
// make a step to the left
if (drawingForm && !wasStacked) {
width += formToTextSpace;
} else if (wasStacked) {
maxWidth = max(maxWidth, width);
maxHeight += labelLineHeight + yEntrySpace;
width = 0;
wasStacked = false;
}
width += Utils.calcTextWidth(labelpainter, label);
if (i < entryCount - 1) {
maxHeight += labelLineHeight + yEntrySpace;
}
} else {
wasStacked = true;
width += formSize;
if (i < entryCount - 1) width += stackSpace;
}
maxWidth = max(maxWidth, width);
}
_neededWidth = maxWidth;
_neededHeight = maxHeight;
break;
}
case LegendOrientation.horizontal:
{
double labelLineHeight = Utils.getLineHeight1(labelpainter!);
double labelLineSpacing =
Utils.getLineSpacing1(labelpainter) + yEntrySpace;
double contentWidth = viewPortHandler!.chartWidth() * _maxSizePercent;
// Start calculating layout
double maxLineWidth = 0;
double currentLineWidth = 0;
double requiredWidth = 0;
int stackedStartIndex = -1;
_calculatedLabelBreakPoints = List.empty(growable: true);
_calculatedLabelSizes = List.empty(growable: true);
_calculatedLineSizes = List.empty(growable: true);
for (int i = 0; i < entryCount; i++) {
LegendEntry e = entries[i]!;
bool drawingForm = e.form != LegendForm.none;
double? formSize = e.formSize.isNaN
? defaultFormSize
: Utils.convertDpToPixel(e.formSize);
String? label = e.label;
_calculatedLabelBreakPoints.add(false);
if (stackedStartIndex == -1) {
// we are not stacking, so required width is for this label
// only
requiredWidth = 0;
} else {
// add the spacing appropriate for stacked labels/forms
requiredWidth += stackSpace;
}
// grouped forms have null labels
if (label != null) {
_calculatedLabelSizes
.add(Utils.calcTextSize1(labelpainter, label));
requiredWidth += drawingForm ? formToTextSpace + formSize : 0;
requiredWidth += _calculatedLabelSizes[i]!.width;
} else {
_calculatedLabelSizes.add(FSize.getInstance(0, 0));
requiredWidth += drawingForm ? formSize : 0;
if (stackedStartIndex == -1) {
// mark this index as we might want to break here later
stackedStartIndex = i;
}
}
if (label != null || i == entryCount - 1) {
double? requiredSpacing = currentLineWidth == 0 ? 0 : xEntrySpace;
if (!wordWrapEnabled // No word wrapping, it must fit.
// The line is empty, it must fit
||
currentLineWidth == 0
// It simply fits
||
(contentWidth - currentLineWidth >=
requiredSpacing + requiredWidth)) {
// Expand current line
currentLineWidth += requiredSpacing + requiredWidth;
} else {
// It doesn't fit, we need to wrap a line
// Add current line size to array
_calculatedLineSizes
.add(FSize.getInstance(currentLineWidth, labelLineHeight));
maxLineWidth = max(maxLineWidth, currentLineWidth);
// Start a new line
_calculatedLabelBreakPoints.insert(
stackedStartIndex > -1 ? stackedStartIndex : i, true);
currentLineWidth = requiredWidth;
}
if (i == entryCount - 1) {
// Add last line size to array
_calculatedLineSizes
.add(FSize.getInstance(currentLineWidth, labelLineHeight));
maxLineWidth = max(maxLineWidth, currentLineWidth);
}
}
stackedStartIndex = label != null ? -1 : stackedStartIndex;
}
_neededWidth = maxLineWidth;
_neededHeight =
labelLineHeight * (_calculatedLineSizes.length).toDouble() +
labelLineSpacing *
(_calculatedLineSizes.isEmpty
? 0
: (_calculatedLineSizes.length - 1));
break;
}
}
_neededHeight += yOffset;
_neededWidth += xOffset;
}