Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Tutorial: Simple 2D Program

Simple Code Example
Simple Image
Basic Example Breakdown

Simple Code Example

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

using std::multimap;
using namespace boost::svg;

int main()
{
	multimap<double, double> map1;
	multimap<double, double> map2;

	// This is random data used purely for example purposes.	
	map1[1.] = 3.2;
	map1[1.] = 5.4;
	map1[7.3] = 9.1;

	map2[3.1] = 6.1;
	map2[5.4] = 7.;

	svg_1d_plot my_plot;

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

	my_plot.plot(map1, "Series 1", blue);
	my_plot.plot(map2, "Series 2", orange);

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

Simple Image

2d_simple

Basic Example Breakdown

Let's examine what this does.

svg_2d_plot my_plot;

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

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

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 images.
  • background_border_color(black) sets the border around the image to black. Ordinarily it is left to be the color of the background.
my_plot.plot(map1, "Series 1", blue);
my_plot.plot(map2, "Series 2", orange);

This draws map1 and map2 to my_plot. As many containers as you want can be drawn to my_plot. After a certain point, however, I recommend just creating another 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 Getting More Out of the plot() Function

my_plot.write("simple_2d.svg");

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

Copyright © 2007 Jake Voytko

PrevUpHomeNext