Commit 070f90f5 authored by Laura Schlimmer's avatar Laura Schlimmer
Browse files

implement chart title/subtitle

parent 0f63937b
Loading
Loading
Loading
Loading
+36 −1
Original line number Diff line number Diff line
@@ -23,11 +23,20 @@ Canvas::Canvas() :
    width_(800),
    height_(320) {}

void Canvas::setTitle(const std::string& title) {
  title_ = title;
}

void Canvas::setSubtitle(const std::string& subtitle) {
  subtitle_ = subtitle;
}

void Canvas::render(RenderTarget* target) const {
  // FIXPAUL: initialize from rendertarget
  Viewport viewport(width_, height_);

  target->beginChart(width_, height_, "fm-chart");
  renderTitle(target, &viewport);
  renderOutsideLegends(target, &viewport);
  renderAxes(target, &viewport);
  renderGrids(target, &viewport);
@@ -36,6 +45,32 @@ void Canvas::render(RenderTarget* target) const {
  target->finishChart();
}

void Canvas::renderTitle(RenderTarget* target, Viewport* viewport) const {
  if (title_.size() > 0) {
    target->drawText(
        title_,
        viewport->paddingLeft() + viewport->innerWidth() * 0.5f,
        viewport->paddingTop(),
        "middle",
        "text-before-edge",
        "chart-title");

    viewport->setPaddingTop(viewport->paddingTop() + kTitleLineHeight);
  }

  if (subtitle_.size() > 0) {
    target->drawText(
        subtitle_,
        viewport->paddingLeft() + viewport->innerWidth() * 0.5f,
        viewport->paddingTop(),
        "middle",
        "text-before-edge",
        "chart-subtitle");

    viewport->setPaddingTop(viewport->paddingTop() + kSubtitleLineHeight);
  }
}

void Canvas::renderCharts(RenderTarget* target, Viewport* viewport) const {
  for (const auto& drawable : drawables_) {
    drawable->render(target, viewport);
+21 −0
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@ public:
  static const int kAxisLabelWidth = 50; // FIXPAUL make configurable
  static const int kAxisTitleLength = 20; // FIXPAUL make configurable
  static const int kCharWidth = 6.0f; // FIXPAUL make configurable
  static const int kTitleLineHeight = 20; // FIXPAUL make configurable
  static const int kSubtitleLineHeight = 20; // FIXPAUL make configurable
  static const int kLegendLabelPadding = 20; // FIXPAUL make configurable
  static const int kLegendLineHeight = 20; // FIXPAUL make configurable
  static const int kLegendInsideVertPadding = 10;
@@ -41,6 +43,16 @@ public:

  Canvas();

  /**
   * Set the title for this canvas
   */
  void setTitle(const std::string& title);

  /**
   * Set the subtitle for this canvas
   */
  void setSubtitle(const std::string& subtitle);

  /**
   * FIXPAUL overcomplicated, just accept a ptr
   *
@@ -109,10 +121,17 @@ public:
   std::string renderSVG() const;

protected:

  // FIXPAUL
  int estimateTextLength(const std::string& str) const {
    return str.size() * kCharWidth;
  }

  /**
   * Render the chart title
   */
  void renderTitle(RenderTarget* target, Viewport* viewport) const;

  /**
   * Render the charts
   */
@@ -217,6 +236,8 @@ protected:
  std::vector<std::unique_ptr<LegendDefinition>> legends_;
  std::vector<std::unique_ptr<GridDefinition>> grids_;
  std::vector<std::unique_ptr<Drawable>> drawables_;
  std::string title_;
  std::string subtitle_;
};

}
+2 −2
Original line number Diff line number Diff line
@@ -17,11 +17,11 @@ namespace ui {
Drawable::Drawable(Canvas* canvas) : canvas_(canvas) {}

void Drawable::setTitle(const std::string& title) {
  printf("TITLE: %s\n", title.c_str());
  canvas_->setTitle(title);
}

void Drawable::setSubtitle(const std::string& subtitle) {
  printf("SUBTITLE: %s\n", subtitle.c_str());
  canvas_->setSubtitle(subtitle);
}

LegendDefinition* Drawable::addLegend(
+9 −0
Original line number Diff line number Diff line
@@ -43,6 +43,15 @@ static const std::string kStyleSheetDefault = R"(
    font-size: 10px;
  }

  .fm-chart .chart-title {
    font-size: 12px;
    font-weight: bold;
  }

  .fm-chart .chart-subtitle {
    font-size: 10px;
  }

  .fm-chart .legend text {
    font-size: 10px;
    fill: #333;