Commit 9c01523b authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add the 'legend' and 'legend-title' properties

parent b3d50a3c
Loading
Loading
Loading
Loading
+84 −4
Original line number Diff line number Diff line
@@ -42,9 +42,9 @@ static const double kDefaultItemPaddingHorizEM = 2.4;
static const double kDefaultItemPaddingVertEM = 1.0;

LegendConfig::LegendConfig() :
    vert_pos(LEGEND_TOP),
    horiz_pos(LEGEND_LEFT),
    placement(LEGEND_INSIDE) {}
    placement(LegendPlacement::INSIDE),
    position_horiz(HAlign::LEFT),
    position_vert(VAlign::TOP) {}

void LegendConfig::addEntry(
    const std::string& name,
@@ -339,7 +339,7 @@ LegendConfig* Legend::legend() const {
  }
}
*/
ReturnCode legend_draw(
ReturnCode legend_draw_inside(
    const LegendConfig& legend,
    const Rectangle& bbox,
    Layer* layer) {
@@ -412,6 +412,76 @@ ReturnCode legend_draw(
  return OK;
}

ReturnCode legend_draw(
    const LegendConfig& config,
    const Rectangle& bbox,
    Layer* layer) {
  switch (config.placement) {
    case LegendPlacement::INSIDE:
      return legend_draw_inside(config, bbox, layer);
    case LegendPlacement::OUTSIDE:
    case LegendPlacement::OFF:
    default:
      return OK;
  }
}

ReturnCode legend_configure_position(
    const plist::Property& prop,
    LegendPlacement* placement,
    HAlign* position_horiz,
    VAlign* position_vert) {
  auto args = plist::flatten(prop);
  bool position_horiz_set = false;
  bool position_vert_set = false;
  for (const auto& prop : args) {
    if (prop == "off") {
      *placement = LegendPlacement::OFF;
      return OK;
    }

    if (prop == "inside") {
      *placement = LegendPlacement::INSIDE;
      continue;
    }

    if (prop == "outside") {
      *placement = LegendPlacement::OUTSIDE;
      continue;
    }

    if (prop == "top") {
      *position_vert = VAlign::TOP;
      continue;
    }

    if (prop == "bottom") {
      *position_vert = VAlign::BOTTOM;
      continue;
    }

    if (prop == "left") {
      *position_horiz = HAlign::LEFT;
      continue;
    }

    if (prop == "right") {
      *position_horiz = HAlign::RIGHT;
      continue;
    }

    if (prop == "center") {
      if (!position_horiz_set) *position_horiz = HAlign::CENTER;
      if (!position_vert_set) *position_vert = VAlign::CENTER;
      continue;
    }

    return ERROR_INVALID_ARGUMENT;
  }

  return OK;
}

ReturnCode legend_configure(
    const Document& doc,
    const plist::PropertyList& plist,
@@ -421,6 +491,16 @@ ReturnCode legend_configure(
  config->text_color = doc.text_color;

  static const ParserDefinitions pdefs = {
    {
      "legend",
      std::bind(
          &legend_configure_position,
          std::placeholders::_1,
          &config->placement,
          &config->position_horiz,
          &config->position_vert)
    },
    {"legend-title", std::bind(&configure_string, std::placeholders::_1, &config->title)},
    {"legend-text-color", std::bind(&configure_color, std::placeholders::_1, &config->text_color)},
    {"legend-border-color", std::bind(&configure_color, std::placeholders::_1, &config->border_color)},
  };
+11 −18
Original line number Diff line number Diff line
@@ -33,25 +33,18 @@
#include <string>
#include <tuple>
#include "graphics/layer.h"
#include "graphics/layout.h"
#include "common/document.h"

namespace plotfx {

struct LegendConfig {
  enum kVerticalPosition {
    LEGEND_TOP = 0,
    LEGEND_BOTTOM = 1
  };

  enum kHorizontalPosition {
    LEGEND_LEFT = 0,
    LEGEND_RIGHT = 1
enum class LegendPlacement {
  OFF = 0,
  INSIDE = 1,
  OUTSIDE = 2
};

  enum kPlacement {
    LEGEND_INSIDE = 0,
    LEGEND_OUTSIDE = 1
  };
struct LegendConfig {

  LegendConfig();

@@ -67,10 +60,10 @@ struct LegendConfig {
  Measure padding_vert;
  Measure padding_item_horiz;
  Measure padding_item_vert;
  kVerticalPosition vert_pos;
  kHorizontalPosition horiz_pos;
  kPlacement placement;
  const std::string title;
  LegendPlacement placement;
  HAlign position_horiz;
  VAlign position_vert;
  std::string title;
  std::vector<std::tuple<std::string, Color, std::string>> entries;
};

+2 −6
Original line number Diff line number Diff line
@@ -5,17 +5,13 @@ plot {
  axis-top: off;
  axis-right: off;

  margin: 0px;
  margin-top: 1.1rem;
  margin-bottom: 2.6rem;
  margin-left: 5rem;
  margin-right: 3.4rem;

  ymin: -10;
  ymax: 32;
  xdomain: categorical;
  xdomain-format: string;

  legend: inside top left;

  series {
    title: "New York";
    xs: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec;
+103 −103

File changed.

Preview size limit exceeded, changes collapsed.