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

add 'labels' property to 'axis' element

parent 7b5daec7
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -35,10 +35,12 @@ axis {

axis {
  position: top;
  layout: discrete;
  layout: linear(1, align 1);
  labels: csv('tests/testdata/city_temperatures_pivot.csv', month_name);
}

axis {
  position: bottom;
  layout: linear(1, align 1);
  layout: discrete;
  labels: csv('tests/testdata/city_temperatures_pivot.csv', month_name);
}
+106 −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.0 480.0">
  <rect width="1200.000000" height="480.000000" fill="#ffffff"/>
  <path stroke-width="2.000000" stroke="#005fbf" fill="none" d="M106.013403 333.502476 L195.829145 330.240762 L285.644886 313.93219 L375.460628 283.489524 L465.27637 246.523429 L555.092112 210.644571 L644.907853 191.074286 L734.723595 195.423238 L824.539337 221.516952 L914.355079 263.919238 L1004.170821 304.147048 L1093.986562 323.717333 "/>
  <path stroke-width="2.000000" stroke="#bf005f" fill="none" d="M106.013403 365.998815 L195.829145 352.468741 L285.644886 304.267852 L375.460628 243.382519 L465.27637 179.114667 L555.092112 153.745778 L644.907853 129.222519 L734.723595 144.443852 L824.539337 196.872889 L914.355079 251.838815 L1004.170821 294.120296 L1093.986562 333.864889 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 49.733333 L52.123958 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 430.266667 L57.457292 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 375.904762 L57.457292 375.904762 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 321.542857 L57.457292 321.542857 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 267.180952 L57.457292 267.180952 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 212.819048 L57.457292 212.819048 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 158.457143 L57.457292 158.457143 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 104.095238 L57.457292 104.095238 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 49.733333 L57.457292 49.733333 "/>
  <text x="20.000000" y="435.266667" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.0</text>
  <text x="20.000000" y="380.904762" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.1</text>
  <text x="20.000000" y="326.542857" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.3</text>
  <text x="20.000000" y="272.180952" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.4</text>
  <text x="20.000000" y="217.819048" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.6</text>
  <text x="20.000000" y="163.457143" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.7</text>
  <text x="20.000000" y="109.095238" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.9</text>
  <text x="20.000000" y="54.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1.0</text>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 49.733333 L1147.876042 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 430.266667 L1142.542708 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 387.985185 L1142.542708 387.985185 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 345.703704 L1142.542708 345.703704 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 303.422222 L1142.542708 303.422222 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 261.140741 L1142.542708 261.140741 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 218.859259 L1142.542708 218.859259 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 176.577778 L1142.542708 176.577778 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 134.296296 L1142.542708 134.296296 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 92.014815 L1142.542708 92.014815 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1147.876042 49.733333 L1142.542708 49.733333 "/>
  <text x="1159.609375" y="435.266667" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.0</text>
  <text x="1159.609375" y="392.985185" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.1</text>
  <text x="1159.609375" y="350.703704" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.2</text>
  <text x="1159.609375" y="308.422222" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.3</text>
  <text x="1159.609375" y="266.140741" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.4</text>
  <text x="1159.609375" y="223.859259" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.6</text>
  <text x="1159.609375" y="181.577778" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.7</text>
  <text x="1159.609375" y="139.296296" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.8</text>
  <text x="1159.609375" y="97.014815" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.9</text>
  <text x="1159.609375" y="54.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1.0</text>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 49.733333 L1147.876042 49.733333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M106.013403 49.733333 L106.013403 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M195.829145 49.733333 L195.829145 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M285.644886 49.733333 L285.644886 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M375.460628 49.733333 L375.460628 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M465.27637 49.733333 L465.27637 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M555.092112 49.733333 L555.092112 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M644.907853 49.733333 L644.907853 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M734.723595 49.733333 L734.723595 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M824.539337 49.733333 L824.539337 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M914.355079 49.733333 L914.355079 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1004.170821 49.733333 L1004.170821 55.066667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1093.986562 49.733333 L1093.986562 55.066667 "/>
  <text x="94.185278" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Jan</text>
  <text x="183.188520" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Feb</text>
  <text x="273.012074" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Mar</text>
  <text x="364.046566" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Apr</text>
  <text x="451.416995" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">May</text>
  <text x="543.263987" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Jun</text>
  <text x="635.525041" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Jul</text>
  <text x="721.676720" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Aug</text>
  <text x="811.492462" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Sep</text>
  <text x="902.941016" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Oct</text>
  <text x="991.123946" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Nov</text>
  <text x="1080.939687" y="34.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Dec</text>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M52.123958 430.266667 L1147.876042 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M61.105532 430.266667 L61.105532 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M61.105532 430.266667 L61.105532 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M150.921274 430.266667 L150.921274 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M150.921274 430.266667 L150.921274 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M240.737016 430.266667 L240.737016 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M240.737016 430.266667 L240.737016 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M330.552757 430.266667 L330.552757 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M330.552757 430.266667 L330.552757 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M420.368499 430.266667 L420.368499 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M420.368499 430.266667 L420.368499 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M510.184241 430.266667 L510.184241 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M510.184241 430.266667 L510.184241 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M599.999983 430.266667 L599.999983 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M599.999983 430.266667 L599.999983 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M689.815724 430.266667 L689.815724 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M689.815724 430.266667 L689.815724 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M779.631466 430.266667 L779.631466 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M779.631466 430.266667 L779.631466 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M869.447208 430.266667 L869.447208 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M869.447208 430.266667 L869.447208 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M959.26295 430.266667 L959.26295 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M959.26295 430.266667 L959.26295 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1049.078691 430.266667 L1049.078691 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1049.078691 430.266667 L1049.078691 424.933333 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1138.894433 430.266667 L1138.894433 424.933333 "/>
  <text x="94.185278" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Jan</text>
  <text x="183.188520" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Feb</text>
  <text x="273.012074" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Mar</text>
  <text x="364.046566" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Apr</text>
  <text x="451.416995" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">May</text>
  <text x="543.263987" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Jun</text>
  <text x="635.525041" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Jul</text>
  <text x="721.676720" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Aug</text>
  <text x="811.492462" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Sep</text>
  <text x="902.941016" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Oct</text>
  <text x="991.123946" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Nov</text>
  <text x="1080.939687" y="456.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Dec</text>
</svg>
 No newline at end of file
+7 −7
Original line number Diff line number Diff line
@@ -127,7 +127,7 @@ std::function<double (double)> domain_translate_fn(const DomainConfig& domain) {
  return bind(&domain_translate, domain, std::placeholders::_1);
}

Value domain_untranslate_linear(const DomainConfig& domain, double vt) {
double  domain_untranslate_linear(const DomainConfig& domain, double vt) {
  auto min = domain_min(domain);
  auto max = domain_max(domain);

@@ -135,10 +135,10 @@ Value domain_untranslate_linear(const DomainConfig& domain, double vt) {
    vt = 1.0 - vt;
  }

  return value_from_float(min + (max - min) * vt);
  return min + (max - min) * vt;
}

Value domain_untranslate_log(const DomainConfig& domain, double vt) {
double domain_untranslate_log(const DomainConfig& domain, double vt) {
  auto min = domain_min(domain);
  auto max = domain_max(domain);
  auto log_base = domain.log_base.value_or(kDefaultLogBase);
@@ -149,10 +149,10 @@ Value domain_untranslate_log(const DomainConfig& domain, double vt) {
    vt = 1.0 - vt;
  }

  return value_from_float(min + pow(log_base, vt * range_log));
  return min + pow(log_base, vt * range_log);
}

Value domain_untranslate(
double domain_untranslate(
    const DomainConfig& domain,
    double value) {
  switch (domain.kind) {
@@ -165,10 +165,10 @@ Value domain_untranslate(
  return {};
}

Series domain_untranslate(
std::vector<double> domain_untranslate(
    const DomainConfig& domain,
    const std::vector<double>& values) {
  Series s;
  std::vector<double> s;
  for (const auto& v : values) {
    s.emplace_back(domain_untranslate(domain, v));
  }
+2 −2
Original line number Diff line number Diff line
@@ -78,11 +78,11 @@ double domain_translate(

std::function<double (double)> domain_translate_fn(const DomainConfig& domain);

Value domain_untranslate(
double domain_untranslate(
    const DomainConfig& domain,
    double data);

Series domain_untranslate(
std::vector<double> domain_untranslate(
    const DomainConfig& domain,
    const std::vector<double>& data);

+34 −13
Original line number Diff line number Diff line
@@ -403,6 +403,22 @@ ReturnCode axis_layout(
  return OK;
}

std::string axis_get_label(
    const DomainConfig& domain,
    const AxisDefinition& axis,
    size_t idx,
    double value) {
  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));
}

ReturnCode axis_place_labels_linear(
    const DomainConfig& domain,
    AxisDefinition* axis,
@@ -414,10 +430,11 @@ ReturnCode axis_place_labels_linear(
  auto begin = std::max(align.value_or(domain_min(domain)), domain_min(domain));
  auto end = domain_max(domain);

  size_t label_idx = 0;
  for (auto v = begin; v <= end; v += step) {
    auto vp = domain_translate(domain, v);
    axis->ticks.emplace_back(vp);
    axis->labels.emplace_back(vp, axis->label_formatter(std::to_string(v)));
    axis->labels.emplace_back(vp, axis_get_label(domain, *axis, label_idx++, vp));
  }

  return OK;
@@ -435,10 +452,11 @@ ReturnCode axis_place_labels_subdivide(
  }

  auto tick_values = domain_untranslate(domain, axis->ticks);
  size_t label_idx = 0;
  for (size_t i = 0; i < divisions; ++i) {
    axis->labels.emplace_back(
        axis->ticks[i],
        axis->label_formatter(tick_values[i]));
        axis_get_label(domain, *axis, label_idx++, tick_values[i]));
  }

  return OK;
@@ -453,20 +471,23 @@ ReturnCode axis_place_labels_discrete(
  axis->labels.clear();
  axis->ticks.clear();

  size_t label_idx = 0;
  for (size_t i = 0; i <= range; i += step) {
    auto o = domain_translate(domain, i);
    auto o1 = domain_translate(domain, i - step * 0.5);
    auto o2 = domain_translate(domain, i + step * 0.5);
    auto v = uint32_t(domain_min(domain)) + i;
    auto o = domain_translate(domain, i * step);
    auto o1 = domain_translate(domain, i * step - step * 0.5);
    auto o2 = domain_translate(domain, i * step + step * 0.5);
    auto v = uint32_t(domain_min(domain)) + i * step;
    auto vn = uint32_t(domain_min(domain)) + (i + 1) * step;

    auto label = axis->label_formatter(std::to_string(i));
    if (o1 >= 0 && o2 <= 1) {
      auto label = axis_get_label(domain, *axis, label_idx, v);
      if (step > 1) {
        label += " - ";
      label += axis->label_formatter(std::to_string(i + step));
        label += axis_get_label(domain, *axis, label_idx, vn);
      }

    if (o1 >= 0 && o2 <= 1) {
      axis->labels.emplace_back(o, label);
      ++label_idx;
    }

    if (o1 >= 0 && o1 <= 1) {
@@ -699,7 +720,7 @@ ReturnCode configure(
      {"position", bind(&parseAxisPositionProp, _1, &config->position)},
      {"layout", bind(&axis_configure_label_placement, _1, &config->label_placement)},
      {"format", bind(&confgure_format, _1, &config->label_formatter)},

      {"labels", bind(&configure_strings, _1, &config->label_override)},
    };

    if (auto rc = parseAll(plist, pdefs); !rc) {
Loading