Skip to content

Method Chaining

To support a more compact syntax, the library allows method chaining on plot objects. For instance, we can create a simple line plot and modify its appearance by

1
2
3
4
5
6
// Using the line handle
auto p = plot(x,y,"--gs");
p->line_width(2);
p->marker_size(10);
p->marker_color("b");
p->marker_face_color({.5,.5,.5});
1
2
3
4
5
6
// Method chaining
plot(x,y,"--gs")
  ->line_width(2)
  .marker_size(10)
  .marker_color("b")
  .marker_face_color({.5,.5,.5});

The first code snippet works because plot returns a line_handle to the object in the axes. We can use this line handle to modify the line plot. Whenever we modify a property, the setter function calls touch, which will draw the figure again if it is in reactive mode. The second option works because setters return a reference to *this rather than void.