Commit 3b7906e0 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

bar chart data labels

parent 2befd758
Loading
Loading
Loading
Loading
+44 −5
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ class BarChart : public Drawable {
public:
  constexpr static const double kBarPadding = 0.3f; // FIXPAUL make configurable
  constexpr static const double kBarPaddingInner = 0.2f; // FIXPAUL make configurable
  constexpr static const int kLabelPadding = 8;

  enum kBarChartOrientation {
    O_VERTICAL,
@@ -241,10 +242,6 @@ void BarChart3D<TX, TY, TZ>::addSeries(Series3D<TX, TY, TZ>* series) {
          "BarChart error: invalid point in series. Z value must be greater "
          "or equal to Y value for all points");
    }

    if (series->hasProperty(Series::P_LABEL)) {
      printf("label=%s\n", series->getProperty(Series::P_LABEL, &point).c_str());
    }
  }

  data_.addSeries(series);
@@ -379,6 +376,10 @@ void BarChart3D<TX, TY, TZ>::renderHorizontalBars(

  for (const auto& bar : data->getData()) {
    auto x = x_domain->scaleRange(bar.x.value());
    auto label_x = viewport->paddingLeft();
    auto label_y = viewport->paddingTop() +
        ((1.0f - x.second) + (x.second - x.first) * 0.5f) *
            viewport->innerHeight();

    for (int n = 0; n < data->seriesCount(); n++) {
      auto y_min = y_domain->scale(bar.ys[n].first.value());
@@ -400,6 +401,20 @@ void BarChart3D<TX, TY, TZ>::renderHorizontalBars(
      }

      target->drawRect(dx, dy, dw, dh, seriesColor(n), "bar");

      if (dx + dw > label_x) {
        label_x = dx + dw;
      }
    }

    if (show_labels_) {
      target->drawText(
          bar.label,
          label_x + kLabelPadding,
          label_y,
          "start",
          "central",
          "label");
    }
  }
}
@@ -414,6 +429,9 @@ void BarChart3D<TX, TY, TZ>::renderVerticalBars(

  for (const auto& bar : data->getData()) {
    auto x = x_domain->scaleRange(bar.x.value());
    auto label_y = viewport->paddingTop() + viewport->innerHeight();
    auto label_x = viewport->paddingLeft() + (x.first +
        ((x.second - x.first) * 0.5)) * viewport->innerWidth();

    for (int n = 0; n < data->seriesCount(); n++) {
      auto y_min = y_domain->scale(bar.ys[n].first.value());
@@ -435,6 +453,20 @@ void BarChart3D<TX, TY, TZ>::renderVerticalBars(
      }

      target->drawRect(dx, dy, dw, dh, seriesColor(n), "bar");

      if (dy < label_y) {
        label_y = dy;
      }
    }

    if (show_labels_) {
      target->drawText(
          bar.label,
          label_x,
          label_y - kLabelPadding,
          "middle",
          "text-after-edge",
          "label");
    }
  }
}
@@ -452,7 +484,9 @@ void BarChart3D<TX, TY, TZ>::stackData(
          typename Series3D<TX, TY, TZ>::Point(
              bar.x,
              Series::Coord<TY>(cur),
              Series::Coord<TY>(cur + delta)), true);
              Series::Coord<TY>(cur + delta)),
          bar.label,
          true);

      cur += delta;
    }
@@ -532,6 +566,11 @@ void BarChart2D<TX, TY>::addSeries(Series2D<TX, TY>* series) {
          Series::P_LABEL,
          &series3d->getData().back(),
          series->getProperty(Series::P_LABEL, &point));
    } else {
        series3d->setProperty(
          Series::P_LABEL,
          &series3d->getData().back(),
          series->labelFor(&point));
    }
  }

+12 −3
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ public:
  struct JoinedPoint {
    JoinedPoint(const Series::Coord<TX>& x_) : x(x_) {}
    Series::Coord<TX> x;
    std::string label;
    std::vector<std::pair<Series::Coord<TY>, Series::Coord<TZ>>> ys;
  };

@@ -30,7 +31,7 @@ public:
  // FIXPAUL this should not be O(n^2)
  void addSeries(Series3D<TX, TY, TZ>* series) {
    for (const auto& point : series->getData()) {
      addPoint(point);
      addPoint(point, series->labelFor(&point));
    }

    for (auto& joined : data_) {
@@ -46,6 +47,7 @@ public:

  void addPoint(
      const typename Series3D<TX, TY, TZ>::Point& point,
      const std::string& label,
      bool force = false) {
    JoinedPoint* joined = nullptr;
    for (auto& candidate : data_) {
@@ -68,6 +70,13 @@ public:
    if (joined->ys.size() < num_series_ + 1 || force) {
      joined->ys.emplace_back(point.y_coord(), point.z_coord());
    }

    if (joined->label.size() == 0) {
      joined->label = label;
    } else if (joined->label != label) {
      joined->label.append(", ");
      joined->label.append(label);
    }
  }

  size_t size() const {