Commit 02529b7c authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add convenience legend constructors

parent 63fc3689
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ ReturnCode build(

    /* grid & legend */
    {"grid", bind(&expr_rewritev, _1, "grid", &layout_opts)},
    {"legend", bind(&expr_rewritev, _1, "legend", &layout_opts)},
    {"legend-overlay", bind(&expr_rewritev, _1, "legend-overlay", &layout_opts)},

    /* background, margins, borders */
    {"background", bind(&expr_rewritev, _1, "background", &layout_opts)},
+1 −1
Original line number Diff line number Diff line
@@ -75,7 +75,7 @@ ReturnCode build(

    /* grid & legend */
    {"grid", bind(&expr_rewritev, _1, "grid", &layout_opts)},
    {"legend", bind(&expr_rewritev, _1, "legend", &layout_opts)},
    {"legend-overlay", bind(&expr_rewritev, _1, "legend-overlay", &layout_opts)},

    /* background, margins, borders */
    {"background", bind(&expr_rewritev, _1, "background", &layout_opts)},
+34 −1
Original line number Diff line number Diff line
@@ -42,6 +42,11 @@ struct LegendConfig {
  std::vector<ElementRef> items;
};

struct LegendItemConfig {
  std::string elem_name;
  ExprStorage opts;
};

LegendConfig::LegendConfig() :
    position_horiz(HAlign::LEFT),
    position_vert(VAlign::TOP) {}
@@ -366,6 +371,17 @@ ReturnCode legend_configure_position(
  return OK;
}

ReturnCode configure_item(
    const std::string& item_elem_name,
    const Expr* expr,
    std::vector<LegendItemConfig>* items) {
  LegendItemConfig item;
  item.elem_name = item_elem_name;
  item.opts = expr_clone(expr_get_list(expr));
  items->emplace_back(std::move(item));
  return OK;
}

ReturnCode build(
    const Environment& env,
    const Expr* expr,
@@ -382,6 +398,8 @@ ReturnCode build(
  }

  /* parse exprerties */
  std::vector<LegendItemConfig> items;

  auto config_rc = expr_walk_map(expr_next(expr), {
    {
      "position",
@@ -393,7 +411,8 @@ ReturnCode build(
    },
    {"item-row-padding", bind(&expr_to_measure, _1, &config->item_row_padding)},
    {"item-column-padding", bind(&expr_to_measure, _1, &config->item_column_padding)},
    {"items", bind(&element_build_list, env, _1, &config->items)},
    {"item", bind(&configure_item, "legend/item", _1, &items)},
    {"extra", bind(&element_build_list, env, _1, &config->items)},
    {
      "padding",
      expr_calln_fn({
@@ -462,6 +481,20 @@ ReturnCode build(
    return config_rc;
  }

  /* build the items */
  for (const auto& item : items) {
    auto elem_config = expr_build(
        item.elem_name,
        expr_clone(item.opts.get()));

    ElementRef elem;
    if (auto rc = element_build_macro(env, elem_config.get(), &elem); !rc) {
      return rc;
    }

    config->items.emplace_back(elem);
  }

  *elem = std::make_shared<Element>();
  (*elem)->draw = bind(&legend_draw, config, _1, _2);
  (*elem)->size_hint = bind(&legend_layout, config, _1, _2, _3, _4, _5);
+6 −7
Original line number Diff line number Diff line
@@ -289,7 +289,7 @@ ReturnCode build(
  std::vector<ExprStorage> axis_y_opts;
  ExprStorage grid_opts;
  std::vector<ExprStorage> grid_extra_opts;
  ExprStorage legend_opts;
  ExprStorage legend_overlay;

  auto config_rc = expr_walk_map(expr_next(expr), {
    /* scale options */
@@ -354,7 +354,7 @@ ReturnCode build(

    /* grid & legend */
    {"grid", bind(&expr_to_copy, _1, &grid_opts)},
    {"legend", bind(&expr_to_copy, _1, &legend_opts)},
    {"legend-overlay", bind(&expr_to_copy, _1, &legend_overlay)},

    /* extra elements */
    {"body", bind(&element_build_list, env, _1, &config->body_elements)},
@@ -584,12 +584,11 @@ ReturnCode build(
    config->body_elements.emplace_back(elem);
  }


  /* build the legend */
  if (legend_opts) {
  /* build the legend overlay */
  if (legend_overlay) {
    auto elem_config = expr_build(
        "chart/legend",
        expr_unwrap(std::move(legend_opts)));
        "legend",
        expr_unwrap(std::move(legend_overlay)));

    ElementRef elem;
    if (auto rc = element_build_macro(env, elem_config.get(), &elem); !rc) {
+6 −4
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@
        data-x (10 20 30 40 50)
        data-y (1.23 10.32 -6.23 4.43 3.45)
        marker-size (3pt))
    legend (
        items ("Series A" "Series B" "Combined")
        colors (#ccc #888 #000)
        position (bottom left)))
    legend-overlay (
        position (bottom left)
        background #fff
        item (label "Series A" color #ccc)
        item (label "Series B" color #888)))
Loading