Commit 9b96a98e authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add the 'exponential-steps' scale layout

parent 63bcebe2
Loading
Loading
Loading
Loading
+74 −3
Original line number Diff line number Diff line
@@ -354,6 +354,38 @@ ReturnCode scale_layout_linear(
  return OK;
}

ReturnCode scale_layout_exponential_steps(
    const ScaleConfig& domain,
    const Formatter& label_format,
    ScaleLayout* layout,
    double base,
    size_t steps) {
  auto begin = scale_min(domain);
  auto end = scale_max(domain);

  for (size_t idx = 0, exp = 0; ; ++idx) {
    auto v = pow(base, exp++);
    auto vn = pow(base, exp);

    if (v < begin) {
      continue;
    }

    if (v > end) {
      break;
    }

    for (size_t s = 0; s < steps - 1; ++s) {
      auto vs = v + (s / double(steps - 1)) * (vn - v);
      auto vp = scale_translate(domain, vs);
      layout->positions.emplace_back(vp);
      layout->labels.emplace_back(label_format(idx++, std::to_string(v))); // FIXME
    }
  }

  return OK;
}

ReturnCode scale_layout_exponential(
    const ScaleConfig& domain,
    const Formatter& label_format,
@@ -561,6 +593,39 @@ ReturnCode scale_configure_layout_linear(
  return OK;
}

ReturnCode scale_configure_layout_exponential_steps(
    const Expr* expr,
    ScaleLayoutFn* layout) {
  auto args = expr_collect(expr);

  if (args.size() != 2) {
    return errorf(
        ERROR,
        "invalid number of arguments for 'exponential-steps'; expected two, got: {}",
        args.size());
  }

  double base;
  if (auto rc = expr_to_float64(args[0], &base); !rc) {
    return rc;
  }

  double steps;
  if (auto rc = expr_to_float64(args[1], &steps); !rc) {
    return rc;
  }

  *layout = bind(
      &scale_layout_exponential_steps,
      _1,
      _2,
      _3,
      base,
      steps);

  return OK;
}

ReturnCode scale_configure_layout_exponential(
    const Expr* expr,
    ScaleLayoutFn* layout) {
@@ -573,8 +638,8 @@ ReturnCode scale_configure_layout_exponential(
        args.size());
  }

  double step;
  if (auto rc = expr_to_float64(args[0], &step); !rc) {
  double base;
  if (auto rc = expr_to_float64(args[0], &base); !rc) {
    return rc;
  }

@@ -583,7 +648,7 @@ ReturnCode scale_configure_layout_exponential(
      _1,
      _2,
      _3,
      step);
      base);

  return OK;
}
@@ -651,6 +716,10 @@ ReturnCode scale_configure_layout(
    return scale_configure_layout_exponential(expr_next(expr), layout);
  }

  if (expr_is_value(expr, "exponential-steps")) {
    return scale_configure_layout_exponential_steps(expr_next(expr), layout);
  }

  if (expr_is_value(expr, "subdivide")) {
    return scale_configure_layout_subdivide(expr_next(expr), layout);
  }
@@ -672,6 +741,8 @@ ReturnCode scale_configure_layout(
      "  - linear-align\n"
      "  - linear-alignat\n"
      "  - linear-interval\n"
      "  - exponential\n"
      "  - exponential-steps\n"
      "  - subdivide\n"
      "  - categorical\n"
      "  - categorical-bounds\n");
+7 −0
Original line number Diff line number Diff line
@@ -113,6 +113,13 @@ ReturnCode scale_layout_linear(
    ScaleLayout* layout,
    double step);

ReturnCode scale_layout_exponential_steps(
    const ScaleConfig& domain,
    const Formatter& label_format,
    ScaleLayout* layout,
    double base,
    size_t steps);

ReturnCode scale_layout_exponential(
    const ScaleConfig& domain,
    const Formatter& label_format,
+7 −0
Original line number Diff line number Diff line
(set height 60px)
(set margin 1em)

(plot/axis
    scale (logarithmic 10)
    tick-placement (exponential-steps 10 10)
    limit (0 10000))
+54 −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="M84.3439 27.3333 L84.3439 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M122.243 27.3333 L122.243 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M149.132 27.3333 L149.132 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M169.989 27.3333 L169.989 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M187.031 27.3333 L187.031 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M201.439 27.3333 L201.439 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M213.921 27.3333 L213.921 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M224.93 27.3333 L224.93 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M234.778 27.3333 L234.778 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M299.566 27.3333 L299.566 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M337.465 27.3333 L337.465 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M364.354 27.3333 L364.354 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M385.212 27.3333 L385.212 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M402.253 27.3333 L402.253 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M416.662 27.3333 L416.662 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M429.143 27.3333 L429.143 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M440.152 27.3333 L440.152 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M450 27.3333 L450 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M514.788 27.3333 L514.788 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M552.687 27.3333 L552.687 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M579.577 27.3333 L579.577 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M600.434 27.3333 L600.434 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M617.475 27.3333 L617.475 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M631.884 27.3333 L631.884 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M644.365 27.3333 L644.365 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M655.374 27.3333 L655.374 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M665.222 27.3333 L665.222 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M730.011 27.3333 L730.011 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M767.909 27.3333 L767.909 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M794.799 27.3333 L794.799 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M815.656 27.3333 L815.656 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M832.698 27.3333 L832.698 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M847.106 27.3333 L847.106 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M859.587 27.3333 L859.587 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M870.596 27.3333 L870.596 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M880.444 27.3333 L880.444 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M945.233 27.3333 L945.233 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M983.132 27.3333 L983.132 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M1010.02 27.3333 L1010.02 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M1030.88 27.3333 L1030.88 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M1047.92 27.3333 L1047.92 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M1062.33 27.3333 L1062.33 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M1074.81 27.3333 L1074.81 32.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M1085.82 27.3333 L1085.82 32.6667 "/>
  <text x="9.360243" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1.0</text>
  <text x="220.504340" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">10.0</text>
  <text x="431.648438" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">100.0</text>
  <text x="642.792535" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1000.0</text>
  <text x="853.936632" y="55.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">10000.0</text>
</svg>
 No newline at end of file
+12 −0
Original line number Diff line number Diff line
@@ -63,6 +63,18 @@ The `exponential` layout will place a 'tick' every `B^I` units.
Example: [`charts-reference/scale_layout_exponential`](/examples/charts-reference/scale_layout_exponential):
[![scale_layout_exponential.svg](/examples/charts-reference/scale_layout_exponential.svg)](/examples/charts-reference/scale_layout_exponential)

---
### `exponential-steps`

The `exponential-steps` layout will go up in `B^I` exponential steps like the
`exponential` layout, but will subdivide each "step" linearly into `N` sub-steps.

    (exponential-steps <B> <N>)

Example: [`charts-reference/scale_layout_exponential_steps`](/examples/charts-reference/scale_layout_exponential_steps):
[![scale_layout_exponential_steps.svg](/examples/charts-reference/scale_layout_exponential_steps.svg)](/examples/charts-reference/scale_layout_exponential_steps)


---
### `subdivide`