Commit 02e966b0 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add the 'custom' label formatter

parent e73ef68c
Loading
Loading
Loading
Loading
+31 −4
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@
namespace fviz {

Formatter format_decimal_scientific(size_t precision) {
  return [precision] (const std::string& v) -> std::string {
  return [precision] (size_t idx, const std::string& v) -> std::string {
    std::stringstream s;
    s << std::scientific << std::setprecision(precision) << value_to_float(v);
    return s.str();
@@ -54,7 +54,7 @@ ReturnCode format_configure_decimal_scientific(
}

Formatter format_decimal_fixed(size_t precision) {
  return [precision] (const std::string& v) -> std::string {
  return [precision] (size_t idx, const std::string& v) -> std::string {
    std::stringstream s;
    s << std::fixed << std::setprecision(precision) << value_to_float(v);
    return s.str();
@@ -86,7 +86,7 @@ ReturnCode format_configure_decimal_fixed(
}

Formatter format_datetime(const std::string& fmt) {
  return [fmt] (const std::string& v) -> std::string {
  return [fmt] (size_t idx, const std::string& v) -> std::string {
    UnixTime v_t(value_to_float(v) * 1000000);
    return v_t.toString(fmt.c_str());
  };
@@ -113,7 +113,7 @@ ReturnCode format_configure_datetime(
}

Formatter format_string() {
  return [] (const std::string& v) -> std::string {
  return [] (size_t idx, const std::string& v) -> std::string {
    return v;
  };
}
@@ -125,6 +125,28 @@ ReturnCode format_configure_string(
  return OK;
}

Formatter format_custom(const std::vector<std::string>& values) {
  return [values] (size_t idx, const std::string& v) -> std::string {
    if (values.empty()) {
      return "";
    } else {
      return values.at(idx % values.size());
    }
  };
}

ReturnCode format_configure_custom(
    const Expr* expr,
    Formatter* formatter) {
  std::vector<std::string> values;
  for (; expr; expr = expr_next(expr)) {
    values.emplace_back(expr_get_value(expr));
  }

  *formatter = format_custom(values);
  return OK;
}

ReturnCode format_configure(
    const Expr* expr,
    Formatter* formatter) {
@@ -153,6 +175,10 @@ ReturnCode format_configure(
    return format_configure_string(expr_next(expr), formatter);
  }

  if (expr_is_value(expr, "custom")) {
    return format_configure_custom(expr_next(expr), formatter);
  }

  return errorf(
      ERROR,
      "invalid value '{}', expected one of: \n"
@@ -160,6 +186,7 @@ ReturnCode format_configure(
      "  - scientific\n",
      "  - datetime\n",
      "  - string\n",
      "  - custom\n",
      expr_inspect(expr));
}

+2 −1
Original line number Diff line number Diff line
@@ -19,12 +19,13 @@

namespace fviz {

using Formatter = std::function<std::string (const std::string&)>;
using Formatter = std::function<std::string (size_t idx, const std::string&)>;

Formatter format_decimal_fixed(size_t precision);
Formatter format_decimal_scientific(size_t precision);
Formatter format_datetime(const std::string& fmt);
Formatter format_string();
Formatter format_custom(const std::vector<std::string>& values);

ReturnCode format_configure(
    const Expr* expr,
+1 −12
Original line number Diff line number Diff line
@@ -64,7 +64,6 @@ struct AxisDefinition {
  ScaleConfig scale;
  ScaleLayoutFn scale_layout;
  std::string title;
  std::vector<std::string> label_override;
  AxisLabelPosition tick_position;
  AxisLabelPosition label_position;
  Formatter label_formatter;
@@ -88,16 +87,7 @@ std::string axis_get_label(
    double offset) {
  const auto& domain = axis.scale;
  auto value = scale_untranslate(domain, offset);

  if (axis.label_override.size()) {
    if (idx < axis.label_override.size()) {
      return axis.label_override[idx];
    } else {
      return "";
    }
  }

  return axis.label_formatter(std::to_string(value));
  return axis.label_formatter(idx, std::to_string(value));
}

static Status renderAxisVertical(
@@ -529,7 +519,6 @@ ReturnCode build(const Environment& env, const Expr* expr, ElementRef* elem) {
      {"limit-max", bind(&expr_to_float64_opt, _1, &config->scale.max)},
      {"scale", bind(&scale_configure_kind, _1, &config->scale)},
      {"scale-padding", bind(&expr_to_float64, _1, &config->scale.padding)},
      {"labels", bind(&data_load_strings, _1, &config->label_override)},
      {"border", bind(&expr_to_stroke_style, _1, &config->border_style)},
      {"border-color", bind(&expr_to_color, _1, &config->border_style.color)},
      {"border-width", bind(&expr_to_measure, _1, &config->border_style.line_width)},
+13 −0
Original line number Diff line number Diff line
(set height 480px)
(set width 1200px)

(chart/linechart
    data-x (csv "tests/testdata/city_temperatures_london.csv" month)
    data-y (csv "tests/testdata/city_temperatures_london.csv" temperature)
    scale-x-padding 0.6
    ticks-x (linear 1 1)
    axis-x-format (custom "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
    limit-y (-10 32)
    ticks-y (linear 5 -10)
    color (#06c)
    axes (bottom left))
+48 −0
Original line number Diff line number Diff line
<svg xmlns="http://www.w3.org/2000/svg" width="1200.000000" height="480.000000" viewBox="0 0 1200 480">
  <rect width="1200.000000" height="480.000000" fill="#ffffff"/>
  <path stroke-width="2.000000" stroke="#0066cc" fill="none" d="M119.598 294.638 L211.05 291.701 L302.502 277.017 L393.955 249.607 L485.407 216.323 L576.859 184.018 L668.312 166.397 L759.764 170.313 L851.216 193.807 L942.668 231.986 L1034.12 268.207 L1125.57 285.828 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 430.711 L1180.44 430.711 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M119.598 430.711 L119.598 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M211.05 430.711 L211.05 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M302.502 430.711 L302.502 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M393.955 430.711 L393.955 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M485.407 430.711 L485.407 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M576.859 430.711 L576.859 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M668.312 430.711 L668.312 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M759.764 430.711 L759.764 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M851.216 430.711 L851.216 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M942.668 430.711 L942.668 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M1034.12 430.711 L1034.12 425.378 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M1125.57 430.711 L1125.57 425.378 "/>
  <text x="107.769644" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Jan</text>
  <text x="198.409443" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Feb</text>
  <text x="289.869555" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Mar</text>
  <text x="382.540605" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Apr</text>
  <text x="471.547592" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">May</text>
  <text x="565.031142" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Jun</text>
  <text x="658.928754" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Jul</text>
  <text x="746.716991" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Aug</text>
  <text x="838.169291" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Sep</text>
  <text x="931.254403" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Oct</text>
  <text x="1021.073890" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Nov</text>
  <text x="1112.526190" y="456.444444" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Dec</text>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 19.5556 L64.7264 430.711 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 430.711 L70.0597 430.711 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 381.764 L70.0597 381.764 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 332.817 L70.0597 332.817 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 283.87 L70.0597 283.87 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 234.923 L70.0597 234.923 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 185.976 L70.0597 185.976 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 137.029 L70.0597 137.029 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 88.0815 L70.0597 88.0815 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M64.7264 39.1344 L70.0597 39.1344 "/>
  <text x="19.555556" y="435.711111" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-10.0</text>
  <text x="27.711806" y="386.764021" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-5.0</text>
  <text x="32.602431" y="337.816931" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.0</text>
  <text x="32.602431" y="288.869841" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">5.0</text>
  <text x="24.446181" y="239.922751" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">10.0</text>
  <text x="24.446181" y="190.975661" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">15.0</text>
  <text x="24.446181" y="142.028571" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">20.0</text>
  <text x="24.446181" y="93.081481" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">25.0</text>
  <text x="24.446181" y="44.134392" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">30.0</text>
</svg>
 No newline at end of file
Loading