Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Tutorial: 1D Simple Program

Code Example
Image
A note on syntax
Basic Example Breakdown

Code Example

#include <boost/svg_plot/svg_1d_plot.hpp>
#include <vector>

using std::vector;
using namespace boost::svg;

int main()
{
	vector<double> dan_times;
	vector<double> elaine_times;

	dan_times.push_back(3.1);
	dan_times.push_back(4.2);
	elaine_times.push_back(2.1);
	elaine_times.push_back(7.8);

	svg_1d_plot my_plot;

	my_plot.title("Race Times")
	       .legend_on(true)
	       .x_range(-1, 11);

	my_plot.plot(dan_times, "Dan", blue);
	my_plot.plot(elaine_times, "Elaine", orange);

	my_plot.write("simple.svg");
	return 0;
}

Image

1d_simple

A note on syntax

The syntax "my_plot.title("Hello").legend_on(true)..." may appear unfamiliar. However, it works on the same principle that the assignment operator, addition operator, and output operator in the following code works:

a = b = c = d = 3;

//prints 12
std::cout << a + b + c + d << std::endl;

Within all of the plot classes, "chaining" works the same way. The equivalent code for the example is as follows:

my_plot.title("Race Times");
my_plot.legend_on(true);
my_plot.x_range(-1, 11);

In the long run, I think you will find that my choice helps organize the code a little better!

Basic Example Breakdown

Let's examine what this does.

svg_1d_plot my_plot;

This initializes a new 1D plot. This also sets many of the default values.

my_plot.title("Race Times")
       .legend_on(true)
       .x_range(-1, 11);

All of the setter methods are fairly self-explanatory. To walk through it once,

  • the title, which will appear at the top of the graph, will say "Race Times".
  • legend_on(true) means that the legend will show up.
  • x_range(-1, 11) means that the axis displayed will be between -1 and 11, as you can see in the above image.
my_plot.plot(my_data, "Race times");

This draws my_data to my_plot. As many containers as you want can be drawn to my_plot. The name of the series is "Race times", and that text will show up in the legend. These are the two required parameters for this function call. There are optional parameters, as seen in the section svg_plot/svg_1d_plot_interface.html

my_plot.write("simple.svg");

This writes our plot to the file "simple.svg".

Copyright © 2007 Jake Voytko

PrevUpHomeNext