Commit 55aa7961 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add 'plot gridlines' command

parent aea90821
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
 */
#include <signaltk.h>
#include <signaltk/core/image_api.h>
#include <signaltk/plot/plot_api.h>
#include <iostream>

namespace signaltk {
@@ -21,6 +22,12 @@ const std::vector<Command> command_list {
    .fn = &cmd_image_new,
  },

  // command: plot gridlines
  Command {
    .name = {"plot", "gridlines"},
    .fn = &cmd_plot_gridlines,
  },

};

int cmd(
@@ -32,12 +39,18 @@ int cmd(
      continue;
    }

    bool match = true;
    for (size_t i = 0; i < cmd.name.size(); ++i) {
      if (cmd.name[i] != args[i]) {
        continue;
        match = false;
        break;
      }
    }

    if (!match) {
      continue;
    }

    return cmd.fn(
        ctx,
        args + cmd.name.size(),
+0 −2
Original line number Diff line number Diff line
@@ -11,7 +11,6 @@
#define _libstx_UI_VIEWPORT_H

namespace signaltk {
namespace chart {

class Viewport {
public:
@@ -82,6 +81,5 @@ public:
  std::tuple<int, int, int, int> padding_;
};

}
}
#endif
+2 −1
Original line number Diff line number Diff line
@@ -16,4 +16,5 @@ add_library(signaltk-plot STATIC
    legenddefinition.cc
    series.cc
    timedomain.cc
    linechart.cc)
    linechart.cc
    plot_api.cc)
+66 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "signaltk" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * libstx is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <iostream>
#include <signaltk/core/layer.h>
#include <signaltk/plot/plot_layout.h>
#include <signaltk/util/flagparser.h>
#include "plot_api.h"

namespace signaltk {

int cmd_plot_gridlines(Context* ctx, const char** args, int arg_count) {
  FlagParser flag_parser;

  std::string flag_in;
  flag_parser.defineString("in", true, &flag_in);

  std::string flag_out;
  flag_parser.defineString("out", true, &flag_out);

  auto rc = flag_parser.parseArgv(arg_count, args);
  if (!rc.isSuccess()) {
    std::cerr << "ERROR: " << rc.getMessage() << std::endl;
    return -1;
  }

  Layer target;
  if (!target.loadPNG(flag_in.c_str())) {
    return -1; // FIXME error
  }

  {
    chart::GridDefinition grid(chart::GridDefinition::GRID_HORIZONTAL);
    grid.addTick(0.0);
    grid.addTick(0.2);
    grid.addTick(0.4);
    grid.addTick(0.6);
    grid.addTick(0.8);
    grid.addTick(1.0);
    chart::renderGrid(grid, Viewport{target.width, target.height}, &target);
  }

  {
    chart::GridDefinition grid(chart::GridDefinition::GRID_VERTICAL);
    grid.addTick(0.0);
    grid.addTick(0.2);
    grid.addTick(0.4);
    grid.addTick(0.6);
    grid.addTick(0.8);
    grid.addTick(1.0);
    chart::renderGrid(grid, Viewport{target.width, target.height}, &target);
  }

  target.writePNG(flag_out.c_str());

  return 0;
}

} // namespace signaltk
+18 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "signaltk" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * libstx is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#pragma once
#include <signaltk.h>

namespace signaltk {

int cmd_plot_gridlines(Context* ctx, const char** args, int arg_count);

} // namespace signaltk
Loading