Commit 2869c3a5 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

render top left legend

parent 2b2fbd83
Loading
Loading
Loading
Loading
+57 −4
Original line number Diff line number Diff line
@@ -386,7 +386,7 @@ void Canvas::renderOutsideLegends(
  for (const auto& legend : legends_) {
    target->beginGroup("legend");

    renderTopRightLegend(
    renderTopLeftLegend(
        target,
        viewport,
        legend.get(),
@@ -405,7 +405,7 @@ void Canvas::renderInsideLegends(
    target->beginGroup("legend");
    viewport->setPaddingTop(viewport->paddingTop() + kLegendInsideVertPadding);

    renderTopRightLegend(
    renderTopLeftLegend(
        target,
        viewport,
        legend.get(),
@@ -424,8 +424,7 @@ void Canvas::renderTopRightLegend(
    LegendDefinition* legend,
    double horiz_padding) const {
  double height = 0.0f;

  std::string title = "legend title";
  std::string title = legend->title();

  target->drawText(
    title,
@@ -472,6 +471,60 @@ void Canvas::renderTopRightLegend(
  viewport->setPaddingTop(viewport->paddingTop() + height);
}

void Canvas::renderTopLeftLegend(
    RenderTarget* target,
    Viewport* viewport,
    LegendDefinition* legend,
    double horiz_padding) const {
  double height = 0.0f;
  std::string title = legend->title();

  target->drawText(
    title,
    viewport->paddingLeft() + viewport->innerWidth() - horiz_padding,
    viewport->paddingTop(),
    "end",
    "text-before-edge",
    "title");

  auto lx = viewport->paddingLeft() + horiz_padding;
  auto lx_boundary = viewport->paddingLeft() + viewport->innerWidth() -
      horiz_padding - estimateTextLength(title) - kLegendLabelPadding;

  for (const auto& entry : legend->entries()) {
    auto this_len = estimateTextLength(entry.first) + kLegendLabelPadding;

    /* line wrap */
    if (lx + this_len > lx_boundary) {
      lx = viewport->paddingLeft() + horiz_padding;
      lx_boundary = viewport->paddingLeft() + viewport->innerWidth() -
          horiz_padding;
      height += kLegendLineHeight;
    }

    target->drawPoint(
        lx,
        viewport->paddingTop() + kLegendPointY + height,
        "circle",
        kLegendPointSize,
        entry.second,
        "point");

    target->drawText(
      entry.first,
      lx + kLegendPointWidth,
      viewport->paddingTop() + height,
      "start",
      "text-before-edge",
      "label");

    lx += this_len;
  }

  height += kLegendLineHeight;
  viewport->setPaddingTop(viewport->paddingTop() + height);
}

std::string Canvas::renderSVG() const {
  auto output = util::OutputStream::getStdout();
  SVGTarget target(output.get());
+6 −0
Original line number Diff line number Diff line
@@ -179,6 +179,12 @@ protected:
      LegendDefinition* legend,
      double horiz_padding) const;

  void renderTopLeftLegend(
      RenderTarget* target,
      Viewport* viewport,
      LegendDefinition* legend,
      double horiz_padding) const;

  // FIXPAUL this belongs into the rendertarget
  int width_;
  int height_;
+4 −0
Original line number Diff line number Diff line
@@ -44,6 +44,10 @@ public:
      kPlacement placement,
      const std::string& title);

  const std::string title() const {
    return "my long legend title";
  }

  const std::vector<std::pair<std::string, std::string>> entries() const {
    std::vector<std::pair<std::string, std::string>> entries;
    entries.emplace_back("fnord1", "color1");