Commit 12c815dd authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add the basic 'linechart' macro element

parent 415ad6d0
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@
#include "elements/chart/labels.h"
#include "elements/chart/legend.h"
#include "elements/chart/lines.h"
#include "elements/chart/linechart.h"
#include "elements/chart/points.h"
#include "elements/layout/box.h"

@@ -60,6 +61,7 @@ fviz_t* fviz_init() {
  element_bind(elems, "chart/labels", bind(elements::chart::labels::build, _1, _2, _3));
  element_bind(elems, "chart/legend", bind(elements::chart::legend::build, _1, _2, _3));
  element_bind(elems, "chart/lines", bind(elements::chart::lines::build, _1, _2, _3));
  element_bind(elems, "chart/linechart", bind(elements::chart::linechart::build, _1, _2, _3));
  element_bind(elems, "chart/points", bind(elements::chart::points::build, _1, _2, _3));
  element_bind(elems, "layout/box", bind(elements::layout::box::build, _1, _2, _3));
  return ctx.release();
+5 −0
Original line number Diff line number Diff line
@@ -129,5 +129,10 @@ ReturnCode expr_to_color_opt(
  return OK;
}

ReturnCode expr_to_copy(const Expr* e, ExprStorage* c) {
  *c = expr_clone(e);
  return OK;
}

} // namespace fviz
+4 −0
Original line number Diff line number Diff line
@@ -59,6 +59,10 @@ ReturnCode expr_to_color_opt(
    const Expr* expr,
    std::optional<Color>* var);

ReturnCode expr_to_copy(
    const Expr* expr,
    ExprStorage* copy);

template <typename T>
ReturnCode expr_to_enum(
    const Expr* expr,
+147 −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 "linechart.h"

#include "data.h"
#include "scale.h"
#include "element_factory.h"
#include "sexpr_conv.h"
#include "sexpr_util.h"

#include <numeric>
#include <functional>

using namespace std::placeholders;

namespace fviz::elements::chart::linechart {

ReturnCode build(
    const Environment& env,
    const Expr* expr,
    ElementRef* elem) {
  std::vector<Measure> x;
  std::vector<Measure> y;
  ScaleConfig xscale;
  ScaleConfig yscale;

  ExprStorage xdata;
  ExprStorage ydata;

  auto config_rc = expr_walk_map(expr_next(expr), {
    {
      "xdata",
      expr_calln_fn({
        bind(&data_load, _1, &x),
        bind(&expr_to_copy, _1, &xdata),
      }),
    },
    {
      "ydata",
      expr_calln_fn({
        bind(&data_load, _1, &y),
        bind(&expr_to_copy, _1, &ydata),
      }),
    },
    {"xmin", bind(&expr_to_float64_opt, _1, &xscale.min)},
    {"xmax", bind(&expr_to_float64_opt, _1, &xscale.max)},
    {"ymin", bind(&expr_to_float64_opt, _1, &yscale.min)},
    {"ymax", bind(&expr_to_float64_opt, _1, &yscale.max)},
    {"yscale", bind(&scale_configure_kind, _1, &yscale)},
    {"xscale", bind(&scale_configure_kind, _1, &xscale)},
  });

  if (!config_rc) {
    return config_rc;
  }

  /* scale autoconfig */
  for (const auto& v : x) {
    if (v.unit == Unit::USER) {
      scale_fit(v.value, &xscale);
    }
  }

  for (const auto& v : y) {
    if (v.unit == Unit::USER) {
      scale_fit(v.value, &yscale);
    }
  }

  auto xmin = expr_create_value(std::to_string(scale_min(xscale)));
  auto xmax = expr_create_value(std::to_string(scale_max(xscale)));
  auto ymin = expr_create_value(std::to_string(scale_min(yscale)));
  auto ymax = expr_create_value(std::to_string(scale_max(yscale)));

  auto chart_body = expr_build(
      "chart/lines",
      "xdata",
      std::move(xdata),
      "ydata",
      std::move(ydata),
      "xmin",
      expr_clone(xmin.get()),
      "xmax",
      expr_clone(xmax.get()),
      "ymin",
      expr_clone(ymin.get()),
      "ymax",
      expr_clone(ymax.get()));

  auto chart_axis_top = expr_build(
      "chart/axis-top",
      "min",
      expr_clone(xmin.get()),
      "max",
      expr_clone(xmax.get()));

  auto chart_axis_right = expr_build(
      "chart/axis-right",
      "min",
      expr_clone(ymin.get()),
      "max",
      expr_clone(ymax.get()));

  auto chart_axis_bottom = expr_build(
      "chart/axis-bottom",
      "min",
      expr_clone(xmin.get()),
      "max",
      expr_clone(xmax.get()));

  auto chart_axis_left = expr_build(
      "chart/axis-left",
      "min",
      expr_clone(ymin.get()),
      "max",
      expr_clone(ymax.get()));

  auto chart = expr_build(
      "chart/layout",
      "top",
      expr_create_list(std::move(chart_axis_top)),
      "right",
      expr_create_list(std::move(chart_axis_right)),
      "bottom",
      expr_create_list(std::move(chart_axis_bottom)),
      "left",
      expr_create_list(std::move(chart_axis_left)),
      "body",
      expr_create_list(std::move(chart_body)));

  std::cerr << expr_inspect(chart.get());
  return element_build_macro(env, std::move(chart), elem);
}

} // namespace fviz::elements::chart::linechart
+25 −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 "element.h"

namespace fviz::elements::chart::linechart {

ReturnCode build(
    const Environment& env,
    const Expr* expr,
    ElementRef* elem);

} // namespace fviz::elements::chart::linechart
Loading