Commit 0970030e authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved axis label and tick drawing

- draw all axis labels
- make tick length and label padding configurable in REM
parent 698b6d84
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ Status drawText(
    Layer* layer) {
  FontInfo font_info {
    .font_file = "/Library/Fonts/Arial.ttf",
    .font_size = 12
    .font_size = text_style.font_size
  };

  std::vector<GlyphPlacement> glyphs;
+21 −21
Original line number Diff line number Diff line
@@ -17,69 +17,69 @@ class Viewport {
public:

  Viewport(
      uint32_t width,
      uint32_t height) :
      Viewport(width, height, std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>(0,0,0,0)) {}
      double width,
      double height) :
      Viewport(width, height, std::tuple<double, double, double, double>(0,0,0,0)) {}

  Viewport(
      uint32_t width,
      uint32_t height,
      const std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>& padding) :
      double width,
      double height,
      const std::tuple<double, double, double, double>& padding) :
      width_(width),
      height_(height),
      padding_(padding) {}

  uint32_t innerWidth() const {
  double innerWidth() const {
    return width_ - paddingRight() - paddingLeft();
  }

  uint32_t innerHeight() const {
  double innerHeight() const {
    return height_ - paddingTop() - paddingBottom();
  }

  uint32_t paddingTop() const {
  double paddingTop() const {
    return std::get<0>(padding_);
  }

  uint32_t paddingRight() const {
  double paddingRight() const {
    return std::get<1>(padding_);
  }

  uint32_t paddingBottom() const {
  double paddingBottom() const {
    return std::get<2>(padding_);
  }

  uint32_t paddingLeft() const {
  double paddingLeft() const {
    return std::get<3>(padding_);
  }

  const std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>& padding() {
  const std::tuple<double, double, double, double>& padding() {
    return padding_;
  }

  void setPadding(const std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>& padding) {
  void setPadding(const std::tuple<double, double, double, double>& padding) {
    padding_ = padding;
  }

  void setPaddingTop(uint32_t val) {
  void setPaddingTop(double val) {
    std::get<0>(padding_) = val;
  }

  void setPaddingRight(uint32_t val) {
  void setPaddingRight(double val) {
    std::get<1>(padding_) = val;
  }

  void setPaddingBottom(uint32_t val) {
  void setPaddingBottom(double val) {
    std::get<2>(padding_) = val;
  }

  void setPaddingLeft(uint32_t val) {
  void setPaddingLeft(double val) {
    std::get<3>(padding_) = val;
  }

  uint32_t width_;
  uint32_t height_;
  std::tuple<uint32_t, uint32_t, uint32_t, uint32_t> padding_;
  double width_;
  double height_;
  std::tuple<double, double, double, double> padding_;
};

}
+39 −7
Original line number Diff line number Diff line
@@ -17,7 +17,10 @@ namespace chart {
AxisDefinition::AxisDefinition() :
    enabled_(false),
    has_ticks_(false),
    has_labels_(false) {}
    has_labels_(false),
    label_padding_horiz_rem(kDefaultLabelPaddingHorizREM),
    label_padding_vert_rem(kDefaultLabelPaddingVertREM),
    tick_length_rem(kDefaultTickLengthREM) {}

void AxisDefinition::addTick(double tick_position) {
  ticks_.push_back(tick_position);
@@ -87,18 +90,26 @@ Status renderAxisVertical(
  for (const auto& tick : axis_config.getTicks()) {
    auto y = y0 + (y1 - y0) * tick;
    StrokeStyle style;
    strokeLine(target, x, y, x + kTickLength * label_placement, y, style);
    strokeLine(
        target,
        x,
        y,
        x + from_rem(*target, axis_config.tick_length_rem) * label_placement,
        y,
        style);
  }

  /* draw labels */
  auto label_padding = from_rem(*target, axis_config.label_padding_horiz_rem);
  for (const auto& label : axis_config.getLabels()) {
    auto [ tick, label_text ] = label;
    auto y = y0 + (y1 - y0) * tick;
    auto sy = y0 + (y1 - y0) * tick;
    auto sx = x + label_padding * label_placement;

    TextStyle style;
    style.halign = TextHAlign::LEFT;
    style.valign = TextVAlign::TOP;
    if (auto rc = drawText(label_text, x, y, style, target); rc != OK) {
    style.halign = label_placement > 0 ? TextHAlign::LEFT : TextHAlign::RIGHT;
    style.valign = TextVAlign::MIDDLE;
    if (auto rc = drawText(label_text, sx, sy, style, target); rc != OK) {
      return rc;
    }
  }
@@ -134,7 +145,28 @@ Status renderAxisHorizontal(
  for (const auto& tick : axis_config.getTicks()) {
    auto x = x0 + (x1 - x0) * tick;
    StrokeStyle style;
    strokeLine(target, x, y, x, y + kTickLength * label_placement, style);
    strokeLine(
        target,
        x,
        y,
        x,
        y + from_rem(*target, axis_config.tick_length_rem) * label_placement,
        style);
  }

  /* draw labels */
  auto label_padding = from_rem(*target, axis_config.label_padding_vert_rem);
  for (const auto& label : axis_config.getLabels()) {
    auto [ tick, label_text ] = label;
    auto sx = x0 + (x1 - x0) * tick;
    auto sy = y + label_padding * label_placement;

    TextStyle style;
    style.halign = TextHAlign::CENTER;
    style.valign = label_placement > 0 ? TextVAlign::TOP : TextVAlign::BOTTOM;
    if (auto rc = drawText(label_text, sx, sy, style, target); rc != OK) {
      return rc;
    }
  }

  return OK;
+6 −5
Original line number Diff line number Diff line
@@ -17,11 +17,9 @@
namespace signaltk {
namespace chart {

static const int kAxisPadding = 0; // FIXPAUL make configurable
static const int kTickLength = 5; // FIXPAUL make configurable
static const int kAxisLabelHeight = 25; // FIXPAUL make configurable
static const int kAxisLabelWidth = 50; // FIXPAUL make configurable
static const int kAxisTitleLength = 20; // FIXPAUL make configurable
static const double kDefaultLabelPaddingVertREM = 0.8f;
static const double kDefaultLabelPaddingHorizREM = 1.0f;
static const double kDefaultTickLengthREM = 0.4f;

class AxisDefinition {
public:
@@ -122,6 +120,9 @@ public:
  std::vector<std::pair<double, std::string>> labels_;
  bool has_labels_;
  kLabelPlacement label_placement;
  double label_padding_horiz_rem;
  double label_padding_vert_rem;
  double tick_length_rem;
};

struct AxisDefinitions {
+3 −3
Original line number Diff line number Diff line
@@ -29,9 +29,9 @@ int main(int argc, char** argv) {
    axis.addTick(0.6);
    axis.addTick(0.8);
    axis.addTick(1.0);
    axis.addLabel(0.0, "fnord");
    axis.addLabel(0.2, "blah");
    axis.addLabel(0.4, "xxx");
    axis.addLabel(0.0, "a");
    axis.addLabel(0.2, "b");
    axis.addLabel(0.4, "c");
    axis.addLabel(0.6, "d");
    axis.addLabel(0.8, "e");
    axis.addLabel(1.0, "f");
Loading