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

merge the 'plot/axis-{top,right,bottom,left}' elements into a single 'plot/axis' element

parent f39078d5
Loading
Loading
Loading
Loading
+6 −9
Original line number Diff line number Diff line
@@ -50,22 +50,19 @@ struct fviz_s {
fviz_t* fviz_init() {
  auto ctx = std::make_unique<fviz_t>();
  auto elems = &ctx->env.element_map;
  element_bind(elems, "text", bind(elements::text::build, _1, _2, _3));
  element_bind(elems, "plot/axis-top", bind(elements::plot::axis::build, _1, _2, _3));
  element_bind(elems, "plot/axis-right", bind(elements::plot::axis::build, _1, _2, _3));
  element_bind(elems, "plot/axis-bottom", bind(elements::plot::axis::build, _1, _2, _3));
  element_bind(elems, "plot/axis-left", bind(elements::plot::axis::build, _1, _2, _3));
  element_bind(elems, "chart/legend", bind(elements::chart::legend::build, _1, _2, _3));
  element_bind(elems, "chart/linechart", bind(elements::chart::linechart::build, _1, _2, _3));
  element_bind(elems, "chart/scatterplot", bind(elements::chart::scatterplot::build, _1, _2, _3));
  element_bind(elems, "layout/box", bind(elements::layout::box::build, _1, _2, _3));
  element_bind(elems, "plot", bind(elements::plot::build, _1, _2, _3));
  element_bind(elems, "plot/areas", bind(elements::plot::areas::build, _1, _2, _3));
  element_bind(elems, "plot/axis", bind(elements::plot::axis::build, _1, _2, _3));
  element_bind(elems, "plot/bars", bind(elements::plot::bars::build, _1, _2, _3));
  element_bind(elems, "plot/grid", bind(elements::plot::grid::build, _1, _2, _3));
  element_bind(elems, "plot/labels", bind(elements::plot::labels::build, _1, _2, _3));
  element_bind(elems, "chart/legend", bind(elements::chart::legend::build, _1, _2, _3));
  element_bind(elems, "plot/lines", bind(elements::plot::lines::build, _1, _2, _3));
  element_bind(elems, "chart/linechart", bind(elements::chart::linechart::build, _1, _2, _3));
  element_bind(elems, "plot/points", bind(elements::plot::points::build, _1, _2, _3));
  element_bind(elems, "chart/scatterplot", bind(elements::chart::scatterplot::build, _1, _2, _3));
  element_bind(elems, "layout/box", bind(elements::layout::box::build, _1, _2, _3));
  element_bind(elems, "text", bind(elements::text::build, _1, _2, _3));
  return ctx.release();
}

+14 −13
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ struct PlotGeom {
};

struct PlotAxis {
  std::string elem_name;
  std::string align;
  ExprStorage opts;
  size_t position;
};
@@ -250,14 +250,14 @@ ReturnCode configure_geom(
}

ReturnCode configure_axis(
    const std::string& axis_elem_name,
    const std::string& align,
    size_t axis_position,
    const Expr* expr,
    std::vector<PlotAxis>* axes) {
  auto opts = expr_clone(expr_get_list(expr));

  PlotAxis a;
  a.elem_name = axis_elem_name;
  a.align = align;
  a.opts = std::move(opts);
  a.position = axis_position;
  axes->emplace_back(std::move(a));
@@ -322,10 +322,10 @@ ReturnCode build(

    /* axis options */
    {"axes", bind(&expr_to_stringset, _1, &axes_auto)},
    {"axis-top", bind(&configure_axis, "plot/axis-top", 0, _1, &axes)},
    {"axis-right", bind(&configure_axis, "plot/axis-right", 1, _1, &axes)},
    {"axis-bottom", bind(&configure_axis, "plot/axis-bottom", 2, _1, &axes)},
    {"axis-left", bind(&configure_axis, "plot/axis-left", 3, _1, &axes)},
    {"axis-top", bind(&configure_axis, "bottom", 0, _1, &axes)},
    {"axis-right", bind(&configure_axis, "left", 1, _1, &axes)},
    {"axis-bottom", bind(&configure_axis, "top", 2, _1, &axes)},
    {"axis-left", bind(&configure_axis, "right", 3, _1, &axes)},
    {"axis-x-labels", bind(&expr_rewritev, _1, "labels", &axis_x_opts)},
    {"axis-y-labels", bind(&expr_rewritev, _1, "labels", &axis_y_opts)},
    {
@@ -466,28 +466,28 @@ ReturnCode build(

  if (axes_auto.count("top")) {
    PlotAxis a;
    a.elem_name = "plot/axis-top";
    a.align= "bottom";
    a.position = 0;
    axes.emplace_back(std::move(a));
  }

  if (axes_auto.count("right")) {
    PlotAxis a;
    a.elem_name = "plot/axis-right";
    a.align = "left";
    a.position = 1;
    axes.emplace_back(std::move(a));
  }

  if (axes_auto.count("bottom")) {
    PlotAxis a;
    a.elem_name = "plot/axis-bottom";
    a.align = "top";
    a.position = 2;
    axes.emplace_back(std::move(a));
  }

  if (axes_auto.count("left")) {
    PlotAxis a;
    a.elem_name = "plot/axis-left";
    a.align = "right";
    a.position = 3;
    axes.emplace_back(std::move(a));
  }
@@ -544,7 +544,9 @@ ReturnCode build(
    }

    auto elem_config = expr_build(
        axis.elem_name,
        "plot/axis",
        "align",
        axis.align,
        expr_unwrap(std::move(elem_config_defaults)),
        expr_clone(axis.opts.get()));

@@ -597,7 +599,6 @@ ReturnCode build(
    config->body_elements.emplace_back(elem);
  }


  /* return the layout element */
  *elem = std::make_shared<Element>();
  (*elem)->draw = bind(&draw, config, _1, _2);
+42 −33
Original line number Diff line number Diff line
@@ -297,11 +297,13 @@ static ReturnCode axis_draw_vertical(
      from_pt(kDefaultTickLengthPT, target->dpi));

  for (const auto& tick : ticks.positions) {
    auto y = y0 + (y1 - y0) * (1.0 - tick);
    auto ty = y0 + (y1 - y0) * (1.0 - tick);
    auto tx = x - tick_length + tick_length * (tick_position + 1) / 2.0;

    strokeLine(
        target,
        {x, y},
        {x + tick_length * tick_position, y},
        {tx, ty},
        {tx + tick_length, ty},
        axis_config.border_style);
  }

@@ -436,11 +438,12 @@ static ReturnCode axis_draw_horizontal(
      from_pt(kDefaultTickLengthPT, target->dpi));

  for (const auto& tick : ticks.positions) {
    auto x = x0 + (x1 - x0) * tick;
    auto ty = y - tick_length + tick_length * (tick_position + 1) / 2.0;
    auto tx = x0 + (x1 - x0) * tick;
    strokeLine(
        target,
        {x, y},
        {x, y + tick_length * tick_position},
        {tx, ty},
        {tx, ty + tick_length},
        axis_config.border_style);
  }

@@ -622,23 +625,33 @@ ReturnCode build(const Environment& env, const Expr* expr, ElementRef* elem) {
  config->border_style.line_width = from_pt(1);
  config->border_style.color = env.border_color;

  if (elem_name == "plot/axis-top") {
    config->align = AxisAlign::BOTTOM;
  }

  if (elem_name == "plot/axis-right") {
    config->align = AxisAlign::LEFT;
  {
    auto rc = expr_walk_map(expr_next(expr), {
      {
        "align",
        expr_to_enum_fn<AxisAlign>(&config->align, {
          {"x", AxisAlign::X},
          {"y", AxisAlign::Y},
          {"top", AxisAlign::TOP},
          {"right", AxisAlign::RIGHT},
          {"bottom", AxisAlign::BOTTOM},
          {"left", AxisAlign::LEFT},
        })
      }
    }, false);

  if (elem_name == "plot/axis-bottom") {
    config->align = AxisAlign::TOP;
    if (!rc) {
      return rc;
    }

  if (elem_name == "plot/axis-left") {
    config->align = AxisAlign::RIGHT;
  }

  switch (config->align) {
    case AxisAlign::X:
      config->label_attach = AxisLabelAttach::CENTER;
      config->title_offset = 1;
      config->tick_offset = 0;
      config->label_offset = 1;
      break;
    case AxisAlign::TOP:
      config->label_attach = AxisLabelAttach::CENTER;
      config->title_offset = 1;
@@ -651,6 +664,13 @@ ReturnCode build(const Environment& env, const Expr* expr, ElementRef* elem) {
      config->tick_offset = 1;
      config->label_offset = -1;
      break;
    case AxisAlign::Y:
      config->label_attach = AxisLabelAttach::BEGIN;
      config->title_offset = 1;
      config->title_rotate = -90;
      config->tick_offset = 0;
      config->label_offset = 1;
      break;
    case AxisAlign::LEFT:
      config->label_attach = AxisLabelAttach::BEGIN;
      config->title_offset = 1;
@@ -669,19 +689,6 @@ ReturnCode build(const Environment& env, const Expr* expr, ElementRef* elem) {

  {
    auto rc = expr_walk_map(expr_next(expr), {
      /* layout options */
      {
        "align",
        expr_to_enum_fn<AxisAlign>(&config->align, {
          {"x", AxisAlign::X},
          {"y", AxisAlign::X},
          {"top", AxisAlign::TOP},
          {"right", AxisAlign::RIGHT},
          {"bottom", AxisAlign::BOTTOM},
          {"left", AxisAlign::LEFT},
        })
      },

      /* label options */
      {"labels", bind(&format_configure, _1, &config->label_formatter)},
      {"label-placement", bind(&scale_configure_layout, _1, &config->label_placement)},
@@ -700,6 +707,8 @@ ReturnCode build(const Environment& env, const Expr* expr, ElementRef* elem) {

      /* tick options */
      {"tick-placement", bind(&scale_configure_layout, _1, &config->tick_placement)},
      {"tick-offset", bind(&expr_to_float64, _1, &config->tick_offset)},
      {"tick-length", bind(&expr_to_measure, _1, &config->tick_length)},

      /* scale options */
      {"scale", bind(&scale_configure_kind, _1, &config->scale)},
@@ -718,7 +727,7 @@ ReturnCode build(const Environment& env, const Expr* expr, ElementRef* elem) {
      {"border", bind(&expr_to_stroke_style, _1, &config->border_style)},
      {"border-color", bind(&expr_to_color, _1, &config->border_style.color)},
      {"border-width", bind(&expr_to_measure, _1, &config->border_style.line_width)},
    });
    }, false);

    if (!rc) {
      return rc;
+0 −25
Original line number Diff line number Diff line
name: plot/axis-bottom
desc: |
  The plot/axis-bottom element is used to draw a labelled 'bottom axis'.

reference: |
  (plot/axis-bottom <options>)

option_summary: true

properties:
  - title: "Label Options"
    anchor: label-options
    inherit: ["elements/plot/axis", "label-options"]

  - title: "Scale Options"
    anchor: scale-options
    inherit: ["elements/plot/axis", "scale-options"]

  - title: "Title Options"
    anchor: title-options
    inherit: ["elements/plot/axis", "title-options"]

  - title: "Border Options"
    anchor: border-options
    inherit: ["elements/plot/axis", "border-options"]

elements/plot/axis_left_ref.yaml

deleted100644 → 0
+0 −25
Original line number Diff line number Diff line
name: plot/axis-left
desc: |
  The plot/axis-left element is used to draw a labelled 'left axis'.

reference: |
  (plot/axis-left <options>)

option_summary: true

properties:
  - title: "Label Options"
    anchor: label-options
    inherit: ["elements/plot/axis", "label-options"]

  - title: "Scale Options"
    anchor: scale-options
    inherit: ["elements/plot/axis", "scale-options"]

  - title: "Title Options"
    anchor: title-options
    inherit: ["elements/plot/axis", "title-options"]

  - title: "Border Options"
    anchor: border-options
    inherit: ["elements/plot/axis", "border-options"]
Loading