Commit 0be6ef38 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

move legend code to elements/legend.{h,cc}

parent 18842956
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ add_library(plotfxlib STATIC
    elements/plot.cc
    elements/plot_axis.cc
    elements/plot_lines.cc
    elements/legend.cc
    common/config_helpers.cc
    common/domain.cc
    common/format.cc

common/layout/legend.h

deleted100644 → 0
+0 −116
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2011-2014 Paul Asmuth, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef _libstx_CANVAS_H
#define _libstx_CANVAS_H
#include <memory>
#include <vector>
#include <string>
#include <tuple>
#include "axisdefinition.h"
#include "drawable.h"
#include "legenddefinition.h"

namespace plotfx {
namespace chart {
class Layer;

class Legend {
public:
  static const int kLegendLabelPadding = 20; // FIXPAUL make configurable
  static const int kLegendLineHeight = 20; // FIXPAUL 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;

  Legend();

  /**
   * Add a leged to this canvas. Usually legends are not specified manually via
   * this method but through one of the Chart subclasses. However it is safe
   * to call this method to explicitly define a custom legend.
   *
   * The returned pointer is owned by the canvas instance and must not be freed
   * by the caller.
   */
  LegendDefinition* addLegend(
      LegendDefinition::kVerticalPosition vert_pos,
      LegendDefinition::kHorizontalPosition horiz_pos,
      LegendDefinition::kPlacement placement,
      const std::string& title);
  /**
   * Return the legend or nullptr
   */
  LegendDefinition* legend() const;

  /**
   * Render the contents of this canvas to the provided render target
   *
   * @param target a Layer subclass instance. Does not transfer ownership
   */
  void render(Layer* target) const;

protected:

  /**
   * Render the legends
   */
  void renderOutsideLegends(Layer* target, Viewport* viewport) const;

  /**
   * Render the legends
   */
  void renderInsideLegends(Layer* target, Viewport* viewport) const;

  void renderRightLegend(
      Layer* target,
      Viewport* viewport,
      LegendDefinition* legend,
      double horiz_padding,
      bool bottom,
      bool outside) const;

  void renderLeftLegend(
      Layer* target,
      Viewport* viewport,
      LegendDefinition* legend,
      double horiz_padding,
      bool bottom,
      bool outside) const;

  std::vector<std::unique_ptr<LegendDefinition>> legends_;
};

}
}
#endif
+50 −16
Original line number Diff line number Diff line
@@ -28,21 +28,56 @@
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <stdlib.h>
#include "../core/layer.h"
#include "../graphics/svgtarget.h"
#include "canvas.h"
#include "domain.h"
#include "graphics/layer.h"
#include "common/domain.h"
#include "legend.h"

namespace plotfx {
namespace chart {

Legend::Legend() :
    width_(800),
    height_(320) {}
LegendDefinition::LegendDefinition(
    kVerticalPosition vert_pos,
    kHorizontalPosition horiz_pos,
    kPlacement placement,
    const std::string& title) :
    vert_pos_(vert_pos),
    horiz_pos_(horiz_pos),
    placement_(placement),
    title_ (title) {}

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(
    const std::string& name,
    const std::string& color,
    const std::string& shape /* = "circle" */) {
  entries_.emplace_back(name, color, shape);
}

const std::vector<std::tuple<std::string, std::string, std::string>>
    LegendDefinition::entries() const {
  return entries_;
}

/*
void Legend::renderOutsideLegends(
    Layer* target,
    Viewport* viewport) const {
    const Rectangle& clip) const {
  for (const auto& legend : legends_) {
    if (legend->placement() != LegendDefinition::LEGEND_OUTSIDE) {
      continue;
@@ -113,7 +148,7 @@ void Legend::renderOutsideLegends(

void Legend::renderInsideLegends(
    Layer* target,
    Viewport* viewport) const {
    const Rectangle& clip) const {
  auto orig_padding = viewport->padding();

  for (const auto& legend : legends_) {
@@ -156,7 +191,7 @@ void Legend::renderInsideLegends(

void Legend::renderRightLegend(
    Layer* target,
    Viewport* viewport,
    const Rectangle& clip,
    LegendDefinition* legend,
    double horiz_padding,
    bool bottom,
@@ -187,7 +222,6 @@ void Legend::renderRightLegend(
    auto this_len = estimateTextLength(std::get<0>(entry)) +
        kLegendLabelPadding;

    /* line wrap */
    if (lx - this_len < lx_boundary) {
      lx = viewport->paddingLeft() + viewport->innerWidth() - horiz_padding;
      height += bottom ? -1 * kLegendLineHeight : kLegendLineHeight;
@@ -233,7 +267,7 @@ void Legend::renderRightLegend(

void Legend::renderLeftLegend(
    Layer* target,
    Viewport* viewport,
    const Rectangle& clip,
    LegendDefinition* legend,
    double horiz_padding,
    bool bottom,
@@ -264,7 +298,6 @@ void Legend::renderLeftLegend(
    auto this_len = estimateTextLength(std::get<0>(entry)) + 
        kLegendLabelPadding;

    /* line wrap */
    if (lx + this_len > lx_boundary) {
      lx = viewport->paddingLeft() + horiz_padding;
      lx_boundary = viewport->paddingLeft() + viewport->innerWidth() -
@@ -326,6 +359,7 @@ LegendDefinition* Legend::legend() const {
    return legends_.back().get();
  }
}
*/

} // namespace plotfx
}
}
+38 −8
Original line number Diff line number Diff line
@@ -27,14 +27,24 @@
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef _libstx_UI_LEGENDDEFINITION_H
#define _libstx_UI_LEGENDDEFINITION_H
#include <tuple>
#include <string>
#pragma once
#include <memory>
#include <vector>
#include <string>
#include <tuple>
#include "graphics/layer.h"

namespace plotfx {
namespace chart {

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:
@@ -84,6 +94,26 @@ protected:
  std::vector<std::tuple<std::string, std::string, std::string>> entries_;
};

}
}
#endif
void renderOutsideLegends(Layer* target, const Rectangle& clip);

void renderInsideLegends(Layer* target, const Rectangle& clip);

void renderRightLegend(
    Layer* target,
    const Rectangle& clip,
    LegendDefinition* legend,
    double horiz_padding,
    bool bottom,
    bool outside);

void renderLeftLegend(
    Layer* target,
    const Rectangle& clip,
    LegendDefinition* legend,
    double horiz_padding,
    bool bottom,
    bool outside);


} // namespace plotfx

elements/legenddefinition.cc

deleted100644 → 0
+0 −76
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2011-2014 Paul Asmuth, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "legenddefinition.h"

namespace plotfx {
namespace chart {

LegendDefinition::LegendDefinition(
    kVerticalPosition vert_pos,
    kHorizontalPosition horiz_pos,
    kPlacement placement,
    const std::string& title) :
    vert_pos_(vert_pos),
    horiz_pos_(horiz_pos),
    placement_(placement),
    title_ (title) {}

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(
    const std::string& name,
    const std::string& color,
    const std::string& shape /* = "circle" */) {
  entries_.emplace_back(name, color, shape);
}

const std::vector<std::tuple<std::string, std::string, std::string>>
    LegendDefinition::entries() const {
  return entries_;
}

}
}
Loading