createLegend method

Legend createLegend (Map<LegA, String> legendAttr, List<String> poly_colors)

Creates a legend from legendAttr. poly_colors, if not null, draws colored subtitle rectangles if there are subtitles specified in legendAttr.

Implementation

static Legend createLegend(
    Map<LegA, String> legendAttr, List<String> poly_colors) {
  Map<LegA, String> attr = Map.from(Legend.LEGEND_DEFAULT_ATTRIBUTES);
  if (legendAttr != null) {
    attr.addAll(legendAttr); // merge with defaults
    legendAttr = attr;
  } else {
    legendAttr = attr;
  }

  if (legendAttr[LegA.TOP_TITLE] == null) {
    return null;
  }

  int nsubtitles = 0;
  List<String> subtitles;
  // setup the subtitle lines, use same color as for polylines
  if (legendAttr[LegA.SUB_TITLES] != null) {
    subtitles = json.decode(legendAttr[LegA.SUB_TITLES]).cast<String>();
    nsubtitles = subtitles.length;
  }

  Legend legend = Legend(legendAttr, nsubtitles); // create the legend

  // draw subtitles only if nsubtitles > 0:
  // - cut color list to the number needed
  // - note that setText draws from bottom up
  List<String> colors;
  if (poly_colors != null) {
    colors = List.from(poly_colors.sublist(0, nsubtitles));
  }
  for (int i = nsubtitles - 1; i >= 0; i--) {
    if (colors == null) {
      legend.setText("${i + 1}", subtitles[i], null); // no colored rect drawn
    } else {
      legend.setText("${i + 1}", subtitles[i], colors[i]);
    }
  }
  return legend;
}