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

add the 'integer' formatter

parent 1fb246fa
Loading
Loading
Loading
Loading
+25 −16
Original line number Diff line number Diff line
@@ -224,25 +224,34 @@ ReturnCode format_configure_base_fixed(
  return OK;
}

Formatter format_string() {
  return [] (size_t idx, const std::string& v) -> std::string {
    return v;
  };
}

Formatter format_noop() {
  return [] (size_t idx, const std::string& v) -> std::string {
    return {};
Formatter format_integer() {
  return [] (size_t idx, const std::string& vs) -> std::string {
    auto v = value_to_float(vs);
    return std::to_string(int(v));
  };
}

ReturnCode format_configure_string(
ReturnCode format_configure_integer(
    const Expr* expr,
    Formatter* formatter) {
  *formatter = format_string();
  auto args = expr_collect(expr);
  if (args.size() != 0) {
    return errorf(
        ERROR,
        "invalid number of arguments for 'integer'; expected zero, but got: {}",
        args.size());
  }

  *formatter = format_integer();
  return OK;
}

Formatter format_noop() {
  return [] (size_t idx, const std::string& v) -> std::string {
    return v;
  };
}

Formatter format_custom(const std::vector<std::string>& values) {
  return [values] (size_t idx, const std::string& v) -> std::string {
    if (values.empty()) {
@@ -277,8 +286,8 @@ ReturnCode format_configure(
  if (!expr || !expr_is_list(expr)) {
    return errorf(
        ERROR,
        "invalid argument; expected a list but got: {}",
        "..."); // FIXME
        "invalid argument; expected a list (<format>), but got: {}",
        expr_inspect(expr));
  }

  expr = expr_get_list(expr);
@@ -299,8 +308,8 @@ ReturnCode format_configure(
    return format_configure_base_fixed(expr_next(expr), formatter);
  }

  if (expr_is_value(expr, "string")) {
    return format_configure_string(expr_next(expr), formatter);
  if (expr_is_value(expr, "integer")) {
    return format_configure_integer(expr_next(expr), formatter);
  }

  if (expr_is_value(expr, "custom")) {
@@ -314,7 +323,7 @@ ReturnCode format_configure(
      "  - scientific\n",
      "  - datetime\n",
      "  - base\n",
      "  - string\n",
      "  - integer\n",
      "  - custom\n",
      expr_inspect(expr));
}
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ Formatter format_decimal_fixed(size_t precision);
Formatter format_decimal_scientific(size_t precision);
Formatter format_datetime(const std::string& fmt);
Formatter format_base_fixed(unsigned base, size_t precision);
Formatter format_string();
Formatter format_integer();
Formatter format_custom(const std::vector<std::string>& values);
Formatter format_noop();

+1 −1
Original line number Diff line number Diff line
@@ -760,7 +760,7 @@ ReturnCode build(const Environment& env, const Expr* expr, ElementRef* elem) {

  if (!config->label_formatter) {
    if (config->scale.kind == ScaleKind::CATEGORICAL) {
      config->label_formatter = format_string();
      config->label_formatter = format_noop();
    } else {
      config->label_formatter = format_decimal_fixed(1);
    }
+7 −0
Original line number Diff line number Diff line
(set height 60px)
(set margin 1em)

(plot/axis
    label-format (integer)
    label-placement (linear 1)
    limit (1 16))
+36 −0
Original line number Diff line number Diff line
<svg xmlns="http://www.w3.org/2000/svg" width="900.000000" height="60.000000" viewBox="0 0 900 60">
  <rect width="900.000000" height="60.000000" fill="#ffffff"/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M19.5556 30 L880.444 30 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M19.5556 27.3333 L19.5556 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M76.9481 27.3333 L76.9481 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M134.341 27.3333 L134.341 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M191.733 27.3333 L191.733 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M249.126 27.3333 L249.126 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M306.519 27.3333 L306.519 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M363.911 27.3333 L363.911 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M421.304 27.3333 L421.304 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M478.696 27.3333 L478.696 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M536.089 27.3333 L536.089 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M593.481 27.3333 L593.481 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M650.874 27.3333 L650.874 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M708.267 27.3333 L708.267 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M765.659 27.3333 L765.659 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M823.052 27.3333 L823.052 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M880.444 27.3333 L880.444 32.6667 "/>
  <text x="15.477431" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1</text>
  <text x="72.870023" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2</text>
  <text x="130.262616" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">3</text>
  <text x="187.655208" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">4</text>
  <text x="245.047801" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">5</text>
  <text x="302.440394" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">6</text>
  <text x="359.832986" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">7</text>
  <text x="417.225579" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">8</text>
  <text x="474.618171" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">9</text>
  <text x="527.932639" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">10</text>
  <text x="585.872106" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">11</text>
  <text x="642.717824" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">12</text>
  <text x="700.110417" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">13</text>
  <text x="757.503009" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">14</text>
  <text x="814.895602" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">15</text>
  <text x="872.288194" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">16</text>
</svg>
 No newline at end of file
Loading