computeLegend method

void computeLegend(
  1. ChartData<IDataSet<Entry>> data
)

Builds the legend entries from the data sets of data, unless the legend is custom: one entry per color of a data set, with stack labels for stacked bar sets, entry labels for pie sets and the decreasing and increasing colors for candle sets, plus the extra entries of the legend. Then measures the legend with labelPaint so the chart can reserve space for it.

Implementation

void computeLegend(ChartData data) {
  if (!legend.isLegendCustom) {
    computedEntries.clear();

    for (var i = 0; i < data.dataSetCount; i++) {
      final dataSet = data.getDataSetByIndex(i);
      if (dataSet == null) continue;

      final clrs = dataSet.colors;
      final entryCount = dataSet.entryCount;

      if (dataSet is IBarDataSet && dataSet.isStacked) {
        final sLabels = dataSet.stackLabels;
        final minEntries = math.min(clrs.length, dataSet.stackSize);

        for (var j = 0; j < minEntries; j++) {
          final String? label;
          if (sLabels.isNotEmpty) {
            final labelIndex = j % minEntries;
            label = labelIndex < sLabels.length ? sLabels[labelIndex] : null;
          } else {
            label = null;
          }

          computedEntries.add(
            LegendEntry(
              label,
              dataSet.form,
              dataSet.formSize,
              dataSet.formLineWidth,
              dataSet.formLineDashEffect,
              clrs[j],
            ),
          );
        }

        computedEntries.add(
          LegendEntry(
            dataSet.label,
            LegendForm.NONE,
            double.nan,
            double.nan,
            null,
            ColorTemplate.COLOR_NONE,
          ),
        );
      } else if (dataSet is IPieDataSet) {
        var j = 0;
        while (j < clrs.length && j < entryCount) {
          computedEntries.add(
            LegendEntry(
              dataSet.getEntryForIndex(j).label?.replaceAll('\n', ' '),
              dataSet.form,
              dataSet.formSize,
              dataSet.formLineWidth,
              dataSet.formLineDashEffect,
              clrs[j],
            ),
          );
          j++;
        }

        if (dataSet.label.trim().isNotEmpty) {
          computedEntries.add(
            LegendEntry(
              dataSet.label,
              LegendForm.NONE,
              double.nan,
              double.nan,
              null,
              ColorTemplate.COLOR_NONE,
            ),
          );
        }
      } else if (dataSet is ICandleDataSet &&
          dataSet.decreasingColor != ColorTemplate.COLOR_NONE) {
        final decreasingColor = dataSet.decreasingColor;
        final increasingColor = dataSet.increasingColor;

        computedEntries.add(
          LegendEntry(
            null,
            dataSet.form,
            dataSet.formSize,
            dataSet.formLineWidth,
            dataSet.formLineDashEffect,
            decreasingColor,
          ),
        );
        computedEntries.add(
          LegendEntry(
            dataSet.label,
            dataSet.form,
            dataSet.formSize,
            dataSet.formLineWidth,
            dataSet.formLineDashEffect,
            increasingColor,
          ),
        );
      } else {
        var j = 0;
        while (j < clrs.length && j < entryCount) {
          final label = j < clrs.length - 1 && j < entryCount - 1
              ? null
              : dataSet.label;
          computedEntries.add(
            LegendEntry(
              label,
              dataSet.form,
              dataSet.formSize,
              dataSet.formLineWidth,
              dataSet.formLineDashEffect,
              clrs[j],
            ),
          );
          j++;
        }
      }
    }

    computedEntries.addAll(legend.extraEntries);

    legend.setComputedEntries(computedEntries);
  }

  final tf = legend.typeface;
  if (tf != null) labelPaint.typeface = tf;

  labelPaint.textSize = Utils.convertDpToPixel(legend.textSize);
  labelPaint.color = legend.textColor;

  legend.calculateDimensions(labelPaint, viewPortHandler);
}