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

implement automatic scale fitting for custom scales

parent 3988c565
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ DomainConfig::DomainConfig() :
    kind(DomainKind::AUTO),
    min_auto_snap_zero(false),
    inverted(false),
    padding(0.0f) {}
    padding(0.1f) {}

void domain_fit_continuous(const Series& data_raw, DomainConfig* domain) {
  auto data = series_to_float(data_raw);
@@ -101,7 +101,7 @@ double domain_min(const DomainConfig& domain) {
  auto min = domain.min.value_or(domain.min_auto.value_or(0));
  auto max = domain.max.value_or(domain.max_auto.value_or(0));

  auto min_auto = 0;
  double min_auto = 0;
  if (!domain.min_auto_snap_zero || min < 0) {
    min_auto = min - (max - min) * domain.padding;
  }
@@ -112,15 +112,15 @@ double domain_min(const DomainConfig& domain) {
double domain_max(const DomainConfig& domain) {
  auto min = domain.min.value_or(domain.min_auto.value_or(0));
  auto max = domain.max.value_or(domain.max_auto.value_or(0));
  auto max_auto = max + (max - min) * domain.padding;
  double max_auto = max + (max - min) * domain.padding;
  return domain.max.value_or(max_auto);
}

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

  auto vf = value_to_float(v);
  auto vt = (vf - min) / (max - min);
@@ -185,6 +185,8 @@ double domain_translate(
      return domain_translate_log(domain, value);
    case DomainKind::CATEGORICAL:
      return domain_translate_categorical(domain, value);
    default:
      return std::numeric_limits<double>::quiet_NaN();
  }

  return 0.0f;
+13 −1
Original line number Diff line number Diff line
@@ -166,12 +166,12 @@ ReturnCode configure_scales(
    DomainMap* scales) {
  {
    DomainConfig d;
    d.padding = 0;
    scales->emplace(SCALE_DEFAULT_X, d);
  }

  {
    DomainConfig d;
    d.padding = 0.1f;
    d.min_auto_snap_zero = true;
    scales->emplace(SCALE_DEFAULT_Y, d);
  }
@@ -201,6 +201,14 @@ ReturnCode configure_scales(
      return rc;
    }

    if (!scales->count(scale_x)) {
      scales->emplace(scale_x, DomainConfig{});
    }

    if (!scales->count(scale_y)) {
      scales->emplace(scale_y, DomainConfig{});
    }

    if (data_x) {
      auto domain_x = find_ptr(scales, scale_x);
      if (!domain_x) {
@@ -316,6 +324,7 @@ ReturnCode configure_style(
    },
    {"axis-y-padding", bind(&configure_float, _1, &domain_y->padding)},
    {"axis-top", bind(&parseAxisModeProp, _1, &config->axis_top.mode)},
    {"axis-top-scale", bind(&configure_string, _1, &config->axis_top.scale)},
    {"axis-top-format", bind(&confgure_format, _1, &config->axis_top.label_formatter)},
    {
      "axis-top-label-placement",
@@ -325,6 +334,7 @@ ReturnCode configure_style(
          &config->axis_top.label_placement),
    },
    {"axis-right", bind(&parseAxisModeProp, _1, &config->axis_right.mode)},
    {"axis-right-scale", bind(&configure_string, _1, &config->axis_right.scale)},
    {"axis-right-format", bind(&confgure_format, _1, &config->axis_right.label_formatter)},
    {
      "axis-right-label-placement",
@@ -334,6 +344,7 @@ ReturnCode configure_style(
          &config->axis_right.label_placement),
    },
    {"axis-bottom", bind(&parseAxisModeProp, _1, &config->axis_bottom.mode)},
    {"axis-bottom-scale", bind(&configure_string, _1, &config->axis_bottom.scale)},
    {"axis-bottom-format", bind(&confgure_format, _1, &config->axis_bottom.label_formatter)},
    {
      "axis-bottom-label-placement",
@@ -343,6 +354,7 @@ ReturnCode configure_style(
          &config->axis_bottom.label_placement),
    },
    {"axis-left", bind(&parseAxisModeProp, _1, &config->axis_left.mode)},
    {"axis-left-scale", bind(&configure_string, _1, &config->axis_left.scale)},
    {"axis-left-format", bind(&confgure_format, _1, &config->axis_left.label_formatter)},
    {
      "axis-left-label-placement",
+28 −28
Original line number Diff line number Diff line
@@ -26,13 +26,13 @@
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1140.1625 156.171422 L1134.829167 156.171422 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1140.1625 100.285688 L1134.829167 100.285688 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1140.1625 44.4 L1134.829167 44.4 "/>
  <text x="1151.895833" y="440.600000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-31.0</text>
  <text x="1151.895833" y="384.714283" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-19.5</text>
  <text x="1151.895833" y="328.828566" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-7.9</text>
  <text x="1151.895833" y="272.942844" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">3.6</text>
  <text x="1151.895833" y="217.057133" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">15.2</text>
  <text x="1151.895833" y="161.171422" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">26.7</text>
  <text x="1151.895833" y="105.285688" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">38.3</text>
  <text x="1151.895833" y="440.600000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-31.8</text>
  <text x="1151.895833" y="384.714283" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-20.1</text>
  <text x="1151.895833" y="328.828566" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-8.5</text>
  <text x="1151.895833" y="272.942844" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">3.2</text>
  <text x="1151.895833" y="217.057133" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">14.8</text>
  <text x="1151.895833" y="161.171422" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">26.5</text>
  <text x="1151.895833" y="105.285688" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">38.1</text>
  <text x="1151.895833" y="49.400000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">49.8</text>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M59.8375 435.6 L1140.1625 435.6 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M59.8375 435.6 L59.8375 430.266667 "/>
@@ -60,28 +60,28 @@
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M59.8375 156.171422 L65.170833 156.171422 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M59.8375 100.285688 L65.170833 100.285688 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M59.8375 44.4 L65.170833 44.4 "/>
  <text x="14.666667" y="440.600000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-31.0</text>
  <text x="14.666667" y="384.714283" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-19.5</text>
  <text x="22.822917" y="328.828566" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-7.9</text>
  <text x="27.713542" y="272.942844" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">3.6</text>
  <text x="19.557292" y="217.057133" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">15.2</text>
  <text x="19.557292" y="161.171422" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">26.7</text>
  <text x="19.557292" y="105.285688" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">38.3</text>
  <text x="14.666667" y="440.600000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-31.8</text>
  <text x="14.666667" y="384.714283" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-20.1</text>
  <text x="22.822917" y="328.828566" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">-8.5</text>
  <text x="27.713542" y="272.942844" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">3.2</text>
  <text x="19.557292" y="217.057133" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">14.8</text>
  <text x="19.557292" y="161.171422" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">26.5</text>
  <text x="19.557292" y="105.285688" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">38.1</text>
  <text x="19.557292" y="49.400000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">49.8</text>
  <path stroke-width="2.666667" stroke="#4572a7" fill="none" d="M131.859167 120.89703 L491.9675 372.659406 L852.075833 77.322773 L924.0975 377.50099 L1032.13 164.471287 L1140.1625 125.738614 "/>
  <path stroke-width="2.666667" stroke="#aa4643" fill="none" d="M59.8375 120.89703 L131.859167 198.362377 L491.9675 304.877228 L744.043333 193.520792 L780.054167 406.550495 L852.075833 396.867327 "/>
  <path fill="#4572a7" d="M135.859167 120.89703 M127.859167 120.89703 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M495.9675 372.659406 M487.9675 372.659406 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M856.075833 77.322773 M848.075833 77.322773 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M928.0975 377.50099 M920.0975 377.50099 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M1036.13 164.471287 M1028.13 164.471287 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M1144.1625 125.738614 M1136.1625 125.738614 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M63.8375 120.89703 M55.8375 120.89703 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M135.859167 198.362377 M127.859167 198.362377 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M495.9675 304.877228 M487.9675 304.877228 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M748.043333 193.520792 M740.043333 193.520792 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M784.054167 406.550495 M776.054167 406.550495 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M856.075833 396.867327 M848.075833 396.867327 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path stroke-width="2.666667" stroke="#4572a7" fill="none" d="M131.859167 120.147059 L491.9675 369.441176 L852.075833 77.0 L924.0975 374.235294 L1032.13 163.294118 L1140.1625 124.941177 "/>
  <path stroke-width="2.666667" stroke="#aa4643" fill="none" d="M59.8375 120.147059 L131.859167 196.852941 L491.9675 302.323529 L744.043333 192.058824 L780.054167 403.0 L852.075833 393.411764 "/>
  <path fill="#4572a7" d="M135.859167 120.147059 M127.859167 120.147059 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M495.9675 369.441176 M487.9675 369.441176 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M856.075833 77.0 M848.075833 77.0 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M928.0975 374.235294 M920.0975 374.235294 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M1036.13 163.294118 M1028.13 163.294118 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M1144.1625 124.941177 M1136.1625 124.941177 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M63.8375 120.147059 M55.8375 120.147059 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M135.859167 196.852941 M127.859167 196.852941 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M495.9675 302.323529 M487.9675 302.323529 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M748.043333 192.058824 M740.043333 192.058824 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M784.054167 403.0 M776.054167 403.0 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#aa4643" d="M856.075833 393.411764 M848.075833 393.411764 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
  <path fill="#4572a7" d="M102.5375 75.2 M92.5375 75.2 a5.0 5.0 0 1 0 10.0 0 a5.0 5.0 0 1 0 -10.0 0 "/>
  <text x="109.037500" y="80.200000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">series1</text>
  <path fill="#aa4643" d="M199.05 75.2 M189.05 75.2 a5.0 5.0 0 1 0 10.0 0 a5.0 5.0 0 1 0 -10.0 0 "/>
−50.4 KiB
Loading image diff...
+24 −0
Original line number Diff line number Diff line
width: 1200px;
height: 480px;

plot {
  data: csv('tests/testdata/city_temperatures_pivot.csv');
  x: month;

  axis-left-scale: y1;
  axis-right-scale: y2;

  layer {
    type: lines;
    y: temp_london;
    color: #0c6;
    y-scale: y1;
  }

  layer {
    type: lines;
    y: temp_beijing;
    color: #06c;
    y-scale: y2;
  }
}
Loading