Commit ef2ef4ee authored by Paul Asmuth's avatar Paul Asmuth
Browse files

draw plot axes

parent 81e767e5
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -134,11 +134,12 @@ static Status renderAxisHorizontal(

Status renderAxis(
    const AxisDefinition& axis,
    AxisPosition axis_position,
    Layer* frame) {
  int padding = 80;

  Status rc;
  switch (axis.position) {
  switch (axis_position) {
    case AxisPosition::LEFT:
      rc = renderAxisVertical(
          axis,
+4 −2
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ enum class AxisLabelPlacement {

struct AxisDefinition {
  AxisDefinition();
  AxisPosition position;
  AxisMode mode;
  std::string title;
  std::vector<double> ticks;
@@ -56,7 +55,10 @@ struct AxisDefinition {
  double tick_length_rem;
};

Status renderAxis(const AxisDefinition& axis, Layer* frame);
Status renderAxis(
    const AxisDefinition& axis,
    AxisPosition axis_position,
    Layer* frame);


} // namespace signaltk
+48 −10
Original line number Diff line number Diff line
@@ -11,20 +11,36 @@

namespace signaltk {

static constexpr const char* ID = "plot";
PlotConfig::PlotConfig() :
    x_domain(PlotDomain::LINEAR),
    y_domain(PlotDomain::LINEAR),
    axis_top_enabled(true),
    axis_right_enabled(true),
    axis_bottom_enabled(true),
    axis_left_enabled(true) {}

ReturnCode PlotElement::configure(
    const PropertyList& plist,
    std::unique_ptr<Element>* elem) {
  elem->reset(new PlotElement());
ReturnCode renderPlot(const PlotConfig& config, Layer* frame) {
  // render axes
  if (config.axis_top_enabled) {
    if (auto rc = renderAxis(config.axis_top, AxisPosition::TOP, frame); rc) {
      return rc;
    }
  }

  return ReturnCode::success();
  if (config.axis_right_enabled) {
    if (auto rc = renderAxis(config.axis_right, AxisPosition::RIGHT, frame); rc) {
      return rc;
    }
  }

ReturnCode PlotElement::renderTo(Layer* frame) const {
  // render axes
  for (const auto& axis : config.axes) {
    if (auto rc = renderAxis(axis, frame); rc) {
  if (config.axis_bottom_enabled) {
    if (auto rc = renderAxis(config.axis_bottom, AxisPosition::BOTTOM, frame); rc) {
      return rc;
    }
  }

  if (config.axis_left_enabled) {
    if (auto rc = renderAxis(config.axis_left, AxisPosition::LEFT, frame); rc) {
      return rc;
    }
  }
@@ -32,5 +48,27 @@ ReturnCode PlotElement::renderTo(Layer* frame) const {
  return ReturnCode::success();
}

ReturnCode configurePlot(const PropertyList& plist, PlotConfig* config) {
  return ReturnCode::success();
}

ReturnCode PlotElement::configure(
    const PropertyList& plist,
    std::unique_ptr<Element>* elem) {
  PlotConfig config;
  if (auto rc = configurePlot(plist, &config); !rc.isSuccess()) {
    return rc;
  }

  *elem = std::make_unique<PlotElement>(config);
  return ReturnCode::success();
}

PlotElement::PlotElement(const PlotConfig& c) : config(c) {}

ReturnCode PlotElement::renderTo(Layer* frame) const {
  return renderPlot(config, frame);
}

} // namespace signaltk
+19 −5
Original line number Diff line number Diff line
@@ -19,15 +19,27 @@
namespace signaltk {

struct PlotConfig {
  PlotConfig();
  PlotDomain x_domain;
  double x_min;
  double x_max;
  std::optional<double> x_min;
  std::optional<double> x_max;
  PlotDomain y_domain;
  double y_min;
  double y_max;
  std::vector<AxisDefinition> axes;
  std::optional<double> y_min;
  std::optional<double> y_max;
  AxisDefinition axis_top;
  bool axis_top_enabled;
  AxisDefinition axis_right;
  bool axis_right_enabled;
  AxisDefinition axis_bottom;
  bool axis_bottom_enabled;
  AxisDefinition axis_left;
  bool axis_left_enabled;
};

ReturnCode renderPlot(const PlotConfig& config, Layer* frame);

ReturnCode configurePlot(const PropertyList& plist, PlotConfig* config);

class PlotElement : public Element {
public:

@@ -35,6 +47,8 @@ public:
      const PropertyList& plist,
      std::unique_ptr<Element>* elem);

  explicit PlotElement(const PlotConfig& config);

  ReturnCode renderTo(Layer* frame) const override;

protected: