Commit 65f9d700 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

plot/points: add the 'size-map' option

parent 3aaae927
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "typographic_map.h"

namespace fviz {

MeasureMap measure_map_linear(const Measure& min, const Measure& max) {
  return [min, max] (const std::string& value, Measure* measure) -> ReturnCode {
    double value_num  = 0.0;
    try {
      value_num = std::clamp(std::stod(value), 0.0, 1.0);
    } catch (...) {
      return errorf(
          ERROR,
          "invalid data; can't map '{}' to a typographic unit",
          value);
    }

    *measure = from_unit(double(min) + (double(max) - double(min)) * value_num);
    return OK;
  };
}

} // namespace fviz

core/typographic_map.h

0 → 100644
+26 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once
#include "graphics/measure.h"
#include "graphics/color.h"
#include "return_code.h"

namespace fviz {

using MeasureMap = std::function<ReturnCode (const std::string& v, Measure* m)>;

MeasureMap measure_map_linear(const Measure& min, const Measure& max);

} // namespace fviz
+56 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
 */
#include "typographic_reader.h"
#include "sexpr_conv.h"
#include "sexpr_util.h"

using namespace std::placeholders;

@@ -49,5 +50,60 @@ ReturnCode measure_read_list(
  return expr_tov<Measure>(expr, bind(&measure_read, _1, _2), measures);
}

ReturnCode measure_map_read_linear(
    const Environment& env,
    const Expr* expr,
    MeasureMap* measure_map) {
  auto args = expr_collect(expr);
  if (args.size() != 2) {
    return errorf(
        ERROR,
        "invalid number of arguments for 'linear'; expected two, got: {}",
        args.size());
  }

  Measure begin;
  if (auto rc = measure_read(args[0], &begin); !rc) {
    return rc;
  }

  Measure end;
  if (auto rc = measure_read(args[1], &end); !rc) {
    return rc;
  }

  *measure_map = measure_map_linear(begin, end);
  return OK;
}

ReturnCode measure_map_read(
    const Environment& env,
    const Expr* expr,
    MeasureMap* measure_map) {
  if (!expr || !expr_is_list(expr)) {
    return errorf(
        ERROR,
        "invalid argument to <measure-map>; expected a list, but got: '{}'",
        expr_inspect(expr));
  }

  expr = expr_get_list(expr);

  if (expr_is_value(expr, "linear")) {
    return measure_map_read_linear(env, expr_next(expr), measure_map);
  }

  return errorf(
      ERROR,
      "invalid value to <measure-map>; got '{}', but expected one of: \n"
      "  - linear\n",
      expr_inspect(expr));
}

ReturnCode measure_map_read(
    const Environment& e,
    const Expr* expr,
    MeasureMap* measure_map);

} // namespace fviz
+7 −0
Original line number Diff line number Diff line
@@ -12,7 +12,9 @@
 * limitations under the License.
 */
#pragma once
#include "environment.h"
#include "graphics/measure.h"
#include "typographic_map.h"
#include "return_code.h"
#include "sexpr.h"

@@ -30,5 +32,10 @@ ReturnCode measure_read_list(
    const Expr* expr,
    std::vector<Measure>* measures);

ReturnCode measure_map_read(
    const Environment& e,
    const Expr* expr,
    MeasureMap* measure_map);

} // namespace fviz
+12 −2
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@
#include "sexpr_util.h"
#include "core/environment.h"
#include "core/color_reader.h"
#include "core/typographic_map.h"
#include "core/typographic_reader.h"
#include "core/layout.h"
#include "core/marker.h"
#include "core/scale.h"
@@ -162,6 +164,7 @@ ReturnCode build(
  std::vector<std::string> data_colors;
  std::vector<std::string> data_sizes;
  ColorMap color_map;
  MeasureMap size_map;

  auto config_rc = expr_walk_map(expr_next(expr), {
    {"data-x", bind(&data_load_strings, _1, &data_x)},
@@ -181,6 +184,7 @@ ReturnCode build(
    {"scale-y-padding", bind(&expr_to_float64, _1, &c->scale_y.padding)},
    {"marker-size", bind(&measure_read, _1, &c->size)},
    {"marker-shape", bind(&marker_configure, _1, &c->shape)},
    {"size-map", bind(&measure_map_read, env, _1, &size_map)},
    {"color", bind(&color_read, env, _1, &c->color)},
    {"color-map", bind(&color_map_read, env, _1, &color_map)},
    {"labels", bind(&data_load_strings, _1, &c->labels)},
@@ -244,9 +248,15 @@ ReturnCode build(
  /* convert size data */
  for (const auto& value : data_sizes) {
    Measure m;
    if (size_map) {
      if (auto rc = size_map(value, &m); !rc) {
        return rc;
      }
    } else {
      if (auto rc = parse_measure(value, &m); !rc) {
        return rc;
      }
    }

    c->sizes.push_back(m);
  }
Loading