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

linechart: add axis label formatter

parent 1ab27060
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ add_library(plotfxlib STATIC
    charts/legenddefinition.cc
    charts/series.cc
    common/config_helpers.cc
    common/format.cc
    common/plist/plist.cc
    common/plist/plist_parser.cc
    common/graphics/path.cc
+5 −1
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ namespace plotfx {
AxisDefinition::AxisDefinition() :
    mode(AxisMode::AUTO),
    label_placement(AxisLabelPlacement::OUTSIDE),
    label_formatter(format_decimal_fixed(1)),
    label_padding_rem(1.0f),
    label_font_size_rem(1.0f),
    tick_length_rem(0.4f) {}
@@ -242,7 +243,10 @@ ReturnCode axis_expand_linear_geom(
    auto o = (1.0f / (num_ticks - 1)) * i;
    auto v = min + (max - min) * o;
    axis->ticks.emplace_back(o);
    axis->labels.emplace_back(o, std::to_string(v)); // FIXME formatter

    if (axis->label_formatter.format_number) {
      axis->labels.emplace_back(o, axis->label_formatter.format_number(v));
    }
  }

  return OK;
+2 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
#include <graphics/layer.h>
#include <graphics/viewport.h>
#include <utils/return_code.h>
#include <common/format.h>

namespace plotfx {
struct DomainConfig;
@@ -71,6 +72,7 @@ struct AxisDefinition {
  std::vector<double> ticks;
  std::vector<std::pair<double, std::string>> labels;
  AxisLabelPlacement label_placement;
  Formatter label_formatter;
  double label_padding_rem;
  double label_font_size_rem;
  double tick_length_rem;

common/format.cc

0 → 100644
+59 −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 <sstream>
#include <iomanip>
#include "format.h"

namespace plotfx {

Formatter format_decimal_scientific(size_t precision) {
  Formatter f;
  f.format_number = [precision] (double v) -> std::string {
    std::stringstream s;
    s << std::scientific << std::setprecision(precision) << v;
    return s.str();
  };

  return f;
}

Formatter format_decimal_fixed(size_t precision) {
  Formatter f;
  f.format_number = [precision] (double v) -> std::string {
    std::stringstream s;
    s << std::fixed << std::setprecision(precision) << v;
    return s.str();
  };

  return f;
}

} // namespace plotfx

common/format.h

0 → 100644
+48 −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.
 */
#pragma once
#include <unordered_map>
#include <optional>
#include <plist/plist.h>
#include "utils/return_code.h"

namespace plotfx {

struct Formatter {
  std::function<std::string (double)> format_number;
  std::function<std::string (const std::string&)> format_string;
};

Formatter format_decimal_scientific(size_t precision);

Formatter format_decimal_fixed(size_t precision);

} // namespace plotfx