Commit 16e6ac2e authored by Paul Asmuth's avatar Paul Asmuth
Browse files

update the 'chart/labels' element

parent ffde11a0
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ include_directories(${CAIRO_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS} ${HARFBUZZ_IN

# Build: fviz Library
# -----------------------------------------------------------------------------
file(GLOB source_files "core/**/*.cc" "core/*.cc" "elements/*.cc"  "elements/layout/*.cc" "elements/chart/axis.cc" "elements/chart/layout.cc" "elements/chart/points.cc" "elements/chart/lines.cc" "elements/chart/areas.cc")
file(GLOB source_files "core/**/*.cc" "core/*.cc" "elements/*.cc"  "elements/layout/*.cc" "elements/chart/axis.cc" "elements/chart/layout.cc" "elements/chart/points.cc" "elements/chart/lines.cc" "elements/chart/areas.cc" "elements/chart/labels.cc")
list(REMOVE_ITEM source_files "core/cli.cc")
add_library(fviz STATIC ${source_files})
set_target_properties(fviz PROPERTIES PUBLIC_HEADER "source/fviz.h;source/fviz_sdl.h")
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include "elements/chart/axis.h"
#include "elements/chart/layout.h"
#include "elements/chart/areas.h"
#include "elements/chart/labels.h"
#include "elements/chart/lines.h"
#include "elements/chart/points.h"
#include "elements/layout/padding.h"
@@ -53,6 +54,7 @@ fviz_t* fviz_init() {
  element_bind(elems, "chart/axis-left", bind(elements::chart::axis::build, _1, _2, _3));
  element_bind(elems, "chart/layout", bind(elements::chart::layout::build, _1, _2, _3));
  element_bind(elems, "chart/areas", bind(elements::chart::areas::build, _1, _2, _3));
  element_bind(elems, "chart/labels", bind(elements::chart::labels::build, _1, _2, _3));
  element_bind(elems, "chart/lines", bind(elements::chart::lines::build, _1, _2, _3));
  element_bind(elems, "chart/points", bind(elements::chart::points::build, _1, _2, _3));
  element_bind(elems, "layout/padding", bind(elements::layout::padding::build, _1, _2, _3));
+112 −25
Original line number Diff line number Diff line
@@ -11,41 +11,76 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "plot_labels.h"
#include <fviz.h>
#include <graphics/path.h>
#include <graphics/brush.h>
#include <graphics/text.h>
#include <graphics/layout.h>
#include "source/config_helpers.h"
#include "source/utils/algo.h"
#include "labels.h"

#include "layout.h"
#include "scale.h"
#include "sexpr.h"
#include "sexpr_conv.h"
#include "sexpr_util.h"
#include "graphics/path.h"
#include "graphics/brush.h"
#include "graphics/text.h"
#include "graphics/layout.h"

using namespace std::placeholders;

namespace fviz {
namespace plot {
namespace labels {
namespace fviz::elements::chart::labels {

static const double kDefaultLabelPaddingEM = 0.8;

struct PlotLabelsConfig {
  std::vector<Measure> x;
  std::vector<Measure> y;
  ScaleConfig scale_x;
  ScaleConfig scale_y;
  std::vector<std::string> labels;
  FontInfo label_font;
  Measure label_padding;
  Measure label_font_size;
  Color label_color;
};

ReturnCode draw(
    const PlotLabelsConfig& config,
    const Rectangle& clip,
    std::shared_ptr<PlotLabelsConfig> config,
    const LayoutInfo& layout,
    Layer* layer) {
  for (size_t i = 0; i < config.labels.size(); ++i) {
    const auto& label_text = config.labels[i];
  const auto& clip = layout.content_box;

  /* convert units */
  convert_units(
      {
        bind(&convert_unit_typographic, layer->dpi, layer->font_size.value, _1),
        bind(&convert_unit_user, scale_translate_fn(config->scale_x), _1),
        bind(&convert_unit_relative, clip.w, _1)
      },
      &*config->x.begin(),
      &*config->x.end());

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

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

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

    TextStyle style;
    style.font = config.label_font;
    style.color = config.label_color;
    style.font_size = config.label_font_size;
    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;
@@ -57,7 +92,59 @@ ReturnCode draw(
  return OK;
}

} // namespace labels
} // namespace plot
} // namespace fviz
ReturnCode build(
    const Environment& env,
    const Expr* expr,
    ElementRef* elem) {
  /* set defaults from environment */
  auto config = std::make_shared<PlotLabelsConfig>();
  config->label_font = env.font;
  config->label_font_size = env.font_size;

  /* parse properties */
  auto config_rc = expr_walk_map(expr_next(expr), {
    {"xdata", bind(&expr_to_measures, _1, &config->x)},
    {"ydata", bind(&expr_to_measures, _1, &config->y)},
    {"xmin", bind(&expr_to_float64_opt, _1, &config->scale_x.min)},
    {"xmax", bind(&expr_to_float64_opt, _1, &config->scale_x.max)},
    {"xscale", bind(&scale_configure_kind, _1, &config->scale_x)},
    {"xscale-padding", bind(&expr_to_float64, _1, &config->scale_x.padding)},
    {"ymin", bind(&expr_to_float64_opt, _1, &config->scale_y.min)},
    {"ymax", bind(&expr_to_float64_opt, _1, &config->scale_y.max)},
    {"yscale", bind(&scale_configure_kind, _1, &config->scale_y)},
    {"yscale-padding", bind(&expr_to_float64, _1, &config->scale_y.padding)},
    {"labels", bind(&expr_to_strings, _1, &config->labels)},
    {"label-font-size", bind(&expr_to_measure, _1, &config->label_font_size)},
  });

  if (!config_rc) {
    return config_rc;
  }

  /* check configuraton */
  if (config->x.size() != config->y.size()) {
    return ReturnCode::error(
        "EARG",
        "the length of the 'xs' and 'ys' properties must be equal");
  }

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

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

  *elem = std::make_shared<Element>();
  (*elem)->draw = bind(&draw, config, _1, _2);
  return OK;
}

} // namespace fviz::elements::chart::labels
+7 −32
Original line number Diff line number Diff line
@@ -12,39 +12,14 @@
 * limitations under the License.
 */
#pragma once
#include <stdlib.h>
#include <sexpr.h>
#include <graphics/layer.h>
#include <source/core/scale.h>
#include <source/element.h>
#include <source/config_helpers.h>
#include "element.h"

namespace fviz {
namespace plot {
namespace labels {
namespace fviz::elements::chart::labels {

struct PlotLabelsConfig {
  std::vector<double> x;
  std::vector<double> y;
  std::vector<std::string> labels;
  FontInfo label_font;
  Measure label_padding;
  Measure label_font_size;
  Color label_color;
};
ReturnCode build(
    const Environment& env,
    const Expr* expr,
    ElementRef* elem);

ReturnCode draw(
    const PlotLabelsConfig& config,
    const Rectangle& clip,
    Layer* layer);;

ReturnCode configure(
    const plist::PropertyList& plist,
    const DataContext& data,
    const Document& doc,
    PlotLabelsConfig* config);

} // namespace labels
} // namespace plot
} // namespace fviz
} // namespace fviz::elements::chart::labels