stephenberry / quicksvg

Quick code to generate an SVG in C++ which graphs a function or time series.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

quicksvg

This is a tiny library for generating SVG files from within C++. It plots time series, graphs functions, and plots ULP accuracy. That's it. If you use it, you should think "huh, that kinda did what I wanted", and accept that the result is good enough, or you should give up immediately.

Here's an example of a function graphed:

Alt text

Here's an example of a time series:

Alt text

Here's an example of a ULP accuracy plot:

Alt text

How do we graph a function?

#include "quicksvg/graph_fn.hpp"
// ...
double a = -3.14159;
double b = 3.14159;
std::string title = "sin(𝑥) and cos(𝑥)";
std::string filename = "examples/sine_and_cosine.svg";
auto f = [](double x)->double { return std::sin(x); };
auto g = [](double x)->double { return std::cos(x); };
quicksvg::graph_fn sin_graph(a, b, title, filename);

sin_graph.add_fn(f);
sin_graph.add_fn(g, "green");

sin_graph.write_all();

How do we graph a time series?

#include "quicksvg/plot_time_series.hpp"
// ...
std::vector<double> v(50);
std::vector<double> u(50);
double start_time = 0;
double time_step = 0.25;
for (size_t i = 0; i < v.size(); ++i) {
  v[i] = std::sin(start_time + i*time_step);
  u[i] = std::cos(start_time + i*time_step);
}

title = "sine and cosine time series";
filename = "examples/sin_cos_time_series.svg";

quicksvg::plot_time_series pts(start_time, time_step, title, filename);
pts.add_dataset(v);
pts.add_dataset(u, /* connect the dots? */ false, "lime", "lightgreen");
pts.write_all();

How do we create a ULP accuracy plot?

#include "quicksvg/ulp_plot.hpp"
// ...
int samples = 25000;

double a = 0;
double b = 1000000;
std::string title = "ULP accuracy of double precision Lambert W₀ on [0, 10⁶)";
std::string filename = "examples/ulp_lambert_w0_0_mil.svg";
quicksvg::ulp_plot(lambert_w0<double>, lambert_w0<boost::multiprecision::cpp_bin_float_50>, a, b, title, filename, samples);

About

Quick code to generate an SVG in C++ which graphs a function or time series.

License:MIT License


Languages

Language:C++ 89.4%Language:CMake 8.9%Language:Makefile 1.6%