Skip to content

Latest commit

 

History

History
179 lines (128 loc) · 5.28 KB

README.md

File metadata and controls

179 lines (128 loc) · 5.28 KB

PlotGenC++

A lightweight C++ library for generating 2D plots and charts using SFML, with optional WebKit-based HTML/SVG rendering.

Overview

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.

Features

  • 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)

Installation

Prerequisites

  • 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

Dependencies

Required Dependencies

  • SFML for graphic rendering
    • Installation: sudo apt install libsfml-dev
  • stb_image_write for image export (it is included in the repository)
    • Installation: No additional steps needed

Optional Dependencies

  • GTK3 and WebKit for enhanced SVG viewing
    • Installation: sudo apt install libgtk-3-dev libwebkit2gtk-4.1-dev

Display Modes

  • 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

Build and Install

# 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.

Usage

Simple Example

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

Running the Examples

Run the example application to see the various available features:

./build/PlotterExamples

Examples

Example 1: Basic 2D Plots

Basic 2D Plots

Example 2: Histograms

Histograms

Example 3: Polar Plots

Polar Plots

Example 4: Multiple Plots and Customization

Multiple Plots

Example 5: Advanced Histograms

Advanced Histograms

Example 6: Plots with Symbols

Plots with Symbols

Example 7: Circles, Text and Arrows

Circles, Text and Arrows

Example 8: Bezier and Spline Curves

Bezier and Spline Curves

Example 9: SVG Export Demo

SVG Export Demo

Documentation

Detailed documentation is available in the docs/ folder.

License

This project is under the GPL-3.0 License. See the LICENSE file for details.

Author

Sofiane KHELLADI

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.