Commit 8cbffa3e authored by Paul Asmuth's avatar Paul Asmuth
Browse files

port plot_labels to new API

parent a75a04e5
Loading
Loading
Loading
Loading
+69 −29
Original line number Diff line number Diff line
@@ -43,7 +43,9 @@ namespace labels {
static const double kDefaultLineWidthPT = 2;
static const double kDefaultLabelPaddingEM = 0.8;

PlotLabelsConfig::PlotLabelsConfig() {}
PlotLabelsConfig::PlotLabelsConfig() :
    x_scale("x"),
    y_scale("y") {}

ReturnCode draw_labels(
    const PlotConfig& plot,
@@ -51,32 +53,62 @@ ReturnCode draw_labels(
    const Document& doc,
    const Rectangle& clip,
    Layer* layer) {
  ///* draw labels */
  //for (size_t i = 0; i < column_x->data.size(); ++i) {
  //  if (i >= config.labels.size()) {
  //    break;
  //  }

  //  const auto& label_text = config.labels[i];
  //  auto label_padding = measure_or(
  //      config.label_padding,
  //      from_em(kDefaultLabelPaddingEM, config.label_font_size));

  //  Point p(
  //      clip.x + x[i] * clip.w,
  //      clip.y + (1.0 - y[i]) * clip.h - label_padding);

  //  TextStyle style;
  //  style.font = config.label_font;
  //  style.color = config.label_color;
  //  style.font_size = config.label_font_size;

  //  auto ax = HAlign::CENTER;
  //  auto ay = VAlign::BOTTOM;
  //  if (auto rc = drawTextLabel(label_text, p, ax, ay, style, layer); rc != OK) {
  //    return rc;
  //  }
  //}
  /* fetch columns */
  const DataColumn* column_x = nullptr;
  if (auto rc = column_find(plot.data, config.x_key, &column_x); !rc) {
    return rc;
  }

  const DataColumn* column_y = nullptr;
  if (auto rc = column_find(plot.data, config.y_key, &column_y); !rc) {
    return rc;
  }

  const DataColumn* labels = nullptr;
  if (auto rc = column_find(plot.data, config.label_key, &labels); !rc) {
    return rc;
  }

  if (column_x->data.size() != column_y->data.size() ||
      column_x->data.size() != labels->data.size()) {
    return ReturnCode::error("EARG", "columns have differing lengths");
  }

  /* fetch domains */
  auto domain_x = domain_find(plot.scales, config.x_scale);
  if (!domain_x) {
    return ReturnCode::errorf("EARG", "scale not found: $0", config.x_scale);
  }

  auto domain_y = domain_find(plot.scales, config.y_scale);
  if (!domain_y) {
    return ReturnCode::errorf("EARG", "scale not found: $0", config.y_scale);
  }

  /* draw labels */
  auto x = domain_translate(*domain_x, column_x->data);
  auto y = domain_translate(*domain_y, column_y->data);
  for (size_t i = 0; i < column_x->data.size(); ++i) {
    const auto& label_text = labels->data[i];
    auto label_padding = measure_or(
        config.label_padding,
        from_em(kDefaultLabelPaddingEM, config.label_font_size));

    Point p(
        clip.x + x[i] * clip.w,
        clip.y + (1.0 - y[i]) * clip.h - label_padding);

    TextStyle style;
    style.font = config.label_font;
    style.color = config.label_color;
    style.font_size = config.label_font_size;

    auto ax = HAlign::CENTER;
    auto ay = VAlign::BOTTOM;
    if (auto rc = drawTextLabel(label_text, p, ax, ay, style, layer); rc != OK) {
      return rc;
    }
  }

  return OK;
}
@@ -87,10 +119,18 @@ ReturnCode configure(const plist::Property& prop, const Document& doc, PlotConfi
  }

  PlotLabelsConfig layer;
  layer.label_font = doc.font_sans; // FIXME
  layer.label_font_size = doc.font_size; // FIXME
  layer.x_key = config->default_x_key;
  layer.y_key = config->default_y_key;
  layer.label_key = config->default_y_key;
  layer.label_font = doc.font_sans;
  layer.label_font_size = doc.font_size;

  static const ParserDefinitions pdefs = {
    {"x", configure_key(&layer.x_key)},
    {"x-scale", std::bind(&configure_string, std::placeholders::_1, &layer.x_scale)},
    {"y", configure_key(&layer.y_key)},
    {"y-scale", std::bind(&configure_string, std::placeholders::_1, &layer.y_scale)},
    {"label", configure_key(&layer.label_key)},
  };

  if (auto rc = parseAll(*prop.next, pdefs); !rc) {
+5 −0
Original line number Diff line number Diff line
@@ -45,6 +45,11 @@ namespace labels {

struct PlotLabelsConfig {
  PlotLabelsConfig();
  std::string x_key;
  std::string x_scale;
  std::string y_key;
  std::string y_scale;
  std::string label_key;
  FontInfo label_font;
  Measure label_padding;
  Measure label_font_size;
+3 −4
Original line number Diff line number Diff line
@@ -99,10 +99,9 @@ ReturnCode draw_lines(
  }

  /* draw lines */
  for (const auto& group : groups) {
  auto x = domain_translate(*domain_x, column_x->data);
  auto y = domain_translate(*domain_y, column_y->data);

  for (const auto& group : groups) {
    Color color;
    if (auto rc = resolve_slot(
          config.line_color,
@@ -153,7 +152,7 @@ ReturnCode configure_legend(
  }

  if (column_x->data.size() != column_y->data.size()) {
    return ERROR_INVALID_ARGUMENT;
    return ReturnCode::error("EARG", "columns have differing lengths");
  }

  const DataColumn* column_group = nullptr;
+1 −2
Original line number Diff line number Diff line
@@ -65,8 +65,7 @@ ReturnCode draw_points(
  }

  if (column_x->data.size() != column_y->data.size()) {
    // FIXME error msg
    return ERROR_INVALID_ARGUMENT;
    return ReturnCode::error("EARG", "columns have differing lengths");
  }

  /* fetch domains */
+23 −10
Original line number Diff line number Diff line
plot {
  data: csv('tests/testdata/city_temperatures_small.csv');
  x: $1;
  y: $2;
  group: $0;

  axis-x-format: string;
  axis-y-min: -10;
  axis-y-max: 32;
  axis-top: off;
  axis-right: off;
  margin-right: 2.4em;

  ymin: -10;
  ymax: 32;
  xdomain: categorical;
  xdomain-format: string;
  legend {
    position: top left inside;
  }

  series {
    title: "London";
    xs: csv(examples/data/city_temperatures_london.csv, 0);
    ys: csv(examples/data/city_temperatures_london.csv, 1);
    labels: csv(examples/data/city_temperatures_london.csv, 2);
  layer {
    type: lines;
    line-color: $0;
  }

  layer {
    type: points;
    point-size: 3.3pt;
    point-color: $0;
  }

  layer {
    type: labels;
    label: $3;
  }
}
Loading