This example demonstrates how to handle user interactions with plots using Plotly.js event callbacks. The program responds to mouse hover and click events on plot elements in real-time.
#include <iostream>
#include <vector>
std::vector<double> x = {1, 2, 3, 4, 5};
std::vector<double> y = {1, 4, 2, 8, 5};
{"x", x}, {"y", y}, {"type", "scatter"}, {"mode", "markers"}};
std::cout << "Hovering over point" << '\n';
auto xValue = event["points"][0]["x"];
auto yValue = event["points"][0]["y"];
{"x", xValue},
{"y", yValue},
{"text", "hover"},
{"showarrow", false},
{"yshift", 30},
});
std::cout << "Point clicked: x=" << event["points"][0]["x"]
<< ", y=" << event["points"][0]["y"] << '\n';
auto xValue = event["points"][0]["x"];
auto yValue = event["points"][0]["y"];
{"x", xValue},
{"y", yValue},
{"text", "click"},
{"showarrow", false},
{"yshift", 30},
});
return 0;
}
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.