Commit 0f0e3b55 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

convert element into simple struct, convert linechart to element builder

parent 009a1fad
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -17,10 +17,10 @@ include_directories(${CAIRO_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS} ${HARFBUZZ_IN
add_definitions(-DFNORDMETRIC_VERSION="unstable")

add_library(plotfxlib STATIC
    charts/linechart.cc
    charts/gridlines.cc
    charts/plot_axis.cc
    charts/plot_domain.cc
    charts/plot_element.cc
    charts/legenddefinition.cc
    charts/series.cc
    common/plist/plist.cc
+97 −1
Original line number Diff line number Diff line
@@ -28,14 +28,18 @@
 * 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 <plotfx.h>
#include <graphics/path.h>
#include <graphics/brush.h>
#include <graphics/text.h>
#include "linechart.h"
#include "common/config_helpers.h"

namespace plotfx {
namespace chart {
namespace linechart {

/*
 *
char LineChart::kDefaultLineStyle[] = "solid";
char LineChart::kDefaultLineWidth[] = "2";
char LineChart::kDefaultPointStyle[] = "none";
@@ -207,5 +211,97 @@ void LineChart::setLabels(bool show_labels) {
  show_labels_ = show_labels;
}

*/

LinechartConfig::LinechartConfig() :
    x_domain(PlotDomain::LINEAR),
    y_domain(PlotDomain::LINEAR) {}

ReturnCode draw(const LinechartConfig& config, Layer* frame) {
  // render axes
  if (auto rc = renderAxis(config.axis_top, AxisPosition::TOP, frame); rc) {
    return rc;
  }

  if (auto rc = renderAxis(config.axis_right, AxisPosition::RIGHT, frame); rc) {
    return rc;
  }

  if (auto rc = renderAxis(config.axis_bottom, AxisPosition::BOTTOM, frame); rc) {
    return rc;
  }

  if (auto rc = renderAxis(config.axis_left, AxisPosition::LEFT, frame); rc) {
    return rc;
  }

  return ReturnCode::success();
}

ReturnCode configureAxisTop(const plist::Property& prop, LinechartConfig* config) {
  if (prop.size() != 1) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  return parseAxisMode(prop[0], &config->axis_top.mode);
}

ReturnCode configureAxisRight(const plist::Property& prop, LinechartConfig* config) {
  if (prop.size() != 1) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  return parseAxisMode(prop[0], &config->axis_right.mode);
}

ReturnCode configureAxisBottom(const plist::Property& prop, LinechartConfig* config) {
  if (prop.size() != 1) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  return parseAxisMode(prop[0], &config->axis_bottom.mode);
}

ReturnCode configureAxisLeft(const plist::Property& prop, LinechartConfig* config) {
  if (prop.size() != 1) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  return parseAxisMode(prop[0], &config->axis_left.mode);
}

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},
  };

  LinechartConfig config;
  if (auto rc = configureProperties(plist, pdefs, &config); !rc.isSuccess()) {
    return rc;
  }

  auto e = std::make_unique<Element>();
  e->draw = std::bind(&draw, config, std::placeholders::_1);
  *elem = std::move(e);

  return ReturnCode::success();
}

} // namespace linechart
} // namespace plotfx
+21 −70
Original line number Diff line number Diff line
@@ -30,81 +30,32 @@
 */
#pragma once
#include <stdlib.h>
#include <plist/plist.h>
#include <graphics/layer.h>
#include <graphics/viewport.h>
#include "series.h"
#include "continuousdomain.h"
#include "domain.h"
#include "domainprovider.h"
#include "colorpalette.h"
#include <common/element.h>
#include "plot_axis.h"
#include "plot_domain.h"

namespace plotfx {
namespace chart {

/**
 * This draws the series data as points.
 *
 * OPTIONS
 *
 *   line_style = {solid,dashed}, default: solid
 *   line_width = default: 2
 *
 */
class LineChart {
public:
  using TX = double;
  using TY = double;

  static const constexpr int kLabelPadding = 8;
  static char kDefaultLineStyle[];
  static char kDefaultLineWidth[];
  static char kDefaultPointStyle[];
  static char kDefaultPointSize[];

  /**
   * Create a new line chart
   *
   * @param canvas the canvas to draw this chart on. does not transfer ownership
   * @param x_domain the x domain. does not transfer ownership
   * @param y_domain the y domain. does not transfer ownership
   */
  LineChart(AnyDomain* x_domain, AnyDomain* y_domain);

  /**
   * Add a (x: string, y: double) series. This will draw one connected line
   * through all points in the series
   *
   * @param series the series to add. does not transfer ownership
   * @param line_width the line widht
   * @param smooth smooth this line?
   * @param line_style the line style ({solid,dashed})
   */
  void addSeries(Series2D<TX, TY>* series);

  /**
   * Get the {x,y} domain of this chart. Will raise an exception if z domain
   * is requested.
   *
   * The returned pointer is owned by the chart object and must not be freed
   * by the caller!
   *
   * @param dimension the dimension (x,y)
   */
  AnyDomain* getDomain(AnyDomain::kDimension dimension);
namespace linechart {

struct LinechartConfig {
  LinechartConfig();
  PlotDomain x_domain;
  PlotDomain y_domain;
  AxisDefinition axis_top;
  AxisDefinition axis_right;
  AxisDefinition axis_bottom;
  AxisDefinition axis_left;
};

  void setLabels(bool show_labels);
ReturnCode draw(const LinechartConfig& config, Layer* frame);

  void render(
      Layer* target,
      Viewport* viewport) const;
ReturnCode configure(
    const plist::PropertyList& plist,
    ElementRef* elem);

protected:
  DomainProvider x_domain_;
  DomainProvider y_domain_;
  std::vector<Series2D<TX, TY>*> series_;
  ColorPalette color_palette_;
  bool show_labels_;
};
} // namespace linechart
} // namespace plotfx
}
}
+0 −1
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@
 */
#include "plot_axis.h"
#include <assert.h>
#include <charts/plot_element.h>
#include <common/config_helpers.h>
#include <graphics/text.h>
#include <graphics/brush.h>

charts/plot_element.cc

deleted100644 → 0
+0 −138
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 "charts/plot_element.h"
#include "common/config_helpers.h"

namespace plotfx {

PlotConfig::PlotConfig() :
    x_domain(PlotDomain::LINEAR),
    y_domain(PlotDomain::LINEAR) {}

ReturnCode renderPlot(const PlotConfig& config, Layer* frame) {
  // render axes
  if (auto rc = renderAxis(config.axis_top, AxisPosition::TOP, frame); rc) {
    return rc;
  }

  if (auto rc = renderAxis(config.axis_right, AxisPosition::RIGHT, frame); rc) {
    return rc;
  }

  if (auto rc = renderAxis(config.axis_bottom, AxisPosition::BOTTOM, frame); rc) {
    return rc;
  }

  if (auto rc = renderAxis(config.axis_left, AxisPosition::LEFT, frame); rc) {
    return rc;
  }

  return ReturnCode::success();
}

ReturnCode configurePlotAxisTop(const plist::Property& prop, PlotConfig* config) {
  if (prop.size() != 1) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  return parseAxisMode(prop[0], &config->axis_top.mode);
}

ReturnCode configurePlotAxisRight(const plist::Property& prop, PlotConfig* config) {
  if (prop.size() != 1) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  return parseAxisMode(prop[0], &config->axis_right.mode);
}

ReturnCode configurePlotAxisBottom(const plist::Property& prop, PlotConfig* config) {
  if (prop.size() != 1) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  return parseAxisMode(prop[0], &config->axis_bottom.mode);
}

ReturnCode configurePlotAxisLeft(const plist::Property& prop, PlotConfig* config) {
  if (prop.size() != 1) {
    return ReturnCode::errorf(
        "EARG",
        "incorrect number of arguments; expected: 1, got: $0",
        prop.size());
  }

  return parseAxisMode(prop[0], &config->axis_left.mode);
}

ReturnCode configurePlot(const PropertyList& plist, PlotConfig* config) {
  static const PropertyDefinitions<PlotConfig> pdefs = {
    {"axis-top", &configurePlotAxisTop},
    {"axis-right", &configurePlotAxisRight},
    {"axis-bottom", &configurePlotAxisBottom},
    {"axis-left", &configurePlotAxisLeft},
  };

  if (auto rc = configureProperties(plist, pdefs, config); !rc.isSuccess()) {
    return rc;
  }

  return ReturnCode::success();
}

ReturnCode PlotElement::configure(
    const PropertyList& plist,
    std::unique_ptr<Element>* elem) {
  PlotConfig config;
  if (auto rc = configurePlot(plist, &config); !rc.isSuccess()) {
    return rc;
  }

  *elem = std::make_unique<PlotElement>(config);
  return ReturnCode::success();
}

PlotElement::PlotElement(const PlotConfig& c) : config(c) {}

ReturnCode PlotElement::renderTo(Layer* frame) const {
  return renderPlot(config, frame);
}

} // namespace plotfx
Loading