Commit 1c237c97 authored by Laura Schlimmer's avatar Laura Schlimmer
Browse files

enable legend rendering for barchart

parent 6730a68b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -132,7 +132,7 @@ ReturnCode configure_layer(

  if (type == "bars")
    layer_builder = elem_builder<bars::PlotBarsConfig>(
        bind(&bars::configure, _1, _2, _3, scales, _4),
        bind(&bars::configure, _1, _2, _3, scales, legend_items, _4),
        &bars::draw);

  if (type == "labels")
+32 −0
Original line number Diff line number Diff line
@@ -232,11 +232,33 @@ ReturnCode draw(
  }
}

ReturnCode build_legend(
    const PlotBarsConfig& config,
    const std::string& title,
    const std::string& legend_key,
    LegendItemMap* legend) {
  LegendItemGroup legend_items;

  for (const auto& g : config.groups) {
    LegendItem li;
    li.title = g.key.empty() ? title : g.key;
    li.color = config.colors.empty()
        ? Color{}
        : config.colors[g.index[0] % config.colors.size()];

    legend_items.items.emplace_back(li);
  }

  legend_items_add(legend_key, legend_items, legend);
  return OK;
}

ReturnCode configure(
    const plist::PropertyList& plist,
    const DataContext& data,
    const Document& doc,
    const DomainMap& scales,
    LegendItemMap* legend,
    PlotBarsConfig* config) {
  SeriesRef data_x1 = find_maybe(data.defaults, "x");
  SeriesRef data_x2;
@@ -248,6 +270,10 @@ ReturnCode configure(
  std::string scale_x = SCALE_DEFAULT_X;
  std::string scale_y = SCALE_DEFAULT_Y;

  std::string title;

  std::string legend_key = LEGEND_DEFAULT;

  Direction direction = Direction::VERTICAL;

  std::optional<Color> color;
@@ -269,6 +295,7 @@ ReturnCode configure(
    {"color", configure_color_opt(&color)},
    {"colors", configure_series_fn(data, &colors)},
    {"labels", configure_series_fn(data, &data_labels)},
    {"title", bind(&configure_string, _1, &title)},
  };

  if (auto rc = parseAll(plist, pdefs); !rc) {
@@ -348,6 +375,11 @@ ReturnCode configure(
    config->labels = *data_labels;
  }

  /* build legend items */
  if (auto rc = build_legend(*config, title, legend_key, legend); !rc) {
    return rc;
  }

  return OK;
}

+1 −0
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ ReturnCode configure(
    const DataContext& data,
    const Document& doc,
    const DomainMap& scales,
    LegendItemMap* legend,
    PlotBarsConfig* config);

} // namespace bars