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

add examples/charts-scientific/vectorfield.fvz

parent 6d59a037
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -75,7 +75,8 @@ Formatter format_decimal_scientific(size_t precision) {
      return "0";
    }

    auto e = int(log10(v));
    auto s = v >= 0 ? 1 : -1;
    auto e = int(log10(v / s));
    auto x = v / pow(10, e);

    std::stringstream sstream;
+23 −0
Original line number Diff line number Diff line
@@ -131,10 +131,33 @@ double scale_translate(
  return 0.0f;
}

double scale_translate_magnitude(
    const ScaleConfig& domain,
    double value) {
  switch (domain.kind) {
    case ScaleKind::LINEAR:
    case ScaleKind::LOGARITHMIC: {
      double min = scale_min(domain);
      double max = scale_max(domain);
      return value / (max - min);
    }
    case ScaleKind::CATEGORICAL:
      return 1;
    default:
      return std::numeric_limits<double>::quiet_NaN();
  }

  return 0.0f;
}

std::function<double (double)> scale_translate_fn(const ScaleConfig& domain) {
  return bind(&scale_translate, domain, std::placeholders::_1);
}

std::function<double (double)> scale_translate_magnitude_fn(const ScaleConfig& domain) {
  return bind(&scale_translate_magnitude, domain, std::placeholders::_1);
}

double  scale_untranslate_linear(const ScaleConfig& domain, double vt) {
  auto min = scale_min(domain);
  auto max = scale_max(domain);
+6 −0
Original line number Diff line number Diff line
@@ -67,12 +67,18 @@ double scale_translate(
    const ScaleConfig& domain,
    double value);

double scale_translate_magnitude(
    const ScaleConfig& domain,
    double value);

double scale_translate_categorical(
    const ScaleConfig& domain,
    size_t index);

std::function<double (double)> scale_translate_fn(const ScaleConfig& domain);

std::function<double (double)> scale_translate_magnitude_fn(const ScaleConfig& domain);

double scale_untranslate(
    const ScaleConfig& domain,
    double data);
+47 −39
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@
#include "core/typographic_map.h"
#include "core/typographic_reader.h"
#include "core/layout.h"
#include "core/marker.h"
#include "core/arrows.h"
#include "core/scale.h"
#include "graphics/path.h"
#include "graphics/brush.h"
@@ -34,8 +34,7 @@ using namespace std::placeholders;

namespace fviz::elements::plot::vectors {

static const double kDefaultPointSizePT = 4;
static const double kDefaultLabelPaddingEM = 0.4;
static const double kDefaultArrowSizePT = 1;

struct PlotPointsConfig {
  std::vector<Measure> x;
@@ -48,8 +47,8 @@ struct PlotPointsConfig {
  std::vector<Color> colors;
  Measure size;
  std::vector<Measure> sizes;
  Marker shape;
  std::vector<Marker> shapes;
  Arrow shape;
  std::vector<Arrow> shapes;
  std::vector<std::string> labels;
  FontInfo label_font;
  Measure label_padding;
@@ -83,6 +82,24 @@ ReturnCode draw(
      &*config->y.begin(),
      &*config->y.end());

  convert_units(
      {
        bind(&convert_unit_typographic, layer->dpi, layer->font_size, _1),
        bind(&convert_unit_user, scale_translate_magnitude_fn(config->scale_x), _1),
        bind(&convert_unit_relative, clip.w, _1)
      },
      &*config->dx.begin(),
      &*config->dx.end());

  convert_units(
      {
        bind(&convert_unit_typographic, layer->dpi, layer->font_size, _1),
        bind(&convert_unit_user, scale_translate_magnitude_fn(config->scale_y), _1),
        bind(&convert_unit_relative, clip.h, _1)
      },
      &*config->dy.begin(),
      &*config->dy.end());

  convert_units(
      {
        bind(&convert_unit_typographic, layer->dpi, layer->font_size, _1)
@@ -95,10 +112,12 @@ ReturnCode draw(
      layer->font_size,
      &config->size);

  /* draw markers */
  /* draw vectors */
  for (size_t i = 0; i < config->x.size(); ++i) {
    auto sx = clip.x + config->x[i];
    auto sy = clip.y + clip.h - config->y[i];
    auto dx = config->dx[i];
    auto dy = config->dy[i];

    const auto& color = config->colors.empty()
        ? config->color
@@ -112,35 +131,7 @@ ReturnCode draw(
        ? config->shape
        : config->shapes[i % config->shapes.size()];

    if (auto rc = shape(Point(sx, sy), size, color, layer); !rc) {
      return rc;
    }
  }

  /* draw labels */
  for (size_t i = 0; i < config->labels.size(); ++i) {
    const auto& label_text = config->labels[i];

    auto size = config->sizes.empty()
        ? 0
        : config->sizes[i % config->sizes.size()].value;

    auto label_padding = size * 0.5 + measure_or(
        config->label_padding,
        from_em(kDefaultLabelPaddingEM, config->label_font_size));

    Point p(
        clip.x + config->x[i],
        clip.y + clip.h - config->y[i] - label_padding);

    TextStyle style;
    style.font = config->label_font;
    style.color = config->label_color;
    style.font_size = config->label_font_size;

    auto ax = HAlign::CENTER;
    auto ay = VAlign::BOTTOM;
    if (auto rc = drawTextLabel(label_text, p, ax, ay, style, layer); rc != OK) {
    if (auto rc = shape({sx, sy}, {sx + dx, sy + dy}, size, color, layer); !rc) {
      return rc;
    }
  }
@@ -155,8 +146,8 @@ ReturnCode build(
  /* set defaults from environment */
  auto c = std::make_shared<PlotPointsConfig>();
  c->color = env.foreground_color;
  c->size = from_pt(kDefaultPointSizePT);
  c->shape = marker_create_disk();
  c->size = from_pt(kDefaultArrowSizePT);
  c->shape = arrow_create_default();
  c->label_font = env.font;
  c->label_font_size = env.font_size;

@@ -168,6 +159,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)},
@@ -175,7 +167,6 @@ ReturnCode build(
    {"data-dx", bind(&data_load_strings, _1, &data_dx)},
    {"data-dy", bind(&data_load_strings, _1, &data_dy)},
    {"data-color", bind(&data_load_strings, _1, &data_colors)},
    {"data-shape", bind(&marker_configure_list, _1, &c->shapes)},
    {"limit-x", bind(&expr_to_float64_opt_pair, _1, &c->scale_x.min, &c->scale_x.max)},
    {"limit-x-min", bind(&expr_to_float64_opt, _1, &c->scale_x.min)},
    {"limit-x-max", bind(&expr_to_float64_opt, _1, &c->scale_x.max)},
@@ -186,9 +177,10 @@ ReturnCode build(
    {"scale-y", bind(&scale_configure_kind, _1, &c->scale_y)},
    {"scale-x-padding", bind(&expr_to_float64, _1, &c->scale_x.padding)},
    {"scale-y-padding", bind(&expr_to_float64, _1, &c->scale_y.padding)},
    {"shape", bind(&marker_configure, _1, &c->shape)},
    {"color", bind(&color_read, env, _1, &c->color)},
    {"color-map", bind(&color_map_read, env, _1, &color_map)},
    {"size", bind(&measure_read, _1, &c->size)},
    {"size-map", bind(&measure_map_read, env, _1, &size_map)},
  });

  if (!config_rc) {
@@ -253,6 +245,22 @@ ReturnCode build(
    c->colors.push_back(color);
  }

  /* 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);
  }

  /* return element */
  *elem = std::make_shared<Element>();
  (*elem)->draw = bind(&draw, c, _1, _2);
+13 −0
Original line number Diff line number Diff line
(set width 2000px)
(set height 1000px)
(set dpi 200)
(set font "Latin Modern Roman")

(plot
  limit-x (-4 4)
  limit-y (-2 2)
  vectors (
      data-x (csv "tests/testdata/vectorfield.csv" x)
      data-y (csv "tests/testdata/vectorfield.csv" y)
      data-dx (csv "tests/testdata/vectorfield.csv" dx)
      data-dy (csv "tests/testdata/vectorfield.csv" dy)))
Loading