-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgallery_event_handling.cpp
More file actions
95 lines (83 loc) · 3 KB
/
gallery_event_handling.cpp
File metadata and controls
95 lines (83 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @file gallery_event_handling.cpp
* @brief Interactive Event Handling - User Interaction with Plots
* @author plotly.cpp contributors
* @date 2025
*
* @example gallery_event_handling.cpp
*
* # Interactive Event Handling Example
*
* 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.
*
* ## What You'll Learn
* - Event-driven programming with interactive plots
* - Handling plotly_hover and plotly_click events
* - Dynamic plot annotation based on user interactions
* - Bidirectional communication between C++ backend and browser
* - Real-time plot updates using relayout() function
*
* ## Sample Output
* The example creates an interactive scatter plot where:
* - Hovering over points displays "hover" annotation at the point location
* - Clicking on points displays "click" annotation and prints coordinates to
* console
* - Both interactions dynamically update the plot with blue text annotations
*
* @image html event_handling.gif "Interactive Event Handling Example"
*
* @see plotly::Figure::on() For event callback registration
* @see plotly::Figure::relayout() For dynamic layout updates
*/
#include "plotly/plotly.hpp"
#include <iostream>
#include <vector>
auto main() -> int {
plotly::Figure fig;
fig.openBrowser();
// Create plot
std::vector<double> x = {1, 2, 3, 4, 5};
std::vector<double> y = {1, 4, 2, 8, 5};
plotly::Object trace = {
{"x", x}, {"y", y}, {"type", "scatter"}, {"mode", "markers"}};
fig.newPlot(plotly::Array{trace});
// Register hover event handler
fig.on("plotly_hover", [&fig](const plotly::Object &event) -> void {
std::cout << "Hovering over point" << '\n';
// Get point information
auto xValue = event["points"][0]["x"];
auto yValue = event["points"][0]["y"];
// Add annotation with "hover" text
plotly::Object annotation = {
{"x", xValue},
{"y", yValue},
{"text", "hover"},
{"showarrow", false},
{"yshift", 30},
{"font", plotly::Object{{"color", "blue"}, {"size", 20}}}};
plotly::Object layoutUpdate = {{"annotations", plotly::Array{annotation}}};
fig.relayout(layoutUpdate);
});
// Register click event handler
fig.on("plotly_click", [&fig](const plotly::Object &event) -> void {
std::cout << "Point clicked: x=" << event["points"][0]["x"]
<< ", y=" << event["points"][0]["y"] << '\n';
// Get point information
auto xValue = event["points"][0]["x"];
auto yValue = event["points"][0]["y"];
// Add annotation with "click" text
plotly::Object annotation = {
{"x", xValue},
{"y", yValue},
{"text", "click"},
{"showarrow", false},
{"yshift", 30},
{"font", plotly::Object{{"color", "blue"}, {"size", 20}}}};
plotly::Object layoutUpdate = {{"annotations", plotly::Array{annotation}}};
fig.relayout(layoutUpdate);
});
fig.waitClose();
return 0;
}