Commit 0ca5fc3d authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add grid-{x,y}-{,scale} and grid-stroke properties

parent b8bcdff6
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@ plot {
  y: y;
  group: series;

  grid: geom;

  layer {
    type: points;
  }
+16 −0
Original line number Diff line number Diff line
@@ -68,6 +68,22 @@
  <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="1.333333" stroke="#e5e5e5" fill="none" d="M59.8375 435.6 L1140.1625 435.6 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M59.8375 379.714283 L1140.1625 379.714283 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M59.8375 323.828566 L1140.1625 323.828566 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M59.8375 267.942844 L1140.1625 267.942844 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M59.8375 212.057133 L1140.1625 212.057133 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M59.8375 156.171422 L1140.1625 156.171422 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M59.8375 100.285688 L1140.1625 100.285688 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M59.8375 44.4 L1140.1625 44.4 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M59.8375 44.4 L59.8375 435.6 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M214.16965 44.4 L214.16965 435.6 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M368.5018 44.4 L368.5018 435.6 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M522.833965 44.4 L522.833965 435.6 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M677.166099 44.4 L677.166099 435.6 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M831.498233 44.4 L831.498233 435.6 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M985.830431 44.4 L985.830431 435.6 "/>
  <path stroke-width="1.333333" stroke="#e5e5e5" fill="none" d="M1140.1625 44.4 L1140.1625 435.6 "/>
  <path fill="#4572a7" d="M712.0325 120.147059 M704.0325 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="M766.04875 369.441176 M758.04875 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="M820.065 77.0 M812.065 77.0 a4.0 4.0 0 1 0 8.0 0 a4.0 4.0 0 1 0 -8.0 0 "/>
+7 −1
Original line number Diff line number Diff line
@@ -41,6 +41,12 @@
    <prop name="axis-left-scale" />
    <prop name="axis-left-format" />
    <prop name="axis-left-label-placement" />
    <prop name="grid" />
    <prop name="grid-x" />
    <prop name="grid-y" />
    <prop name="grid-x-scale" />
    <prop name="grid-y-scale" />
    <prop name="grid-stroke" />
    <prop name="margin" />
    <prop name="margin-top" />
    <prop name="margin-right" />
+2 −1
Original line number Diff line number Diff line
@@ -413,7 +413,8 @@ ReturnCode configure(
        config),
    bind(&grid_configure,
        ref(plist),
        doc,
        ref(doc),
        ref(scales),
        &config->grid),
    bind(&legend_configure_all,
        doc,
+91 −20
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
 */
#include "plot_gridlines.h"
#include "document.h"
#include "utils/algo.h"

using namespace std::placeholders;

@@ -38,26 +39,6 @@ namespace plot {

static const double kDefaultLineWidthPT = 1;

ReturnCode grid_configure(
    const PropertyList& plist,
    const Document& doc,
    GridlineDefinition* grid) {
  Measure line_width;
  Color line_color = Color::fromRGB(.9, .9, .9); // TODO

  static const ParserDefinitions pdefs = {
    {"grid-stroke", bind(&configure_measure_rel, _1, doc.dpi, doc.font_size, &line_width)},
  };

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

  grid->line_width = measure_or(line_width, from_pt(kDefaultLineWidthPT, doc.dpi));
  grid->line_color = line_color;
  return OK;
}

ReturnCode grid_draw(
    const GridlineDefinition& grid,
    const Rectangle& bbox,
@@ -89,6 +70,96 @@ ReturnCode grid_draw(
  return OK;
}

ReturnCode grid_place_lines_geom(
    const DomainConfig& domain,
    std::vector<double>* ticks) {
  uint32_t num_ticks = 8; // FIXME make configurable
  double min = domain.min.value_or(0.0f);
  double max = domain.max.value_or(0.0f);

  ticks->clear();

  for (size_t i = 0; i < num_ticks; ++i) {
    ticks->emplace_back((1.0f / (num_ticks - 1)) * i);
  }

  return OK;
}

using GridPlacement = std::function<
    ReturnCode (
        const DomainConfig& domain,
        std::vector<double>* ticks)>;

ReturnCode grid_configure_placement(
    const plist::Property& prop,
    GridPlacement* placement) {
  if (plist::is_value_literal(prop, "geom")) {
    *placement = bind(&grid_place_lines_geom, _1, _2);
    return OK;
  }

  return ReturnCode::errorf(
      "EARG",
      "invalid value for 'grid': $0; expected one of: geom(...)",
      prop.value);
}

ReturnCode grid_configure(
    const PropertyList& plist,
    const Document& doc,
    const DomainMap& scales,
    GridlineDefinition* grid) {
  Measure line_width;
  Color line_color = Color::fromRGB(.9, .9, .9); // TODO

  GridPlacement placement_horiz;
  GridPlacement placement_vert;

  std::string scale_horiz = SCALE_DEFAULT_X;
  std::string scale_vert = SCALE_DEFAULT_Y;
  static const ParserDefinitions pdefs = {
    {
      "grid",
      configure_multiprop({
        bind(&grid_configure_placement, _1, &placement_horiz),
        bind(&grid_configure_placement, _1, &placement_vert)
      })
    },
    {"grid-x", bind(&grid_configure_placement, _1, &placement_horiz)},
    {"grid-x-scale", bind(&configure_string, _1, &scale_horiz)},
    {"grid-y", bind(&grid_configure_placement, _1, &placement_horiz)},
    {"grid-y-scale", bind(&configure_string, _1, &scale_vert)},
    {"grid-stroke", bind(&configure_measure_rel, _1, doc.dpi, doc.font_size, &line_width)},
  };

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

  grid->line_width = measure_or(line_width, from_pt(kDefaultLineWidthPT, doc.dpi));
  grid->line_color = line_color;

  auto domain_horiz = find_ptr(scales, scale_horiz);
  auto domain_vert = find_ptr(scales, scale_vert);

  if (domain_horiz && placement_horiz) {
    if (auto rc = placement_horiz(*domain_horiz, &grid->ticks_horiz); !rc) {
      return rc;
    }
  }

  if (domain_vert && placement_vert) {
    if (auto rc = placement_vert(*domain_vert, &grid->ticks_vert); !rc) {
      return rc;
    }
  }

  return OK;
}



} // namespace plot
} // namespace plotfx
Loading