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

add legend-margin-{top,right,bottom,left} and legend-item-margin-{top,right,bottom,left} properties

parent fdf26664
Loading
Loading
Loading
Loading
+122 −22
Original line number Diff line number Diff line
@@ -51,7 +51,10 @@ void LegendConfig::addEntry(
    const std::string& name,
    const Color& color,
    const std::string& shape /* = "circle" */) {
  entries.emplace_back(name, color, shape);
  LegendItem item;
  item.title = name;
  item.color = color;
  entries.emplace_back(std::move(item));
}

ReturnCode legend_draw_inside(
@@ -60,34 +63,48 @@ ReturnCode legend_draw_inside(
    Layer* layer) {
  auto font_size = from_em(kDefaultLabelFontSizeEM, layer->font_size);

  double padding_horiz = measure_or(
      legend.padding_horiz,
  double padding_left = measure_or(
      legend.margins[3],
      from_em(kDefaultPaddingHorizEM, font_size));

  double padding_vert = measure_or(
      legend.padding_vert,
  double padding_top = measure_or(
      legend.margins[0],
      from_em(kDefaultPaddingVertEM, font_size));

  double padding_item_horiz = measure_or(
      legend.padding_horiz,
      from_em(kDefaultItemPaddingHorizEM, font_size));

  double padding_item_vert = measure_or(
      legend.padding_vert,
      from_em(kDefaultItemPaddingVertEM, font_size));
  double padding_bottom = measure_or(
      legend.margins[2],
      from_em(kDefaultPaddingVertEM, font_size));

  double point_size = 5;
  double line_height = 14;
  double padding_item_right = measure_or(
      legend.item_margins[1],
      from_em(kDefaultItemPaddingHorizEM, font_size));

  double sx = bbox.x + padding_horiz;
  double sy = bbox.y + padding_vert + line_height / 2;
  double point_size = 5; // FIXME

  double sx = bbox.x + padding_left;
  double sy = 0;
  double line_height;
  switch (legend.position_vert) {
    case VAlign::TOP:
      line_height = font_size; // FIXME
      sy = bbox.y + padding_top + line_height / 2;
      break;
    case VAlign::BOTTOM:
      line_height = -font_size; // FIXME
      sy = bbox.y + bbox.h - padding_bottom + line_height / 2;
      break;
    case VAlign::CENTER:
      line_height = font_size; // FIXME
      sy = bbox.y + bbox.h / 2;
      break;
  }

  for (const auto& e : legend.entries) {
    const auto& label_text = std::get<0>(e);
    const auto& label_text = e.title;

    {
      FillStyle style;
      style.color = std::get<1>(e);
      style.color = e.color;
      Path path;
      path.moveTo(sx + point_size, sy);
      path.arcTo(sx, sy, point_size, 0, M_PI * 2);
@@ -120,7 +137,7 @@ ReturnCode legend_draw_inside(
        return rc;
      }

      sx += label_bbox.w + padding_item_horiz;
      sx += label_bbox.w + padding_item_right;
    }
  }

@@ -215,9 +232,92 @@ ReturnCode legend_configure(
          &config->position_horiz,
          &config->position_vert)
    },
    {"legend-title", std::bind(&configure_string, std::placeholders::_1, &config->title)},
    {"legend-text-color", std::bind(&configure_color, std::placeholders::_1, &config->text_color)},
    {"legend-border-color", std::bind(&configure_color, std::placeholders::_1, &config->border_color)},
    {
      "legend-title",
      std::bind(
          &configure_string,
          std::placeholders::_1,
          &config->title)
      },
    {
      "legend-text-color",
      std::bind(
          &configure_color,
          std::placeholders::_1,
          &config->text_color)
    },
    {
      "legend-border-color",
      std::bind(
          &configure_color,
          std::placeholders::_1,
          &config->border_color)
    },
    {
      "legend-item-margin",
      configure_multiprop({
          std::bind(
              &configure_measure_rel,
              std::placeholders::_1,
              doc.dpi,
              doc.font_size,
              &config->item_margins[0]),
          std::bind(
              &configure_measure_rel,
              std::placeholders::_1,
              doc.dpi,
              doc.font_size,
              &config->item_margins[1]),
          std::bind(
              &configure_measure_rel,
              std::placeholders::_1,
              doc.dpi,
              doc.font_size,
              &config->item_margins[2]),
          std::bind(
              &configure_measure_rel,
              std::placeholders::_1,
              doc.dpi,
              doc.font_size,
              &config->item_margins[3])
      })
    },
    {
      "legend-item-margin-top",
      std::bind(
          &configure_measure_rel,
          std::placeholders::_1,
          doc.dpi,
          doc.font_size,
          &config->item_margins[0])
    },
    {
      "legend-item-margin-right",
      std::bind(
          &configure_measure_rel,
          std::placeholders::_1,
          doc.dpi,
          doc.font_size,
          &config->item_margins[1])
    },
    {
      "legend-item-margin-bottom",
      std::bind(
          &configure_measure_rel,
          std::placeholders::_1,
          doc.dpi,
          doc.font_size,
          &config->item_margins[2])
    },
    {
      "legend-item-margin-left",
      std::bind(
          &configure_measure_rel,
          std::placeholders::_1,
          doc.dpi,
          doc.font_size,
          &config->item_margins[3])
    },
  };

  if (auto rc = parseAll(plist, pdefs); !rc.isSuccess()) {
+8 −1
Original line number Diff line number Diff line
@@ -45,6 +45,11 @@ enum class LegendPlacement {
  OUTSIDE
};

struct LegendItem {
  std::string title;
  Color color;
};

struct LegendConfig {

  LegendConfig();
@@ -61,11 +66,13 @@ struct LegendConfig {
  Measure padding_vert;
  Measure padding_item_horiz;
  Measure padding_item_vert;
  Measure margins[4];
  Measure item_margins[4];
  LegendPlacement placement;
  HAlign position_horiz;
  VAlign position_vert;
  std::string title;
  std::vector<std::tuple<std::string, Color, std::string>> entries;
  std::vector<LegendItem> entries;
};

ReturnCode legend_configure(
+22 −0
Original line number Diff line number Diff line
width: 1200px;
height: 480px;

plot {
  axis-top: off;
  axis-right: off;

  legend: inside top left;
  legend-title: "The quick brown fox";

  series {
    title: "Series 1";
    xs: csv('tests/testdata/measurement.csv', 0);
    ys: csv('tests/testdata/measurement.csv', 2);
  }

  series {
    title: "Series 2";
    xs: csv('tests/testdata/measurement.csv', 0);
    ys: csv('tests/testdata/measurement.csv', 4);
  }
}
+42 −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" viewport-fill="#ffffff">
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 435.6 L1185.333333 435.6 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 435.6 L63.103125 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M223.421733 435.6 L223.421733 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M383.740342 435.6 L383.740342 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M544.058967 435.6 L544.058967 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M704.377558 435.6 L704.377558 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M864.69615 435.6 L864.69615 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1025.014809 435.6 L1025.014809 430.266667 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M1185.333333 435.6 L1185.333333 430.266667 "/>
  <text x="16.204688" y="461.333333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1404278100.0</text>
  <text x="176.523296" y="461.333333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1404282685.7</text>
  <text x="336.841904" y="461.333333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1404287271.4</text>
  <text x="497.160529" y="461.333333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1404291857.1</text>
  <text x="657.479121" y="461.333333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1404296442.9</text>
  <text x="817.797713" y="461.333333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1404301028.6</text>
  <text x="978.116371" y="461.333333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1404305614.3</text>
  <text x="1138.434896" y="461.333333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">1404310200.0</text>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 14.666667 L63.103125 435.6 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 435.6 L68.436458 435.6 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 375.466664 L68.436458 375.466664 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 315.333328 L68.436458 315.333328 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 255.199986 L68.436458 255.199986 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 195.066656 L68.436458 195.066656 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 134.933326 L68.436458 134.933326 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 74.799971 L68.436458 74.799971 "/>
  <path stroke-width="1.333333" stroke="#a8a8a8" fill="none" d="M63.103125 14.666667 L68.436458 14.666667 "/>
  <text x="30.979167" y="440.600000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">0.0</text>
  <text x="22.822917" y="380.466664" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">18.2</text>
  <text x="22.822917" y="320.333328" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">36.4</text>
  <text x="22.822917" y="260.199986" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">54.5</text>
  <text x="22.822917" y="200.066656" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">72.7</text>
  <text x="22.822917" y="139.933326" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">90.9</text>
  <text x="14.666667" y="79.799971" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">109.1</text>
  <text x="14.666667" y="19.666667" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">127.2</text>
  <path stroke-width="2.666667" stroke="#4572a7" fill="none" d="M63.103125 331.416924 L73.591258 340.095624 L84.079391 409.717774 L94.567523 406.566231 L105.055656 391.897276 L115.543789 425.324225 L126.031922 427.758092 L136.520055 406.631729 L147.008187 386.781484 L157.49632 427.025014 L167.984453 402.623555 L178.472586 411.877276 L188.960718 358.818311 L199.448851 375.729626 L209.936984 398.162594 L220.425117 372.213445 L230.91325 402.441857 L241.401382 415.319268 L251.889515 425.914067 L262.377648 426.335442 L272.865781 426.28252 L283.353914 377.842126 L293.842046 193.011131 L304.330179 52.220433 L314.818312 58.853531 L325.306445 70.941454 L335.794577 235.307769 L346.28271 279.197224 L356.770843 361.809201 L367.258976 404.651417 L377.747109 419.744172 L388.235241 415.779453 L398.723374 420.60313 L409.211507 415.808412 L419.69964 368.205144 L430.187773 364.039831 L440.675905 362.35164 L451.164038 343.785146 L461.652171 352.319822 L472.140304 366.963614 L482.628437 360.023626 L493.116569 336.314576 L503.604702 299.634185 L514.092835 303.788523 L524.580968 279.669217 L535.0691 317.712533 L545.557233 334.684864 L556.045366 373.976147 L566.533499 353.543796 L577.021632 285.854336 L587.509764 272.856534 L597.997897 305.438215 L608.48603 312.189039 L618.974163 302.872573 L629.462296 285.590355 L639.950428 354.175616 L650.438561 376.443838 L660.926694 315.153772 L671.414827 355.032316 L681.90296 399.533154 L692.391092 394.88571 L702.879225 348.767209 L713.367358 383.037118 L723.855491 386.694119 L734.343623 393.533438 L744.831756 386.289863 L755.319889 393.136253 L765.808022 367.511881 L776.296155 389.957838 L786.784287 375.944331 L797.27242 361.899253 L807.760553 374.336911 L818.248686 399.256048 L828.736819 417.18185 L839.224951 415.457492 L849.713084 413.281595 L860.201217 413.997525 L870.68935 414.587577 L881.177482 413.413886 L891.665615 414.36991 L902.153748 416.389096 L912.641881 414.902146 L923.130014 415.290759 L933.618146 414.085289 L944.106279 409.805883 L954.594412 408.29244 L965.082545 410.065487 L975.570678 407.986177 L986.05881 412.513433 L996.546943 408.331446 L1007.035076 402.290388 L1017.523209 415.052103 L1028.011342 411.782285 L1038.499474 374.672192 L1048.987607 409.821141 L1059.47574 409.788525 L1069.963873 410.65052 L1080.452005 411.872041 L1090.940138 410.152573 L1101.428271 410.783021 L1111.916404 411.329202 L1122.404537 412.80245 L1132.892669 410.839789 L1143.380802 412.872407 L1153.868935 406.582089 L1164.357068 402.517882 L1174.845201 404.856023 L1185.333333 410.601383 "/>
  <path stroke-width="2.666667" stroke="#aa4643" fill="none" d="M63.103125 435.363504 L73.591258 435.387755 L84.079391 435.454555 L94.567523 435.447577 L105.055656 435.45355 L115.543789 435.482741 L126.031922 435.455659 L136.520055 435.472991 L147.008187 435.41778 L157.49632 435.408168 L167.984453 435.265267 L178.472586 435.33686 L188.960718 435.35631 L199.448851 435.399822 L209.936984 435.412978 L220.425117 435.404416 L230.91325 435.395407 L241.401382 435.393234 L251.889515 435.408645 L262.377648 435.406204 L272.865781 435.423542 L283.353914 435.291119 L293.842046 435.326766 L304.330179 435.252514 L314.818312 435.306629 L325.306445 435.304907 L335.794577 435.292699 L346.28271 435.322821 L356.770843 435.354922 L367.258976 435.385734 L377.747109 435.296378 L388.235241 435.404485 L398.723374 435.390226 L409.211507 435.242793 L419.69964 435.25443 L430.187773 435.248864 L440.675905 435.24579 L451.164038 435.21531 L461.652171 435.237995 L472.140304 435.205402 L482.628437 435.262091 L493.116569 435.264332 L503.604702 435.302462 L514.092835 435.240242 L524.580968 435.257878 L535.0691 435.178666 L545.557233 435.256291 L556.045366 435.279667 L566.533499 435.367667 L577.021632 435.353828 L587.509764 435.353845 L597.997897 435.351058 L608.48603 435.354501 L618.974163 435.327711 L629.462296 435.36432 L639.950428 435.359473 L650.438561 435.342919 L660.926694 435.261112 L671.414827 435.269771 L681.90296 435.275666 L692.391092 435.303963 L702.879225 435.279838 L713.367358 435.380802 L723.855491 435.407812 L734.343623 435.378424 L744.831756 435.39881 L755.319889 435.411188 L765.808022 435.416365 L776.296155 435.381302 L786.784287 435.370318 L797.27242 435.390744 L807.760553 435.390935 L818.248686 435.406128 L828.736819 435.417064 L839.224951 435.437275 L849.713084 435.443523 L860.201217 435.449992 L870.68935 435.463452 L881.177482 435.463144 L891.665615 435.470197 L902.153748 435.453866 L912.641881 435.466552 L923.130014 435.458289 L933.618146 435.465898 L944.106279 435.466008 L954.594412 435.448924 L965.082545 435.448912 L975.570678 435.439421 L986.05881 435.450234 L996.546943 435.437987 L1007.035076 435.434549 L1017.523209 435.441591 L1028.011342 435.447663 L1038.499474 435.442375 L1048.987607 435.456708 L1059.47574 435.465646 L1069.963873 435.468117 L1080.452005 435.475826 L1090.940138 435.482403 L1101.428271 435.480729 L1111.916404 435.486016 L1122.404537 435.486884 L1132.892669 435.476973 L1143.380802 435.47419 L1153.868935 435.487065 L1164.357068 435.484934 L1174.845201 435.478332 L1185.333333 435.486248 "/>
  <path fill="#4572a7" d="M103.303125 39.6 M93.303125 39.6 a5.0 5.0 0 1 0 10.0 0 a5.0 5.0 0 1 0 -10.0 0 "/>
  <text x="110.303125" y="44.600000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Series 1</text>
  <path fill="#aa4643" d="M204.33125 39.6 M194.33125 39.6 a5.0 5.0 0 1 0 10.0 0 a5.0 5.0 0 1 0 -10.0 0 "/>
  <text x="211.331250" y="44.600000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">Series 2</text>
</svg>
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -68,6 +68,6 @@
  <text x="15.760417" y="105.285688" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">11.5</text>
  <text x="14.666667" y="49.400000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">13.4</text>
  <path stroke-width="2.666667" stroke="#4572a7" fill="none" d="M54.946875 399.768888 L176.069792 309.754144 L297.192708 341.506918 L418.315625 248.287766 L539.438542 335.098101 L660.561458 76.706263 L781.684375 198.473781 L902.807292 284.118877 L1023.930208 333.641552 L1145.053125 371.511833 "/>
  <path fill="#4572a7" d="M95.146875 69.0 M85.146875 69.0 a5.0 5.0 0 1 0 10.0 0 a5.0 5.0 0 1 0 -10.0 0 "/>
  <text x="102.146875" y="69.000000" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif"></text>
  <path fill="#4572a7" d="M95.146875 69.333333 M85.146875 69.333333 a5.0 5.0 0 1 0 10.0 0 a5.0 5.0 0 1 0 -10.0 0 "/>
  <text x="102.146875" y="69.333333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif"></text>
</svg>
 No newline at end of file
Loading