Commit 1c68f4a4 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

implement the legend-{top,right,bottom,left} legend placement

parent 5f262043
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -134,7 +134,7 @@ ReturnCode legend_layout_item_flow(
  double m_width = 0;
  double m_height = 0;

  size_t r_begin = item_boxes->size();
  size_t r_begin = item_boxes ? item_boxes->size() : 0;
  double r_width = 0;
  double r_height = 0;
  for (const auto& e : config.items) {
@@ -156,7 +156,7 @@ ReturnCode legend_layout_item_flow(
      m_height += r_height + config.item_row_padding;
      r_width = 0;
      r_height = 0;
      r_begin = item_boxes->size();
      r_begin = item_boxes ? item_boxes->size() : 0;
    }

    Rectangle item_box;
@@ -165,19 +165,19 @@ ReturnCode legend_layout_item_flow(
    item_box.w = e_width;
    item_box.h = e_height;

    if (item_boxes) {
      item_boxes->push_back(item_box);
    }

    r_width += e_width;
    r_height = std::max(r_height, e_height);
    m_width = std::max(m_width, r_width);

    if (item_boxes) {
      item_boxes->push_back(item_box);

      // not a problem since rows are O(10^1)
      for (size_t i = r_begin; i < item_boxes->size(); ++i) {
        item_boxes->at(i).h = r_height;
      }
    }
  }

  *min_width = m_width;
  *min_height = m_height + r_height;
+22 −1
Original line number Diff line number Diff line
@@ -291,6 +291,7 @@ ReturnCode build(
  ExprStorage grid_opts;
  std::vector<ExprStorage> grid_extra_opts;
  ExprStorage legend_overlay;
  std::array<ExprStorage, 4> legend_margins;

  auto config_rc = expr_walk_map(expr_next(expr), {
    /* scale options */
@@ -358,6 +359,10 @@ ReturnCode build(
    /* grid & legend */
    {"grid", bind(&expr_to_copy, _1, &grid_opts)},
    {"legend-overlay", bind(&expr_to_copy, _1, &legend_overlay)},
    {"legend-top", bind(&expr_to_copy, _1, &legend_margins[0])},
    {"legend-right", bind(&expr_to_copy, _1, &legend_margins[1])},
    {"legend-bottom", bind(&expr_to_copy, _1, &legend_margins[2])},
    {"legend-left", bind(&expr_to_copy, _1, &legend_margins[3])},

    /* extra elements */
    {"body", bind(&element_build_list, env, _1, &config->body_elements)},
@@ -561,7 +566,6 @@ ReturnCode build(
    config->margin_elements[axis.position].emplace_back(elem);
  }


  /* build the chart (body) geom elements */
  geom_opts.emplace_back(expr_create_value("limit-x-min"));
  geom_opts.emplace_back(expr_clone(xmin.get()));
@@ -601,6 +605,23 @@ ReturnCode build(
    config->body_elements.emplace_back(elem);
  }

  for (size_t i = 0; i < legend_margins.size(); ++i) {
    if (!legend_margins[i]) {
      continue;
    }

    auto elem_config = expr_build(
        "legend",
        expr_unwrap(std::move(legend_margins[i])));

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

    config->margin_elements[i].emplace_back(elem);
  }

  /* return the layout element */
  *elem = std::make_shared<Element>();
  (*elem)->draw = bind(&draw, config, _1, _2);