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

linechart: load series data

parent cef4b43e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ add_library(plotfxlib STATIC
    charts/plot_domain.cc
    charts/legenddefinition.cc
    charts/series.cc
    common/config_helpers.cc
    common/plist/plist.cc
    common/plist/plist_parser.cc
    common/graphics/path.cc
+21 −2
Original line number Diff line number Diff line
@@ -198,8 +198,7 @@ void LineChart::render(
LinechartConfig::LinechartConfig() :
    x_domain(PlotDomain::LINEAR),
    y_domain(PlotDomain::LINEAR),
    margin_rem(4.0f),
    padding_rem(0.0f) {}
    margin_rem(4.0f) {}

ReturnCode draw(
    const LinechartConfig& config,
@@ -301,12 +300,32 @@ ReturnCode configureAxisLeft(const plist::Property& prop, LinechartConfig* confi
  return parseAxisMode(prop[0], &config->axis_left.mode);
}


ReturnCode configureSeries(const plist::Property& prop, LinechartConfig* config) {
  if (!prop.child) {
    return ERROR_INVALID_ARGUMENT;
  }

  LinechartSeries series;
  static const ParserDefinitions pdefs = {
    {"xs", std::bind(&parseDataSeries, std::placeholders::_1, &series.xs)},
    {"ys", std::bind(&parseDataSeries, std::placeholders::_1, &series.ys)},
  };

  if (auto rc = parseAll(*prop.child, pdefs); !rc) {
    return rc;
  }

  return OK;
}

ReturnCode configure(const plist::PropertyList& plist, ElementRef* elem) {
  static const PropertyDefinitions<LinechartConfig> pdefs = {
    {"axis-top", &configureAxisTop},
    {"axis-right", &configureAxisRight},
    {"axis-bottom", &configureAxisBottom},
    {"axis-left", &configureAxisLeft},
    {"series", &configureSeries},
  };

  LinechartConfig config;
+6 −1
Original line number Diff line number Diff line
@@ -40,6 +40,11 @@
namespace plotfx {
namespace linechart {

struct LinechartSeries {
  std::vector<double> xs;
  std::vector<double> ys;
};

struct LinechartConfig {
  LinechartConfig();
  PlotDomain x_domain;
@@ -49,7 +54,7 @@ struct LinechartConfig {
  AxisDefinition axis_bottom;
  AxisDefinition axis_left;
  double margin_rem;
  double padding_rem;
  std::vector<LinechartSeries> series;
};

ReturnCode draw(const LinechartConfig& config, const Rectangle& clip, Layer* frame);
+54 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "config_helpers.h"
#include <iostream>

namespace plotfx {

ReturnCode parseDataSeries(
    const plist::Property& prop,
    std::vector<double>* data) {
  std::cerr << "configure series data " << prop.size() << " \n";
  for (const auto& v : prop.values) {
    double value;
    try {
      value = std::stod(v.data);
    } catch (... ) {
      return ERROR_INVALID_ARGUMENT;
    }

    data->emplace_back(value);
  }

  return OK;
}

} // namespace plotfx
+29 −0
Original line number Diff line number Diff line
@@ -34,6 +34,31 @@

namespace plotfx {

using ParserDefinitions = std::unordered_map<
    std::string,
    std::function<ReturnCode (const plist::Property&)>>;

inline ReturnCode parseAll(
    const plist::PropertyList& plist,
    const ParserDefinitions& pdefs) {
  for (const auto& prop : plist) {
    const auto& pdef = pdefs.find(prop.name);
    if (pdef != pdefs.end()) {
      if (auto rc = pdef->second(prop); !rc.isSuccess()) {
        return ReturnCode::errorf(
            "EPARSE",
            "error while parsing property '$0': $1",
            prop.name,
            rc.getMessage());

        return rc;
      }
    }
  }

  return ReturnCode::success();
}

template <typename T>
using PropertyDefinitions = std::unordered_map<
    std::string,
@@ -79,5 +104,9 @@ ReturnCode parseEnum(
  return ReturnCode::success();
}

ReturnCode parseDataSeries(
    const plist::Property& prop,
    std::vector<double>* data);

} // namespace plotfx
Loading