Plotly.cpp 0.1.0
A C++ plotting library for expressive, interactive, real-time & streaming data visualization
Loading...
Searching...
No Matches
gallery_hello_world.cpp

Hello World Example

This example demonstrates the most basic usage of the plotly.cpp library by creating a simple scatter plot with both line and marker visualization.

What You'll Learn

  • Basic plot creation with scatter traces
  • Layout configuration with titles
  • Interactive browser display vs headless image export
  • Command line argument parsing for different execution modes

Sample Output

The example creates a scatter plot with the following data points:

  • X coordinates: [1, 2, 3, 4, 5]
  • Y coordinates: [1, 4, 2, 8, 5]

The resulting plot shows both line connections and individual markers, with the title "Hello World!" displayed at the top.

Hello World Example Output
See also
plotly::Figure For the main plotting interface
parseGalleryArgs() For command line argument handling
#include <vector>
auto main(int argc, char *argv[]) -> int {
// Parse command line arguments for headless/interactive mode
auto args = parseGalleryArgs(argc, argv);
// Create a new figure instance
fig.openBrowser(args.headless); // Configure browser opening behavior
// Sample data points for demonstration
std::vector<double> x = {1, 2, 3, 4, 5};
std::vector<double> y = {1, 4, 2, 8, 5};
// Create trace object with Plotly.js-compatible structure
plotly::Object trace = {
{"x", x}, {"y", y}, {"type", "scatter"}, {"mode", "lines+markers"}};
// Create layout object with title
plotly::Object layout = {{"title", {{"text", "Hello World!"}}}};
// Generate the plot with trace data and layout configuration
fig.newPlot(plotly::Array{trace}, layout);
// Handle output based on execution mode
if (!args.headless) {
fig.waitClose(); // Wait until browser is closed
} else {
plotly::Object imageOpts = {
{"format", "png"},
{"width", 800},
{"height", 600},
{"filename", "hello_world"}
};
fig.downloadImage(imageOpts);
}
return 0;
}
auto parseGalleryArgs(int argc, char *argv[]) -> GalleryArgs
Parse command line arguments for gallery examples.
Definition arg_parser.cpp:4
Handle for creating and manipulating a Plotly figure.
Definition plotly.hpp:48
void waitClose() const
Wait until the figure is closed (no client connected).
Definition plotly.cpp:406
auto downloadImage(const Object &opts=Object()) -> bool
Download the figure as an image.
Definition plotly.cpp:413
auto newPlot(const Object &data, const Object &layout=Object(), const Object &config=Object()) -> bool
Create and render a new plot.
Definition plotly.cpp:408
auto openBrowser(bool headless=false) -> bool
Open the figure in the browser.
Definition plotly.cpp:529
auto main() -> int
Definition gallery_animate_sin_wave.cpp:48
std::vector< Object > Array
Definition plotly.hpp:27
nlohmann::json Object
Definition plotly.hpp:26
Public Plotly C++ API header.