layout method
Lays out the legend area.
Evenly divides the availableWidth
to all legend items.
Implementation
@override
void layout(LayoutExpansion parentLayoutExpansion) {
if (!chartTopContainer.data.chartOptions.legendOptions.isLegendContainerShown) {
return;
}
ChartOptions options = chartTopContainer.data.chartOptions;
double containerMarginTB = options.legendOptions.legendContainerMarginTB;
double containerMarginLR = options.legendOptions.legendContainerMarginLR;
List<String> dataRowsLegends = chartTopContainer.data.dataRowsLegends;
// Initially all [LabelContainer]s share same text style object from options.
LabelStyle labelStyle = LabelStyle(
textStyle: options.labelCommonOptions.labelTextStyle,
textDirection: options.labelCommonOptions.labelTextDirection,
textAlign: options.legendOptions.legendTextAlign, // keep left, close to indicator
textScaleFactor: options.labelCommonOptions.labelTextScaleFactor,
);
// First paint all legends, to figure out max height of legends to center all
// legends label around common center.
double legendItemWidth = (parentLayoutExpansion.width - 2.0 * containerMarginLR) / dataRowsLegends.length;
_legendItemContainers = List<LegendItemContainer>.empty(growable: true);
// Layout legend core: for each row, create and position
// - an indicator rectangle and it's paint
// - label painter
for (int index = 0; index < dataRowsLegends.length; index++) {
ui.Paint indicatorPaint = ui.Paint();
List<ui.Color> dataRowsColors = chartTopContainer.data.dataRowsColors; //!;
indicatorPaint.color = dataRowsColors[index % dataRowsColors.length];
var legendItemLayoutExpansion = parentLayoutExpansion.cloneWith(
width: legendItemWidth,
);
var legendItemContainer = LegendItemContainer(
label: dataRowsLegends[index],
labelStyle: labelStyle,
indicatorPaint: indicatorPaint,
options: options,
);
legendItemContainer.layout(legendItemLayoutExpansion);
legendItemContainer.applyParentOffset(
ui.Offset(
containerMarginLR + index * legendItemWidth,
containerMarginTB,
),
);
_legendItemContainers.add(legendItemContainer);
}
layoutSize = ui.Size(
parentLayoutExpansion.width,
_legendItemContainers.map((legendItemContainer) => legendItemContainer.layoutSize.height).reduce(math.max) +
(2.0 * containerMarginTB),
);
}