A lightweight C++ library for generating 2D plots and charts using SFML, with optional WebKit-based HTML/SVG rendering.
PlotGenC++ is a C++ library designed for generating 2D plots and charts. It is built on top of the SFML (Simple and Fast Multimedia Library) and provides a simple interface for creating various types of plots, including line plots, histograms, and polar plots. The library is inspired by popular plotting libraries in Python and MATLAB, making it easy to use for those familiar with those environments.
- Various 2D Charts with multiple style options
- Histograms with customizable bars
- Polar Plots for parametric and polar functions
- Circle Drawing with automatic scaling
- Text Annotations at specific coordinates
- Lines, Arcs and Arrows for geometric annotations
- Multiple layouts to display several plots on the same figure
- Bézier Curves for smooth curve generation with control points
- Splines including natural cubic splines and cardinal splines
- Complete customization of colors, symbols, grids, and legends
- PNG/JPG export for integration into documents
- SVG export for high-quality vector graphics suitable for publications
- Configurable legend positioning including outside the plot area
- Visual representation of styles in legends (lines, symbols, etc.)
- Enhanced SVG viewer (optional, requires WebKit and GTK3)
- CMake 3.10 or higher
- C++ compiler with C++17 support
- SFML 2.5 or higher
- (Optional) GTK3 and WebKit for enhanced SVG viewing capabilities
- SFML for graphic rendering
- Installation:
sudo apt install libsfml-dev
- Installation:
- stb_image_write for image export (it is included in the repository)
- Installation: No additional steps needed
- GTK3 and WebKit for enhanced SVG viewing
- Installation:
sudo apt install libgtk-3-dev libwebkit2gtk-4.1-dev
- Installation:
- If GTK3 and WebKit are installed, PlotGenC++ will use an embedded HTML/SVG viewer for enhanced visualization
- If these libraries are not available, visualization will fall back to SFML-based rendering automatically
# Clone the repository
git clone https://github.com/skhelladi/PlotGenCpp.git
# Create a build directory
cd PlotGenCpp
mkdir build && cd build
# Configure and build
cmake ..
make
# Install (optional)
sudo make install
During the cmake configuration, you will see a status message indicating whether HTMLViewer will be enabled or disabled based on the availability of GTK3 and WebKit on your system.
#include "plotgen.h"
#include <vector>
#include <cmath>
int main() {
PlotGen plt(800, 600); // Create an 800x600 window
// Generate data
std::vector<float> x(100), y(100);
for (int i = 0; i < 100; ++i) {
x[i] = i * 0.1f - 5.0f;
y[i] = std::sin(x[i]);
}
// Configure and plot
auto& fig = plt.subplot(0, 0);
PlotGen::Style style;
style.color = sf::Color::Blue;
style.legend = "sin(x)";
plt.set_title(fig, "Sine Wave Plot");
plt.set_xlabel(fig, "x");
plt.set_ylabel(fig, "sin(x)");
plt.set_axis_limits(fig, -5, 5, -1.2, 1.2);
plt.grid(fig, true, false);
plt.plot(fig, x, y, style);
// Position the legend in the top-left corner
plt.set_legend_position(fig, "top-left");
// Add a circle and text annotation
PlotGen::Style circle_style;
circle_style.color = sf::Color::Red;
circle_style.thickness = 2.0;
plt.circle(fig, 0, 0, 1.0, circle_style);
PlotGen::Style text_style;
text_style.color = sf::Color::Green;
plt.text(fig, 0, 1.5, "Maximum value", text_style);
// Display and save as PNG/JPG and SVG
plt.save("sinusoid.png");
plt.save_svg("sinusoid.svg");
plt.show();
return 0;
}
Run the example application to see the various available features:
./build/PlotterExamples
Detailed documentation is available in the docs/
folder.
This project is under the GPL-3.0 License. See the LICENSE file for details.
Sofiane KHELLADI
Contributions are welcome! Please feel free to submit a Pull Request.