Commit 7dd39b73 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

clean up LegendConfig struct, add legend-{text,border}-colour properties

parent e652cecf
Loading
Loading
Loading
Loading
+49 −51
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include <stdlib.h>
#include "graphics/layer.h"
#include "common/domain.h"
#include "common/config_helpers.h"
#include "legend.h"

namespace plotfx {
@@ -40,39 +41,16 @@ static const double kDefaultPaddingVertEM = 1.2;
static const double kDefaultItemPaddingHorizEM = 2.4;
static const double kDefaultItemPaddingVertEM = 1.0;

LegendDefinition::LegendDefinition() :
    vert_pos_(LEGEND_TOP),
    horiz_pos_(LEGEND_LEFT),
    placement_(LEGEND_INSIDE) {}
LegendConfig::LegendConfig() :
    vert_pos(LEGEND_TOP),
    horiz_pos(LEGEND_LEFT),
    placement(LEGEND_INSIDE) {}

const std::string LegendDefinition::title() const {
  return title_;
}

LegendDefinition::kVerticalPosition LegendDefinition::verticalPosition() 
    const {
  return vert_pos_;
}

LegendDefinition::kHorizontalPosition LegendDefinition::horizontalPosition() 
    const {
  return horiz_pos_;
}

LegendDefinition::kPlacement LegendDefinition::placement() const {
  return placement_;
}

void LegendDefinition::addEntry(
void LegendConfig::addEntry(
    const std::string& name,
    const Colour& color,
    const std::string& shape /* = "circle" */) {
  entries_.emplace_back(name, color, shape);
}

const std::vector<std::tuple<std::string, Colour, std::string>>
    LegendDefinition::entries() const {
  return entries_;
  entries.emplace_back(name, color, shape);
}

/*
@@ -80,16 +58,16 @@ void Legend::renderOutsideLegends(
    Layer* target,
    const Rectangle& clip) const {
  for (const auto& legend : legends_) {
    if (legend->placement() != LegendDefinition::LEGEND_OUTSIDE) {
    if (legend->placement() != LegendConfig::LEGEND_OUTSIDE) {
      continue;
    }

    target->beginGroup("legend");

    switch (legend->verticalPosition()) {
      case LegendDefinition::LEGEND_TOP: {
      case LegendConfig::LEGEND_TOP: {
        switch (legend->horizontalPosition()) {
          case LegendDefinition::LEGEND_LEFT:
          case LegendConfig::LEGEND_LEFT:
            renderLeftLegend(
                target,
                viewport,
@@ -98,7 +76,7 @@ void Legend::renderOutsideLegends(
                false,
                true);
            break;
          case LegendDefinition::LEGEND_RIGHT:
          case LegendConfig::LEGEND_RIGHT:
            renderRightLegend(
                target,
                viewport,
@@ -114,9 +92,9 @@ void Legend::renderOutsideLegends(
        break;
      }

      case LegendDefinition::LEGEND_BOTTOM: {
      case LegendConfig::LEGEND_BOTTOM: {
        switch (legend->horizontalPosition()) {
          case LegendDefinition::LEGEND_LEFT:
          case LegendConfig::LEGEND_LEFT:
            renderLeftLegend(
                target,
                viewport,
@@ -125,7 +103,7 @@ void Legend::renderOutsideLegends(
                true,
                true);
            break;
          case LegendDefinition::LEGEND_RIGHT:
          case LegendConfig::LEGEND_RIGHT:
            renderRightLegend(
                target,
                viewport,
@@ -153,7 +131,7 @@ void Legend::renderInsideLegends(
  auto orig_padding = viewport->padding();

  for (const auto& legend : legends_) {
    if (legend->placement() != LegendDefinition::LEGEND_INSIDE) {
    if (legend->placement() != LegendConfig::LEGEND_INSIDE) {
      continue;
    }

@@ -164,22 +142,22 @@ void Legend::renderInsideLegends(
        viewport->paddingBottom() + kLegendInsideVertPadding);

    switch (legend->horizontalPosition()) {
      case LegendDefinition::LEGEND_LEFT:
      case LegendConfig::LEGEND_LEFT:
        renderLeftLegend(
            target,
            viewport,
            legend.get(),
            kLegendOutsideHorizPadding,
            legend->verticalPosition() == LegendDefinition::LEGEND_BOTTOM,
            legend->verticalPosition() == LegendConfig::LEGEND_BOTTOM,
            false);
        break;
      case LegendDefinition::LEGEND_RIGHT:
      case LegendConfig::LEGEND_RIGHT:
        renderRightLegend(
            target,
            viewport,
            legend.get(),
            kLegendOutsideHorizPadding,
            legend->verticalPosition() == LegendDefinition::LEGEND_BOTTOM,
            legend->verticalPosition() == LegendConfig::LEGEND_BOTTOM,
            false);
        break;
      }
@@ -193,7 +171,7 @@ void Legend::renderInsideLegends(
void Legend::renderRightLegend(
    Layer* target,
    const Rectangle& clip,
    LegendDefinition* legend,
    LegendConfig* legend,
    double horiz_padding,
    bool bottom,
    bool outside) const {
@@ -269,7 +247,7 @@ void Legend::renderRightLegend(
void Legend::renderLeftLegend(
    Layer* target,
    const Rectangle& clip,
    LegendDefinition* legend,
    LegendConfig* legend,
    double horiz_padding,
    bool bottom,
    bool outside) const {
@@ -343,17 +321,17 @@ void Legend::renderLeftLegend(
  }
}

LegendDefinition* Legend::addLegend(
    LegendDefinition::kVerticalPosition vert_pos,
    LegendDefinition::kHorizontalPosition horiz_pos,
    LegendDefinition::kPlacement placement,
LegendConfig* Legend::addLegend(
    LegendConfig::kVerticalPosition vert_pos,
    LegendConfig::kHorizontalPosition horiz_pos,
    LegendConfig::kPlacement placement,
    const std::string& title) {
  legends_.emplace_back(
      new LegendDefinition(vert_pos, horiz_pos, placement, title));
      new LegendConfig(vert_pos, horiz_pos, placement, title));
  return legends_.back().get();
}

LegendDefinition* Legend::legend() const {
LegendConfig* Legend::legend() const {
  if (legends_.size() == 0) {
    return nullptr;
  } else {
@@ -362,7 +340,7 @@ LegendDefinition* Legend::legend() const {
}
*/
ReturnCode legend_draw(
    const LegendDefinition& legend,
    const LegendConfig& legend,
    const Rectangle& bbox,
    Layer* layer) {
  auto font_size = from_em(kDefaultLabelFontSizeEM, layer->font_size);
@@ -389,7 +367,7 @@ ReturnCode legend_draw(
  double sx = bbox.x + padding_horiz;
  double sy = bbox.y + padding_vert + line_height / 2;

  for (const auto& e : legend.entries()) {
  for (const auto& e : legend.entries) {
    const auto& label_text = std::get<0>(e);

    {
@@ -434,5 +412,25 @@ ReturnCode legend_draw(
  return OK;
}

ReturnCode legend_configure(
    const Document& doc,
    const plist::PropertyList& plist,
    LegendConfig* config) {
  config->font = doc.font_sans;
  config->border_colour = doc.border_colour;
  config->text_colour = doc.text_colour;

  static const ParserDefinitions pdefs = {
    {"legend-text-colour", std::bind(&configure_colour, std::placeholders::_1, &config->text_colour)},
    {"legend-border-colour", std::bind(&configure_colour, std::placeholders::_1, &config->border_colour)},
  };

  if (auto rc = parseAll(plist, pdefs); !rc.isSuccess()) {
    return rc;
  }

  return OK;
}

} // namespace plotfx
+16 −32
Original line number Diff line number Diff line
@@ -33,22 +33,11 @@
#include <string>
#include <tuple>
#include "graphics/layer.h"
#include "common/document.h"

namespace plotfx {

static const int kLegendLabelPadding = 20; // FIXME make configurable
static const int kLegendLineHeight = 20; // FIXME make configurable
static const int kLegendInsideVertPadding = 10;
static const int kLegendInsideHorizPadding = 15;
static const int kLegendOutsideVertPadding = 10;
static const int kLegendOutsideHorizPadding = 25;
static const int kLegendPointY = 6;
static const int kLegendPointWidth = 8;
static const int kLegendPointSize = 3;

class LegendDefinition {
public:

struct LegendConfig {
  enum kVerticalPosition {
    LEGEND_TOP = 0,
    LEGEND_BOTTOM = 1
@@ -64,21 +53,13 @@ public:
    LEGEND_OUTSIDE = 1
  };

  LegendDefinition();

  const std::string title() const;
  kVerticalPosition verticalPosition() const;
  kHorizontalPosition horizontalPosition() const;
  kPlacement placement() const;
  LegendConfig();

  void addEntry(
      const std::string& name,
      const Colour& color,
      const std::string& shape = "circle");

  const std::vector<std::tuple<std::string, Colour, std::string>>
      entries() const;

  Colour text_colour;
  Colour border_colour;
  FontInfo font;
@@ -86,13 +67,11 @@ public:
  Measure padding_vert;
  Measure padding_item_horiz;
  Measure padding_item_vert;

protected:
  kVerticalPosition vert_pos_;
  kHorizontalPosition horiz_pos_;
  kPlacement placement_;
  const std::string title_;
  std::vector<std::tuple<std::string, Colour, std::string>> entries_;
  kVerticalPosition vert_pos;
  kHorizontalPosition horiz_pos;
  kPlacement placement;
  const std::string title;
  std::vector<std::tuple<std::string, Colour, std::string>> entries;
};

void renderOutsideLegends(Layer* target, const Rectangle& clip);
@@ -102,7 +81,7 @@ void renderInsideLegends(Layer* target, const Rectangle& clip);
void renderRightLegend(
    Layer* target,
    const Rectangle& clip,
    LegendDefinition* legend,
    LegendConfig* legend,
    double horiz_padding,
    bool bottom,
    bool outside);
@@ -110,13 +89,18 @@ void renderRightLegend(
void renderLeftLegend(
    Layer* target,
    const Rectangle& clip,
    LegendDefinition* legend,
    LegendConfig* legend,
    double horiz_padding,
    bool bottom,
    bool outside);

ReturnCode legend_configure(
    const Document& doc,
    const plist::PropertyList& plist,
    LegendConfig* config);

ReturnCode legend_draw(
    const LegendDefinition& legend,
    const LegendConfig& legend,
    const Rectangle& bbox,
    Layer* layer);

+5 −3
Original line number Diff line number Diff line
@@ -126,9 +126,6 @@ ReturnCode configure(

  // FIXME
  config.colour_scheme = doc.colour_scheme;
  config.legend.font = doc.font_sans;
  config.legend.border_colour = doc.border_colour;
  config.legend.text_colour = doc.text_colour;
  config.axis_top.font = doc.font_sans;
  config.axis_top.label_font_size = doc.font_size;
  config.axis_top.border_colour = doc.border_colour;
@@ -263,6 +260,11 @@ ReturnCode configure(
    return rc;
  }

  /* configure legend */
  if (auto rc = legend_configure(doc, plist, &config.legend); !rc) {
    return rc;
  }

  auto e = std::make_unique<Element>();
  e->draw = std::bind(
      &draw,
+1 −1
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ struct PlotConfig {
  Measure margins[4];
  ColourScheme colour_scheme;
  std::vector<PlotSeries> series;
  LegendDefinition legend;
  LegendConfig legend;
};

ReturnCode draw(