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

clean up

parent 2169ac4e
Loading
Loading
Loading
Loading

core/config_helpers.cc

deleted100644 → 0
+0 −209
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 "config_helpers.h"
#include "utils/fileutil.h"
#include "utils/csv.h"
#include "utils/algo.h"
#include <iostream>

using namespace std::placeholders;

namespace fviz {

/*

ParserFn configure_alt(const ParserDefinitions& parsers) {
  return [parsers] (const Expr* prop) -> ReturnCode {
    std::string parser_key = "";
    if (plist::is_enum(prop) && parsers.count(prop.value)) {
      parser_key = prop.value;
    }

    return parsers.at(parser_key)(prop);
  };
}

ReturnCode configure_float(
    const Expr* prop,
    double* value) {
  if (!plist::is_value(prop)) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  try {
    *value = std::stod(prop);
  } catch (... ) {
    return ERROR;
  }

  return OK;
}

ReturnCode configure_float_opt(
    const Expr* prop,
    std::optional<double>* value) {
  if (!plist::is_value(prop)) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  try {
    *value = std::optional<double>(std::stod(prop));
  } catch (... ) {
    return ERROR;
  }

  return OK;
}
*/


/*
ReturnCode configure_direction(
    const Expr* prop,
    Direction* value) {
  if (!plist::is_value(prop)) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  static const EnumDefinitions<Direction> defs = {
    { "horizontal", Direction::HORIZONTAL },
    { "vertical", Direction::VERTICAL },
  };

  return parseEnum(defs, prop.value, value);
}

ReturnCode configure_position(
    const Expr* prop,
    Position* value) {
  if (!plist::is_value(prop)) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  static const EnumDefinitions<Position> defs = {
    { "relative", Position::RELATIVE },
    { "absolute", Position::ABSOLUTE },
    { "top", Position::TOP },
    { "right", Position::RIGHT },
    { "bottom", Position::BOTTOM },
    { "left", Position::LEFT },
  };

  return parseEnum(defs, prop.value, value);
}

ReturnCode load_csv(
    const std::string& csv_path,
    bool csv_headers,
    SeriesMap* data) {
  std::string csv_data_str;
  if (auto rc = read_file(csv_path, &csv_data_str); !rc) {
    return rc;
  }

  auto csv_data = CSVData{};
  CSVParserConfig csv_opts;
  if (auto rc = parseCSV(csv_data_str, csv_opts, &csv_data); !rc) {
    return rc;
  }

  std::optional<size_t> column_count;
  for (const auto& row : csv_data) {
    if (!column_count || row.size() < column_count) {
      column_count = row.size();
    }
  }

  for (size_t i = 0; i < column_count; ++i) {
    std::string series_name;

    auto row = csv_data.begin();
    if (csv_headers &&
        row != csv_data.end() &&
        row->size() > i) {
      series_name = (row++)->at(i);
    } else {
      series_name = std::to_string(i);
    }

    auto series = std::make_shared<Series>();
    for (; row != csv_data.end(); ++row) {
      if (row->size() > i) {
        series->emplace_back(row->at(i));
      } else {
        series->emplace_back();
      }
    }

    (*data)[series_name] = series;
  }

  return OK;
}

ReturnCode parse_data_series_csv(
    const Expr* prop,
    SeriesRef* data_ref) {
  if (!plist::is_enum(prop, "csv")) {
    return ERROR;
  }

  if (prop.size() < 2) {
    return ReturnCode::errorf("EARG", "csv() takes exactly two or more arguments, got: $0", prop.size());
  }

  const auto& csv_path = prop[0].value;
  const auto& csv_column = prop[1].value;
  bool csv_headers = true;

  for (size_t i = 2; i < prop.size(); ++i) {
    if (prop[i].value == "noheaders") {
      csv_headers = false;
      continue;
    }
  }

  SeriesMap csv_data;
  if (auto rc = load_csv(csv_path, csv_headers, &csv_data); !rc) {
    return rc;
  }

  *data_ref = find_maybe(csv_data, csv_column);

  if (*data_ref) {
    return OK;
  } else {
    return ReturnCode::errorf(
        "EARG",
        "CSV file '$0' has no column named '$1'", 
        csv_path,
        csv_column);
  }
}

*/
} // namespace fviz

core/config_helpers.h

deleted100644 → 0
+0 −104
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 <unordered_map>
#include <optional>
#include <sexpr.h>
#include <graphics/measure.h>
#include <graphics/color.h>
#include "graphics/layout.h"
#include "utils/return_code.h"
#include "core/layout.h"

namespace fviz {

using ParserFn = std::function<ReturnCode (const Expr*)>;

template <typename T>
using ParseToFn = std::function<ReturnCode (const Expr*, T* value)>;

using ParserDefinitions = std::unordered_map<std::string, ParserFn>;

ReturnCode parseAll(
    const Expr* expr,
    const ParserDefinitions& pdefs);
/*
template <typename T>
using EnumDefinitions = std::unordered_map<std::string, T>;

template<typename T>
ReturnCode parseEnum(
    const EnumDefinitions<T>& defs,
    const std::string& str,
    T* value);

ReturnCode parse_classlike(
    const Expr* prop,
    const std::string& fn,
    std::vector<std::string>* args);

template <typename T>
ParserFn configure_opt(ParserFn parser);

template <typename T>
ParserFn configure_vec(ParseToFn<T> parser, std::vector<T>* values);

ParserFn configure_multiprop(const std::vector<ParserFn>& parsers);

ParserFn configure_alt(
    const ParserDefinitions& parsers);

ReturnCode configure_measure(
    const Expr* prop,
    Measure* value);

ReturnCode configure_measure_opt(
    const Expr* prop,
    std::optional<Measure>* value);

ReturnCode configure_color(
    const Expr* prop,
    Color* value);

ParserFn configure_color_fn(Color* var);
ParserFn configure_color_opt(std::optional<Color>* var);

ReturnCode configure_float(
    const Expr* prop,
    double* value);

ReturnCode configure_direction(
    const Expr* prop,
    Direction* value);

ReturnCode configure_position(
    const Expr* prop,
    Position* value);

ReturnCode configure_float_opt(
    const Expr* prop,
    std::optional<double>* value);

ReturnCode configure_strings(
    const Expr* prop,
    std::vector<std::string>* data);

ReturnCode configure_measures(
    const Expr* prop,
    std::vector<Measure>* measures);
*/

} // namespace fviz

#include "config_helpers_impl.h"

core/config_helpers_impl.h

deleted100644 → 0
+0 −35
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

namespace fviz {

/*
template<typename T>
ReturnCode parseEnum(
    const EnumDefinitions<T>& defs,
    const std::string& str,
    T* value) {
  const auto& def = defs.find(str);
  if (def == defs.end()) {
    return ReturnCode::errorf("EPARSE", "invalid value '$0'", str);
  }

  *value = def->second;
  return ReturnCode::success();
}
*/

} // namespace fviz
+37 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "data_model.h"
#include "data.h"
#include <assert.h>
#include <iostream>

@@ -83,5 +83,41 @@ std::vector<DataGroup> series_group(const Series& data) {
  return groups;
}

std::vector<Color> series_to_colors(
    SeriesRef series,
    const ScaleConfig& domain_config,
    const ColorScheme& palette) {
  if (!series) {
    return {};
  }

  auto domain = domain_config;
  //domain_fit(*series, &domain);

  std::vector<Color> colors;
  //for (const auto& v : *series) {
  //  auto value = domain_translate(domain, v) * domain_cardinality(domain);
  //  colors.emplace_back(palette.get(value));
  //}

  return colors;
}

std::vector<Color> groups_to_colors(
    size_t count,
    const std::vector<DataGroup>& groups,
    const ColorScheme& palette) {
  std::vector<Color> colors(count);
  for (size_t gi = 0; gi < groups.size(); ++gi) {
    for (auto i : groups[gi].index) {
      if (i < colors.size()) {
        colors[i] = palette.get(gi);
      }
    }
  }

  return colors;
}

} // namespace fviz
+18 −0
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
#include <string>
#include <vector>
#include "return_code.h"
#include "graphics/measure.h"
#include "scale.h"

namespace fviz {

@@ -39,5 +41,21 @@ std::vector<double> series_to_float(const Series& s);
double value_to_float(const Value&);
Value value_from_float(double);

std::vector<Color> series_to_colors(
    SeriesRef series,
    const ScaleConfig& domain_config,
    const ColorScheme& palette);

std::vector<Color> groups_to_colors(
    size_t count,
    const std::vector<DataGroup>& groups,
    const ColorScheme& palette);

std::vector<Measure> series_to_sizes(
    SeriesRef series,
    const ScaleConfig& domain_config,
    const Measure& low,
    const Measure& high);

} // namespace fviz
Loading