Commit 7fba1e08 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved automatic formatter config

parent 2627db1d
Loading
Loading
Loading
Loading
+4 −16
Original line number Diff line number Diff line
@@ -36,14 +36,11 @@
namespace plotfx {

Formatter format_decimal_scientific(size_t precision) {
  Formatter f;
  f.format_value = [precision] (const Value& v) -> std::string {
  return [precision] (const Value& v) -> std::string {
    std::stringstream s;
    s << std::scientific << std::setprecision(precision) << value_to_float(v);
    return s.str();
  };

  return f;
}

ReturnCode confgure_format_decimal_scientific(
@@ -73,14 +70,11 @@ ReturnCode confgure_format_decimal_scientific(
}

Formatter format_decimal_fixed(size_t precision) {
  Formatter f;
  f.format_value = [precision] (const Value& v) -> std::string {
  return [precision] (const Value& v) -> std::string {
    std::stringstream s;
    s << std::fixed << std::setprecision(precision) << value_to_float(v);
    return s.str();
  };

  return f;
}

ReturnCode confgure_format_decimal_fixed(
@@ -110,13 +104,10 @@ ReturnCode confgure_format_decimal_fixed(
}

Formatter format_datetime(const std::string& fmt) {
  Formatter f;
  f.format_value = [fmt] (const Value& v) -> std::string {
  return [fmt] (const Value& v) -> std::string {
    UnixTime v_t(value_to_float(v) * 1000000);
    return v_t.toString(fmt.c_str());
  };

  return f;
}

ReturnCode confgure_format_datetime(
@@ -142,12 +133,9 @@ ReturnCode confgure_format_datetime(
}

Formatter format_string() {
  Formatter f;
  f.format_value = [] (const Value& v) -> std::string {
  return [] (const Value& v) -> std::string {
    return v;
  };

  return f;
}

ReturnCode confgure_format_string(
+1 −3
Original line number Diff line number Diff line
@@ -36,9 +36,7 @@

namespace plotfx {

struct Formatter {
  std::function<std::string (const Value&)> format_value;
};
using Formatter = std::function<std::string (const Value&)>;

Formatter format_decimal_fixed(size_t precision);
Formatter format_decimal_scientific(size_t precision);
+12 −4
Original line number Diff line number Diff line
@@ -48,8 +48,7 @@ static const double kDefaultTickLengthPT = 4;
AxisDefinition::AxisDefinition() :
    mode(AxisMode::AUTO),
    label_position(AxisLabelPosition::OUTSIDE),
    tick_position(AxisLabelPosition::INSIDE),
    label_formatter(format_decimal_fixed(1)) {}
    tick_position(AxisLabelPosition::INSIDE) {}

ReturnCode parseAxisMode(
    const std::string& str,
@@ -487,7 +486,7 @@ ReturnCode axis_place_labels_geom(
  for (size_t i = 0; i < num_ticks; ++i) {
    axis->labels.emplace_back(
        axis->ticks[i],
        axis->label_formatter.format_value(tick_values[i]));
        axis->label_formatter(tick_values[i]));
  }

  return OK;
@@ -519,7 +518,7 @@ ReturnCode axis_place_labels_categorical(
  for (size_t i = 0; i < label_positions.size(); ++i) {
    axis->labels.emplace_back(
        label_positions[i],
        axis->label_formatter.format_value(label_values[i]));
        axis->label_formatter(label_values[i]));
  }

  return OK;
@@ -579,6 +578,15 @@ ReturnCode axis_resolve(
    return ReturnCode::errorf("EARG", "scale not found: $0", axis->scale);
  }

  if (!axis->label_formatter) {
    // TODO: improved automatic formatter config
    if (domain->kind == DomainKind::CATEGORICAL) {
      axis->label_formatter = format_string();
    } else {
      axis->label_formatter = format_decimal_fixed(1);
    }
  }

  switch (axis->tick_position) {
    case AxisLabelPosition::OUTSIDE:
      switch (pos) {
+0 −1
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ plot {
  y: var(temperature);
  group: var(city);

  axis-x-format: string;
  axis-y-min: -10;
  axis-y-max: 32;
  axis-top: off;