|
Boost-Commit : |
From: pbristow_at_[hidden]
Date: 2008-05-24 12:27:15
Author: pbristow
Date: 2008-05-24 12:27:14 EDT (Sat, 24 May 2008)
New Revision: 45719
URL: http://svn.boost.org/trac/boost/changeset/45719
Log:
New demonstration examples.
Added:
sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_autoscaling.cpp (contents, props changed)
sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_axis_scaling.cpp (contents, props changed)
sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_containers.cpp (contents, props changed)
sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_plot.cpp (contents, props changed)
sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_values.cpp (contents, props changed)
sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_vector.cpp (contents, props changed)
sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_2d_bars.cpp (contents, props changed)
sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_2d_histogram.cpp (contents, props changed)
Added: sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_autoscaling.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_autoscaling.cpp 2008-05-24 12:27:14 EDT (Sat, 24 May 2008)
@@ -0,0 +1,196 @@
+// demo_1d_autoscaling.cpp
+
+// Copyright Jacob Voytko 2007
+// Copyright Paul A Bristow 2008
+
+// Use, modification and distribution are subject to the
+// Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// An example to demonstrate simple 1D settings, including *auto-scaling*.
+// See auto_1d_containers.cpp for an example autoscaling with multiple data series.
+// See also demo_1d_plot.cpp for a wider range of use.
+
+// This file is written to be included from a Quickbook .qbk document.
+// It can be compiled by the C++ compiler, and run. Any output can
+// also be added here as comment or included or pasted in elsewhere.
+
+// Caution: this file contains Quickbook markup as well as code
+// and comments: don't change any of the special comment markups!
+
+//[demo_1d_autoscaling_1
+
+/*`First some includes to use Boost.Plot
+(and some others only needed for this example).
+*/
+
+#include <boost/svg_plot/svg_1d_plot.hpp>
+ using namespace boost::svg;
+ using boost::svg::svg_1d_plot;
+
+#include <boost/svg_plot/show_1d_settings.hpp>
+// Only needed for showing which settings in use.
+ void boost::svg::show_1d_plot_settings(svg_1d_plot&);
+ // (Also provides operator<< for std::pair).
+
+#include <boost/algorithm/minmax.hpp>
+ using boost::minmax;
+#include <boost/algorithm/minmax_element.hpp>
+ using boost::minmax_element;
+
+#include <iostream> // for debugging.
+ using std::cout;
+ using std::endl;
+ using std::boolalpha;
+
+#include <limits>
+ using std::numeric_limits;
+
+#include <vector>
+ using std::vector;
+#include <set>
+ using std::multiset;
+
+#include <utility>
+ using std::pair;
+
+#include <boost/svg_plot/detail/auto_axes.hpp>
+ using boost::svg::show; // A single STL container.
+ using boost::svg::show_all; // Multiple STL containers.
+ using boost::svg::range; // Find min and max of a STL container.
+ using boost::svg::range_all;// Find min and max of multipler STL containers.
+
+//] [/demo_1d_autoscaling_1]
+
+double tol100eps = 1000 * numeric_limits<double>::epsilon(); // suitable tight value.
+
+int main()
+{
+ //[demo_1d_autoscaling_2
+ /*`This example uses a few types of containers to demonstrate autoscaling.
+ Autoscaling can inspect the container in order to find axis ranges that will be suitable.
+ First we create a container and fill with some fictional data, and display the values.
+ */
+
+ vector<double> my_data;
+ // Initialize my_data with some entirely fictional data.
+ my_data.push_back(0.2);
+ my_data.push_back(1.1); // [1]
+ my_data.push_back(4.2); // [2]
+ my_data.push_back(3.3); // [3]
+ my_data.push_back(5.4); // [4]
+ my_data.push_back(6.5); // [5]
+ show(my_data); // Show entire container contents,
+ // 6 values in container: 0.2 1.1 4.2 3.3 5.4 6.5
+
+ /*`Construct a plot , and add some data to the plot. */
+
+ svg_1d_plot my_1d_plot; // Construct a plot with all the default constructor values.
+
+ my_1d_plot.plot(my_data, "Auto 1D my_data"); // Add whole data series from my_data.
+
+ /*`Use x_autoscale to scale the axis, in this most common and simplest case, using all the values.*/
+ my_1d_plot.x_autoscale(my_data);
+
+ /*` and finally write the SVG to a file.*/
+ my_1d_plot.write("demo_1d_autoscaling.svg"); // Write the plot to file.
+
+//] [/demo_1d_autoscaling_2]
+
+
+ { // Autoscaling with my_set
+ try
+ {
+//[demo_1d_autoscaling_3
+ /*`In a second example, we use a different type of container, a set,
+ and use autoscale in a more advanced way.*/
+ multiset<double> my_set;
+ // Initialize my_set with some entirely fictional data.
+ my_set.insert(1.2);
+ my_set.insert(2.3);
+ my_set.insert(3.4);
+ my_set.insert(4.5);
+ my_set.insert(5.6);
+ my_set.insert(6.7);
+ my_set.insert(7.8);
+ my_set.insert(8.9);
+ // Show the set.
+ multiset<double>::const_iterator si;
+ show(my_set); // for two different types of container.
+ // 8 values in container: 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9
+
+ svg_1d_plot my_1d_plot; // Construct a plot with all the default constructor values.
+
+ /*`and also override the default controls of scale_axis function used by autoscaling.*/
+ // Set some autoscaling parameters:
+ my_1d_plot.x_with_zero(true); // Always include zero in the axis range, even if the data values don't.
+ my_1d_plot.x_min_ticks(10); // Ensure more than the default minimum number of ticks.
+ my_1d_plot.x_steps(0); // Permits axis ticks in steps of 1,2,4,6,8 or 1, 5, 10, or 1, 2, 5, 10.
+ my_1d_plot.x_tight(0.01); // Prevent values that are only 1% outside the last tick requiring another tick.
+
+ // Show the flags just set.
+ cout << (my_1d_plot.x_with_zero() ? "x_with_zero, " : "not x_with_zero, ") // x_with_zero
+ << my_1d_plot.x_min_ticks() << " x_min_ticks, " // 10
+ << my_1d_plot.x_steps() << " x_steps, " // 0
+ << my_1d_plot.x_tight() << " tightness." << endl; // 0.001
+
+ /*`Purely to show the possibilities with autoscale, we don't use the whole container,
+ but use the two-iterators version of autoscale to *not use the first and last values* for autoscaling.
+ (Remember values in set are sorted).
+ */
+ my_1d_plot.x_autoscale(++my_set.begin(),--my_set.end());
+ /*`This also sets autoscale(true), but note that x_range() is still not updated.
+ If we want, we can display the ranges chosen by autoscale:
+ */
+ cout << " x_auto_min_value " << my_1d_plot.x_auto_min_value()
+ << ", x_auto_max _value " << my_1d_plot.x_auto_max_value()
+ << ", x_auto_tick_interval " << my_1d_plot.x_auto_tick_interval() << endl;
+
+ /*`As before, we add the data set to the plot, and write to SVG XML to file.
+ If you inspect the plot, you will see that the
+ lowest data value 1.2 and highest data value 8.9 are no longer shown,
+ because it is now outside the plot window.
+ This is as if we had only written part of the data series.
+ */
+ my_1d_plot.plot(++my_set.begin(),--my_set.end(), "Auto 1D my_set"); // Add whole data series from my_set.
+ //my_1d_plot.plot(my_set, "Auto 1D my_set"); // Add whole data series from my_set.
+ my_1d_plot.write("demo_1d_autoscaling_s.svg"); // Write the plot to file.
+
+ /*`If we want, we can check the autoscale range used, noting that zero *is* included because we demanded it.*/
+ cout << "x_range() " << my_1d_plot.x_range() << endl; // x_range() 0, 8
+ //show_1d_plot_settings(my_1d_plot); // If required.
+ }
+ catch(const std::exception& e)
+ {
+ /*` Try & catch blocks are very useful to display any plot error messages.
+ Otherwise any exceptions thrown will just terminate - silently and unhelpfully.
+ */
+ cout <<"\n""Message from thrown exception was:\n " << e.what() << endl;
+ }
+ } // // Autoscaling with my_set
+
+ //] [/demo_1d_autoscaling_4]
+
+ return 0;
+} // int main()
+
+/*
+
+//[demo_1d_autoscaling_output
+
+demo_1d_autoscaling.cpp
+Linking...
+Embedding manifest...
+Autorun "j:\Cpp\SVG\debug\demo_1d_autoscaling.exe"
+6 values in container: 0.2 1.1 4.2 3.3 5.4 6.5
+8 values in container: 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9
+x_with_zero, 10 x_min_ticks, 0 x_steps, 0.01 tightness.
+ x_auto_min_value 0, x_auto_max _value 8, x_auto_tick_interval 0.5
+x_range() 0, 8
+Build Time 0:04
+
+//] [/demo_1d_autoscaling_output]
+
+*/
+
Added: sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_axis_scaling.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_axis_scaling.cpp 2008-05-24 12:27:14 EDT (Sat, 24 May 2008)
@@ -0,0 +1,296 @@
+// demo_1d_axis_scaling.cpp
+
+// Copyright Jacob Voytko 2007
+// Copyright Paul A Bristow 2008
+
+// Use, modification and distribution are subject to the
+// Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// An example to demonstrate use of *axis-scaling*.
+// See auto_1d_containers.cpp for an example axis_scaling with multiple data series.
+
+// This file is written to be included from a Quickbook .qbk document.
+// It can be compiled by the C++ compiler, and run. Any output can
+// also be added here as comment or included or pasted in elsewhere.
+
+// Caution: this file contains Quickbook markup as well as code
+// and comments: don't change any of the special comment markups!
+
+//[demo_1d_axis_scaling_1
+
+/*`This example shows the use of functions scale_axis to find suitable axis limits.
+Normally one would use autoscaling, but there are conceivable circumstances when
+one would want to check on the algorithms choice of axis
+and perhaps intervene in the process.
+
+First some includes to use Boost.Plot
+(and some others only needed for this example).
+*/
+
+#include <boost/svg_plot/svg_1d_plot.hpp>
+ using namespace boost::svg;
+ using boost::svg::svg_1d_plot;
+
+#include <boost/svg_plot/show_1d_settings.hpp>
+// Only needed for showing which settings in use.
+ void boost::svg::show_1d_plot_settings(svg_1d_plot&);
+ // (Also provides operator<< for std::pair).
+
+#include <boost/algorithm/minmax.hpp>
+ using boost::minmax;
+#include <boost/algorithm/minmax_element.hpp>
+ using boost::minmax_element;
+
+#include <iostream> // for debugging.
+ using std::cout;
+ using std::endl;
+ using std::boolalpha;
+
+#include <limits>
+ using std::numeric_limits;
+
+#include <vector>
+ using std::vector;
+#include <set>
+ using std::multiset;
+
+#include <utility>
+ using std::pair;
+
+#include <boost/svg_plot/detail/auto_axes.hpp>
+ using boost::svg::show; // A single STL container.
+ using boost::svg::show_all; // Multiple STL containers.
+ using boost::svg::range; // Find min and max of a STL container.
+ using boost::svg::range_all;// Find min and max of multipler STL containers.
+
+//] [/demo_1d_axis_scaling_1]
+
+void scale_axis(double min_value, double max_value, // Input range
+ double* axis_min_value, double* axis_max_value, double* axis_tick_increment, int* major_ticks, // All 4 updated.
+ bool origin, // If true, ensures that zero is a tick value.
+ double tight, // Allows user to avoid a small fraction over a tick using another tick.
+ int min_ticks, // Minimum number of ticks.
+ int steps); // Round up and down to 2, 4, 6, 8, 10, or 5, 10 or 2, 5, 10 systems.
+
+double tol100eps = 1000 * numeric_limits<double>::epsilon(); // suitable tight value.
+
+int main()
+{
+ //[demo_1d_axis_scaling_2
+ /*`This example uses a few types of containers to demonstrate axis_scaling.
+ axis_scaling must inspect the container in order to find axis ranges that will be suitable.
+ First we create a container and fill with some fictional data.
+ */
+ vector<double> my_data;
+ // Initialize my_data with some entirely fictional data.
+ my_data.push_back(0.2);
+ my_data.push_back(1.1); // [1]
+ my_data.push_back(4.2); // [2]
+ my_data.push_back(3.3); // [3]
+ my_data.push_back(5.4); // [4]
+ my_data.push_back(6.5); // [5]
+ show(my_data); // Show entire container contents,
+ // 6 values in container: 0.2 1.1 4.2 3.3 5.4 6.5
+
+//] [/demo_1d_axis_scaling_2]
+
+//[demo_1d_axis_scaling_3
+
+ multiset<double> my_set;
+ // Initialize my_set with some entirely fictional data.
+ my_set.insert(1.2);
+ my_set.insert(2.3);
+ my_set.insert(3.4);
+ my_set.insert(4.5);
+ my_set.insert(5.6);
+ my_set.insert(6.7);
+ my_set.insert(7.8);
+ my_set.insert(8.9);
+ // Show the set.
+ multiset<double>::const_iterator si;
+ show(my_set); // for two different types of container.
+ // 8 values in container: 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9
+
+/*` Show can also display just a part of the container contents.
+*/
+ // show(&my_data[0], &my_data[my_data.size()]); // pointers - wrong! > all data ;-)
+ show(&my_data[0], &my_data[my_data.size()-1]); // pointers, all data.
+ show(&my_data[1], &my_data[5]); // pointers, part data.
+ show(my_data.begin(), my_data.end()); // iterators.
+ show(++(my_data.begin()), --(my_data.end())); // Just the 4 middle values.
+
+ vector<double>::const_iterator idb = my_data.begin();
+ vector<double>::const_iterator ide = my_data.end();
+ show(idb, ide); // All
+ ++idb; // Move to 2nd value.
+ --ide; // move back from last value.
+ show(idb, ide); // Just the 4 middle values.
+//] [/demo_1d_axis_scaling_3]
+
+//[demo_1d_axis_scaling_4
+
+ /*`Is is possible to find the minimum and maximum values in a container using the STL functions min & max
+ or more conveiniently and efficiently with boost::minmax_element:
+ */
+
+ typedef vector<double>::const_iterator vector_iterator;
+ pair<vector_iterator, vector_iterator> result = boost::minmax_element(my_data.begin(), my_data.end());
+ cout << "The smallest element is " << *(result.first) << endl; // 0.2
+ cout << "The largest element is " << *(result.second) << endl; // 6.5
+
+ // axis_scaling using two double min and max values.
+ double min_value = *(my_data.begin());
+ double max_value = *(--my_data.end());
+ cout << "axis_scaling min " << min_value << ", max = " << max_value << endl;
+
+/*` and to apply these values to the axis_scaling algorithm using by plot to choose the axes limits and ticks.
+*/
+ double axis_min_value; // Values to be updated by function scale_axis.
+ double axis_max_value;
+ double axis_tick_increment;
+ int axis_ticks;
+
+ scale_axis(min_value, max_value,
+ &axis_min_value, &axis_max_value, &axis_tick_increment, &axis_ticks,
+ false, tol100eps, 6); // Display range.
+ cout << "Axis_scaled min " << axis_min_value << ", max = " << axis_max_value << ", increment " << axis_tick_increment << endl;
+/*`It is also possible to use this with containers that use iterators and whose contents are ordered in ascending value,
+axis_scaling using first and last in container, for example, set, map, multimap, or a sorted vector or array.
+A number of variations are shown below, mianly by way of testing.
+*/
+ scale_axis(*my_data.begin(),*(--my_data.end()),
+ &axis_min_value, &axis_max_value, &axis_tick_increment, &axis_ticks,
+ false, tol100eps, 6); // Display range.
+ cout << "Axis_scaled min " << axis_min_value << ", max = " << axis_max_value << ", increment " << axis_tick_increment << endl;
+
+ // axis_scaling using two begin & end iterators into STL container,
+ // scale_axis does finding min and max.
+ scale_axis(my_data.begin(), my_data.end(),
+ &axis_min_value, &axis_max_value, &axis_tick_increment, &axis_ticks,
+ false, tol100eps, 6); // Display range.
+ cout << "Axis_scaled min " << axis_min_value << ", max = " << axis_max_value << ", increment " << axis_tick_increment << endl;
+
+ // axis_scaling using two begin & end iterators into STL container,
+ // scale_axis does finding min and max.
+ scale_axis(my_data[1], my_data[4], // Only middle part of the container used, ignoring 1st and last values.
+ &axis_min_value, &axis_max_value, &axis_tick_increment, &axis_ticks,
+ false, tol100eps, 6); // Display range.
+ cout << "Axis_scaled min " << axis_min_value << ", max = " << axis_max_value << ", increment " << axis_tick_increment << endl;
+
+ // axis_scaling using whole STL container,
+ // scale_axis does finding min and max.
+ scale_axis(my_data, &axis_min_value, &axis_max_value, &axis_tick_increment, &axis_ticks,
+ false, tol100eps, 6); // Display range.
+ cout << "Axis_scaled min " << axis_min_value << ", max = " << axis_max_value << ", increment " << axis_tick_increment << endl;
+
+ svg_1d_plot my_1d_plot; // Construct a plot with all the default constructor values.
+
+ // One could intercept and change any values calculated by scale_axis here?
+ // Set the plot to use range and interval from the scale_axis values.
+
+ /*`The axis range thus computed can be inserted directly into the plot using the `range` and `x_major_interval` functions.
+ */
+
+ my_1d_plot.x_range(axis_min_value, axis_max_value)
+ .x_major_interval(axis_tick_increment);
+
+ //my_1d_plot.x_autoscale(false); // Ensure autoscale values are *not* recalculated for the plot.
+
+ //] [/demo_1d_axis_scaling_4]
+
+ //[demo_1d_axis_scaling_5
+
+ // Set some axis_scaling parameters:
+ my_1d_plot.x_with_zero(false);
+ my_1d_plot.x_min_ticks(10);
+ my_1d_plot.x_steps(0);
+ my_1d_plot.x_tight(0.001);
+
+ // Show the flags just set.
+ cout << (my_1d_plot.x_with_zero() ? "x_with_zero, " : "not x_with_zero, ")
+ << my_1d_plot.x_min_ticks() << " x_min_ticks, "
+ << my_1d_plot.x_steps() << " x_steps, "
+ << my_1d_plot.x_tight() << " tightness." << endl;
+
+ my_1d_plot.x_autoscale(my_data); // Use all my_data to autoscale.
+ cout << "Axis_scaled " // Show the results of autoscale:
+ "min " << my_1d_plot.x_auto_min_value()
+ << ", max "<< my_1d_plot.x_auto_max_value()
+ << ", interval " << my_1d_plot.x_auto_tick_interval() << endl; // Autoscaled min 0, max 6.5, interval 0.5
+
+ my_1d_plot.x_autoscale(my_data.begin(), my_data.end()); // Use all my_data to autoscale.
+
+ cout << "Axis_scaled " // Show the results of autoscale:
+ "min " << my_1d_plot.x_auto_min_value()
+ << ", max "<< my_1d_plot.x_auto_max_value()
+ << ", interval " << my_1d_plot.x_auto_tick_interval() << endl; // Autoscaled min 0, max 6.5, interval 0.5
+
+ my_1d_plot.x_autoscale(my_data[1], my_data[4]); // Use only part of my_data to autoscale.
+
+ cout << "Axis_scaled " // Show the results of autoscale:
+ "min " << my_1d_plot.x_auto_min_value()
+ << ", max "<< my_1d_plot.x_auto_max_value()
+ << ", interval " << my_1d_plot.x_auto_tick_interval() << endl; // Autoscaled min 1, max 5.5, interval 0.5
+
+ my_1d_plot.x_autoscale(true); // Ensure autoscale values are used for the plot.
+
+ my_1d_plot.plot(my_data, "Auto 1D"); // Add the one data series.
+ cout << "Axis_scaled " // Show the results of autoscale:
+ " min " << my_1d_plot.x_auto_min_value()
+ << ", max "<< my_1d_plot.x_auto_max_value()
+ << ", interval " << my_1d_plot.x_auto_tick_interval() << endl; // Autoscaled min 1, max 5.5, interval 0.5
+
+ my_1d_plot.plot(my_set.begin(), my_set.end(), "Auto 1D"); // Add another data series from my_set.
+ my_1d_plot.plot(my_set, "Auto 1D"); // Add another whole data series from my_set.
+ my_1d_plot.plot(&my_data[1], &my_data[4], "Auto 1D"); // Add part (1,2 3 but *not* 4) of the one data series.
+ //my_1d_plot.plot(&my_set[1], &my_set[4], "Auto 1D"); // operator[] is not defined for set container!
+
+ my_1d_plot.write("auto_1d_plot.svg"); // Write the plot to file.
+
+ using boost::svg::detail::operator<<;
+ cout << "x_range() " << my_1d_plot.x_range() << endl; // x_range() 1, 5.5
+
+ //show_1d_plot_settings(my_1d_plot);
+
+//] [/demo_1d_axis_scaling_5]
+
+ return 0;
+} // int main()
+
+/*
+
+//[demo_1d_axis_scaling_output
+
+Autorun "j:\Cpp\SVG\debug\demo_1d_axis_scaling.exe"
+6 values in container: 0.2 1.1 4.2 3.3 5.4 6.5
+8 values in container: 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9
+0.2 1.1 4.2 3.3 5.4 : 5 values used.
+1.1 4.2 3.3 5.4 : 4 values used.
+0.2 1.1 4.2 3.3 5.4 6.5 : 6 values used.
+1.1 4.2 3.3 5.4 : 4 values used.
+0.2 1.1 4.2 3.3 5.4 6.5 : 6 values used.
+1.1 4.2 3.3 5.4 : 4 values used.
+The smallest element is 0.2
+The largest element is 6.5
+axis_scaling min 0.2, max = 6.5
+Axis_scaled min 0, max = 7, increment 1
+Axis_scaled min 0, max = 7, increment 1
+Axis_scaled min 0, max = 7, increment 1
+Axis_scaled min 1, max = 6, increment 1
+Axis_scaled min 0, max = 7, increment 1
+not x_with_zero, 10 x_min_ticks, 0 x_steps, 0.001 tightness.
+Axis_scaled min 0, max 6.5, interval 0.5
+Axis_scaled min 0, max 6.5, interval 0.5
+Axis_scaled min 1, max 5.5, interval 0.5
+Axis_scaled min 1, max 5.5, interval 0.5
+x_range() 1, 5.5
+Build Time 0:03
+
+
+
+//] [/demo_1d_axis_scaling_output]
+
+*/
+
Added: sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_containers.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_containers.cpp 2008-05-24 12:27:14 EDT (Sat, 24 May 2008)
@@ -0,0 +1,157 @@
+// demo_1d_containers.cpp
+
+// Copyright Jacob Voytko 2007
+// Copyright Paul A Bristow 2008
+
+// Use, modification and distribution are subject to the
+// Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// An example to demonstrate simple 1D settings,
+// with a range of different STL containers.
+
+// This file is written to be included from a Quickbook .qbk document.
+// It can be compiled by the C++ compiler, and run. Any output can
+// also be added here as comment or included or pasted in elsewhere.
+
+// Caution: this file contains Quickbook markup as well as code
+// and comments: don't change any of the special comment markups!
+
+//[demo_1d_containers_1
+
+/*`First a few includes to use Boost.Plot and various STL containers:
+*/
+#include <boost/svg_plot/svg_1d_plot.hpp>
+ using namespace boost::svg;
+#include <boost/array.hpp>
+ using boost::array;
+#include <vector>
+ using std::vector;
+#include <set>
+ using std::set; // Automatically sorted - though this is not useful to the plot process.
+ using std::multiset; // At present using multiset, allowing duplicates, plot does not indicate duplicates.
+ // With 2_D data in multimap, duplicate values are usefully displayed.
+#include <list>
+ using std::list;
+#include <deque>
+ using std::deque;
+
+//] [/demo_1d_containers_1]
+
+int main()
+{
+ {
+//[demo_1d_containers_2
+
+/*`STL vector is used as the container,
+and fictional values are inserted using push_back.
+Since this is a 1-D plot the order of data values is not important.
+*/
+ vector<float> values;
+ values.push_back(3.1f);
+ values.push_back(-5.5f);
+ values.push_back(8.7f);
+ values.push_back(0.5f);
+
+/*`The constructor initializes a new 1D plot, called `my_plot`, and also sets all the many default values.
+*/
+ svg_1d_plot my_plot;
+
+/*`Setting (member) functions are fairly self-explanatory:
+Title provides a title at the top for the whole plot,
+and plot adds a (unamed) data series (naming isn't very useful if there is only one data series).
+*/
+ my_plot.title("vector<float> example");
+/*`
+[note One must insert the XML character entity equivalents of < for < and > for >).]
+*/
+ my_plot.plot(values);
+
+/*`Write the SVG to a file.
+*/
+ my_plot.write("./demo_1d_vector_float.svg");
+//] [/demo_1d_containers_2]
+ }
+
+ {
+//[demo_1d_containers_3
+/*`If the container is static array, then it must be filled by assignment:*/
+ array<long double, 4> values = {3.1L,-5.5L, 8.7L, 0.5L};
+
+ svg_1d_plot my_plot;
+ my_plot.title("array<long double> example");
+ my_plot.plot(values);
+ my_plot.write("./demo_1d_array_long_double.svg");
+//] [/demo_1d_containers_3]
+ }
+
+ {
+//[demo_1d_containers_4
+/*`If the container type is a set, then it can be filled with insert:*/
+ set<double> values;
+ values.insert(-8.4);
+ values.insert(-2.3);
+ values.insert(0.1);
+ values.insert(5.6);
+ values.insert(7.8);
+
+ svg_1d_plot my_plot;
+ my_plot.title("set<double> example");
+ my_plot.plot(values);
+ my_plot.write("./demo_1d_set_double.svg");
+//] [/demo_1d_containers_4]
+ }
+
+ {
+//[demo_1d_containers_5
+/*`If the container type is a list, then it can be filled with push_back or push_front:*/
+ list<double> values;
+ values.push_back(-8.4);
+ values.push_back(-2.3);
+ values.push_back(0.1);
+ values.push_back(5.6);
+ values.push_back(7.8);
+ svg_1d_plot my_plot;
+ my_plot.title("list<double> example");
+ my_plot.plot(values);
+ my_plot.write("./demo_1d_list_double.svg");
+//] [/demo_1d_containers_5]
+ }
+
+ {
+//[demo_1d_containers_6
+/*`If the container type is a deque, then it can be filled with push_back or push_front:*/
+ deque<double> values;
+ values.push_front(-8.4);
+ values.push_front(-2.3);
+ values.push_front(0.1);
+ values.push_front(5.6);
+ values.push_front(7.8);
+
+ svg_1d_plot my_plot;
+ my_plot.title("deque<double> example");
+ my_plot.plot(values);
+ my_plot.write("./demo_1d_deque_double.svg");
+//] [/demo_1d_containers_6]
+ }
+
+ return 0;
+} // int main()
+
+/*
+
+Output:
+//[demo_1d_containers_output
+
+Compiling...
+demo_1d_containers.cpp
+Linking...
+Embedding manifest...
+Autorun "j:\Cpp\SVG\debug\demo_1d_containers.exe"
+Build Time 0:03
+
+
+
+//] [/demo_1d_containers_output]
+*/
Added: sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_plot.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_plot.cpp 2008-05-24 12:27:14 EDT (Sat, 24 May 2008)
@@ -0,0 +1,159 @@
+// svg_1d_plot.hpp
+
+// Copyright Jacob Voytko 2007
+// Copyright Paul A Bristow 2007
+
+// Use, modification and distribution are subject to the
+// Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// An entirely contrived example designed to demonstration
+// as many features as possible in a single example.
+// The results is not intended to be useful,
+// nor is it intended to be pretty.
+// See other examples for more practical (and tasteful) examples using typical features.
+
+# pragma warning(disable : 4267) // '=' : conversion from 'size_t' to 'unsigned int'
+# pragma warning(disable : 4172) //
+// in spirit TODO remove when not using Boost.Parameter
+# pragma warning(disable : 4310) // cast truncates constant value
+# pragma warning(disable : 4512) // assignment operator could not be generated
+# pragma warning(disable : 4702) // unreachable code
+# pragma warning(disable : 4224) // nonstandard extension used : formal parameter 'function_ptr' was previously defined as a type
+# pragma warning(disable : 4800) // const char *' : forcing value to bool 'true' or 'false' (performance warning)
+
+#include <boost/svg_plot/svg.hpp>
+
+#include <boost/svg_plot/svg_1d_plot.hpp>
+ using namespace boost::svg;
+ using boost::svg::svg_1d_plot;
+
+#include <boost/svg_plot/svg_fwd.hpp> // forward declarations.
+// for testing its correctness.
+
+#include <iostream> // for debugging.
+ using std::cout;
+ using std::endl;
+
+#include <vector>
+ using std::vector;
+
+int main()
+{
+ vector<double> my_data;
+ // Initialize my_data here with some entirely fictional data.
+ my_data.push_back(0.1);
+ my_data.push_back(1.1);
+ my_data.push_back(4.2);
+ my_data.push_back(3.3);
+ my_data.push_back(5.4);
+ my_data.push_back(6.5);
+
+ vector<double> my_data2;
+ // Initialize my_data here with some more entirely fictional data.
+ my_data2.push_back(0.5);
+ my_data2.push_back(1.5);
+ my_data2.push_back(4.7);
+
+ vector<double> my_data3;
+ // Initialize my_data here with some more entirely fictional data.
+ my_data3.push_back(0.7);
+ my_data3.push_back(2.5);
+ my_data3.push_back(5.8);
+
+ vector<double> my_data4;
+ // Initialize my_data with some integral values so can check points are marked exactly right..
+ my_data4.push_back(1.);
+ my_data4.push_back(2.);
+ my_data4.push_back(3.);
+ my_data4.push_back(6.);
+ my_data4.push_back(7.);
+ my_data4.push_back(8.);
+
+ svg_1d_plot my_1d_plot; // Construct with all the default constructor values.
+ cout << "Image x & y " << my_1d_plot.image_x_size() << " to " << my_1d_plot.image_y_size() << endl;
+ //my_1d_plot.image_size(100,100); // Alter both together.
+ //cout << "Image x & y " << my_1d_plot.image_x_size() << " to " << my_1d_plot.image_y_size() << endl;
+ //// And alter both separately.
+ //my_1d_plot.image_x_size(200);
+ //my_1d_plot.image_y_size(600);
+ //cout << "Image x & y " << my_1d_plot.image_x_size() << " to " << my_1d_plot.image_y_size() << endl;
+
+ my_1d_plot.document_title("My Document title");
+ my_1d_plot.description("My Document description");
+ my_1d_plot.copyright_date("2007");
+ my_1d_plot.copyright_holder("Paul A. Bristow");
+ //my_1d_plot.license("permits", "permits", "permits", "permits");
+ //my_1d_plot.coord_precision(4);
+
+ cout << "font-family was " << my_1d_plot.title_font_family() << endl;
+
+ my_1d_plot
+
+ .title("Demo 1D plot <sup>-α </sup> Ω Φ √ ∞ ζ ±")
+ // domain of the random variable is [0, ∞]") // Capital Omega <superscript> Ω </superscript>") doesn't work yet.
+ .title_font_size(20)
+ .title_font_family("Times New Roman")
+ .legend_on(true)
+ .legend_title("My Legend μ") // generates <em>μ</em> greek mu
+ .legend_title_font_size(12)
+ //.legend_title("") // generates <em>μ</em> greek mu
+ //.plot_area(true) // TODO in docs..
+ .background_color(ghostwhite) // whole image
+ .plot_window_on(true)
+ .image_y_size(250)
+ .plot_background_color(aliceblue) // just the plot area
+ .x_label_on(true) // show x-axis text label.
+ .x_label("volume") // Care: this doesn't show unless .x_label_on() == true!
+ .x_axis_color(green)
+ .x_label_color(blue)
+ .x_label_font_family("verdana")
+// .x_label_font_family("Lucida sans console")
+// .x_label_font_family("Times New Roman")
+ .x_label_units_on(true)
+ .x_label_font_size(12)
+ .x_label_units("meter³ or m³") // super 2 = ² super 3 = ³
+ // Care: this doesn't show unless .x_label_units_on() == true!
+ .x_ticks_up_on(false)
+ .x_ticks_down_on(true) // So only have downward ticks.
+ .x_major_grid_on(true)
+ .x_major_grid_width(2)
+ .x_major_grid_color(green)
+ .x_minor_grid_on(true)
+ .x_minor_grid_width(1)
+ .x_minor_grid_color(red)
+ .x_range(0., 7.);
+
+ cout << "font-family is " << my_1d_plot.title_font_family() << endl;
+
+ my_1d_plot.plot(my_data, "my values 1", _stroke_color = red, _fill_color = blue, _point_style = vertical_tick, _size = 12);
+ // Produces a blue vertical bar!
+
+ my_1d_plot.plot(my_data4, "values symbol", red, _point_style = symbol, _size = 14);
+ // Hardcoded X only TODO other when Boost.Parameter goes have way to set symbol(s)
+ // U+2721 is Star of David or hexagram http://en.wikipedia.org/wiki/Hexagram
+ my_1d_plot.plot(my_data2, "my values round", red, _point_style = round, _size = 10);
+ my_1d_plot.plot(my_data3, "my values 3", red, _point_style = diamond, _size = 14);
+ my_1d_plot.plot(my_data, "my values spade", blue, _point_style = spade, _size = 14);
+ my_1d_plot.plot(my_data2, "values asterisk", green, _point_style = asterisk, _size = 14);
+ my_1d_plot.plot(my_data, "my cone", red, _point_style = cone, _size = 14);
+
+ my_1d_plot.write("demo_1d_plot.svg");
+
+ cout << "Plot window x " << my_1d_plot.plot_window_x().first << " to " << my_1d_plot.plot_window_x().second
+ << ", y " << my_1d_plot.plot_window_y().first << " to " << my_1d_plot.plot_window_y().second << endl;
+ return 0;
+} // int main()
+
+/*
+
+Compiling...
+demo_1d_plot.cpp
+Linking...
+Embedding manifest...
+Autorun "j:\Cpp\SVG\debug\demo_1d_plot.exe"
+Build Time 0:11
+
+*/
+
Added: sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_values.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_values.cpp 2008-05-24 12:27:14 EDT (Sat, 24 May 2008)
@@ -0,0 +1,161 @@
+// demo_1d_values.hpp
+
+// Copyright Jacob Voytko 2007
+// Copyright Paul A Bristow 2008
+
+// Use, modification and distribution are subject to the
+// Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// An example to demonstrate simplest 1D *default* settings.
+// See also demo_1d_plot.cpp for a wider range of use.
+
+// This file is written to be included from a Quickbook .qbk document.
+// It can be compiled by the C++ compiler, and run. Any output can
+// also be added here as comment or included or pasted in elsewhere.
+
+// Caution: this file contains Quickbook markup as well as code
+// and comments: don't change any of the special comment markups!
+
+//[demo_1d_values_1
+
+/*`As ever, we need a few includes to use Boost.Plot
+*/
+
+#include <boost/svg_plot/svg_1d_plot.hpp>
+ using namespace boost::svg;
+ using boost::svg::svg_1d_plot;
+
+#include <iostream>
+ using std::cout;
+ using std::endl;
+ using std::hex;
+
+#include <vector>
+ using std::vector;
+
+//] [demo_1d_values_1]
+
+int main()
+{
+
+//[demo_1d_values_2
+/*`Some fictional data is pushed into an STL container, here vector<double>:*/
+ vector<double> my_data;
+ my_data.push_back(-1.6);
+ my_data.push_back(2.0);
+ my_data.push_back(4.2563);
+ my_data.push_back(0.00333974);
+ my_data.push_back(5.4);
+ my_data.push_back(6.556);
+
+ try
+ { // try'n'catch clocks are needed to ensure error messages from any exceptions are shown.
+ svg_1d_plot my_1d_plot; // Construct a plot with all the default constructor values.
+
+ my_1d_plot.title("Default 1D Values Demo") // Add a string title of the plot.
+ .x_range(-5, 10) // Add a range for the X-axis.
+ .x_label("length (m)"); // Add a label for the X-axis.
+
+/*`Add the one data series, `my_data` and a description, and how the data points are to marked,
+here a circle with a diameter of 5 pixels.
+*/
+ my_1d_plot.plot(my_data, "1D Values").shape(round).size(5);
+
+/*`To put a value label against each data point, switch on the option:
+*/
+ my_1d_plot.x_values_on(true); // Add a label for the X-axis.
+
+/*`If the default size and color are not to your taste, set more options, like:
+*/
+ my_1d_plot.x_values_font_size(14) // Change font size for the X-axis value labels.
+ .x_values_font_family("Times New Roman") // Change font for the X-axis value labels.
+ .x_values_color(red); // Change color from default black to red.
+
+/*`The format of the values may also not be ideal,
+so we can use the normal `iostream precision` and `ioflags` to change,
+here to reduce the number of digits used from default precision 6 down to a more readable 2,
+reducing the risk of collisions between adjacent values.
+(Obviously the most suitable precision depends on the range of the data points.
+If values are very close to each other, a higher precision wil be needed to differentiate them).
+*/
+ my_1d_plot.x_values_precision(2); // precision label for the X-axis value label.
+
+/*`We can also prescribe the use of scientific format and force a positive sign:
+*/
+ my_1d_plot.x_values_ioflags(std::ios::scientific | std::ios::showpos);
+
+/*`By default, any unnecessary spacing-wasting zeros in the exponent field are removed.
+(If, perversely, the full 1.123456e+012 format is required, the stripping can be switched off with:
+ `my_1d_plot.x_labels_strip_e0s(false);` )
+
+In general, sticking to the defaults usually produces the neatest presentation of the values.
+*/
+
+/*`The default value label is horizontal, centered above the data point marker,
+but, depending on the type and density of data points, and the length of the values
+(controlled in turn by the `precision` and `ioflags` in use),
+it is often clearer to use a different orientation.
+This can be controlled in steps of 45 degrees, using an 'enum rotate_style`.
+
+* `uphill` - writing up at 45 degree slope is often a good choice,
+* `upward` - writing vertically up and
+* `backup` are also useful.
+
+(For 1-D plots other directions are less attractive,
+placing the values below the horizontal Y-axis line,
+but for 2-D plots all writing orientations can be useful).
+*/
+ my_1d_plot.x_values_rotation(upward); // Orientation for the X-axis value labels.
+
+/*`To use all these settings, finally write the plot to file.
+*/
+ my_1d_plot.write("demo_1d_values.svg");
+
+/*`If chosen settings do not have the expected effect, is may be helpful to display them.
+
+(All settings can be displayed with `show_1d_plot_settings(my_1d_plot)`.)
+*/
+ cout << "my_1d_plot.x_values_font_size() " << my_1d_plot.x_values_font_size() << endl;
+ cout << "my_1d_plot.x_values_font_family() " << my_1d_plot.x_values_font_family() << endl;
+ cout << "my_1d_plot.x_values_color() " << my_1d_plot.x_values_color() << endl;
+ cout << "my_1d_plot.x_values_precision() " << my_1d_plot.x_values_precision() << endl;
+ cout << "my_1d_plot.x_values_ioflags() " << hex << my_1d_plot.x_values_ioflags() << endl;
+//] [demo_1d_values_2]
+ }
+ catch(const std::exception& e)
+ {
+ std::cout <<
+ "\n""Message from thrown exception was:\n " << e.what() << std::endl;
+ }
+ return 0;
+} // int main()
+
+/*
+
+//[demo_1d_values_output
+
+Output:
+
+demo_1d_values.cpp
+Linking...
+Embedding manifest...
+Autorun "j:\Cpp\SVG\debug\demo_1d_values.exe"
+ my_1d_plot.x_values_font_size() 14
+my_1d_plot.x_values_font_family() Times New Roman
+my_1d_plot.x_values_color() RGB(255,0,0)
+my_1d_plot.x_values_precision() 2
+ my_1d_plot.x_values_ioflags() 1020
+Build Time 0:03
+
+
+//] [demo_1d_values_output]
+
+ //my_1d_plot.x_values_rotation(uphill); // Orientation for the X-axis value labels.
+ //my_1d_plot.x_values_rotation(horizontal); // Orientation for the X-axis value labels.
+ //my_1d_plot.x_values_rotation(backup); // Orientation for the X-axis value labels.
+ my_1d_plot.x_values_rotation(downhill); // Orientation for the X-axis value labels.
+
+*/
+
Added: sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_vector.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_1d_vector.cpp 2008-05-24 12:27:14 EDT (Sat, 24 May 2008)
@@ -0,0 +1,105 @@
+// demo_1d_vector.cpp
+
+// Copyright Jacob Voytko 2007
+// Copyright Paul A Bristow 2008
+
+// Use, modification and distribution are subject to the
+// Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// An example to demonstrate simple 1D plot using two vectors,
+// see also demo_1d_containers for examples using other STL containers.
+
+// This file is written to be included from a Quickbook .qbk document.
+// It can be compiled by the C++ compiler, and run. Any output can
+// also be added here as comment or included or pasted in elsewhere.
+
+// Caution: this file contains Quickbook markup as well as code
+// and comments: don't change any of the special comment markups!
+
+//[demo_1d_vector_1
+
+/*`First we need a few includes to use Boost.Plot:
+*/
+#include <boost/svg_plot/svg_1d_plot.hpp>
+ using namespace boost::svg;
+#include <vector>
+ using std::vector;
+//] [/demo_1d_vector_1]
+
+int main()
+{
+//[demo_1d_vector_2
+
+/*`STL vector is used as the container for our two data series,
+and values are inserted using push_back. Since this is a 1-D plot
+the order of data values is not important.
+*/
+ vector<double> dan_times;
+ dan_times.push_back(3.1);
+ dan_times.push_back(4.2);
+
+ vector<double> elaine_times;
+ elaine_times.push_back(2.1);
+ elaine_times.push_back(7.8);
+
+/*`The constructor initializes a new 1D plot, called `my_plot`, and also sets all the many default values.
+*/
+ svg_1d_plot my_plot;
+
+/*`A few setting (member) functions are fairly self-explanatory:
+
+* title provides a title at the top for the whole plot,
+* `legend_on(true)` will mean that data series titles and markers will display in the legend box.
+* `x_range(-1, 11)` sets the axis limits from -1 to +11 (instead of the default -10 to +10).
+* `background_border_color(blue)` sets just one of the very many options.
+
+*/
+
+ my_plot.background_border_color(blue)
+ .legend_on(true)
+ .title("Race Times")
+ .x_range(-1, 11);
+
+/*`The syntax `my_plot.title("Hello").legend_on(true)...` may appear unfamiliar,
+but is a convenient way of specifying many parameters in any order. It is equivalent to:
+``
+ my_plot.title("Race Times");
+ my_plot.legend_on(true);
+ my_plot.x_range(-1, 11);
+``
+Chaining thus allows you to avoid repeatedly typing "`myplot.`"
+and easily group related settings like plot window, axes ... together.
+A fixed order would clearly become impracticable with
+hundreds of possible arguments needed to set all the myriad plot options.
+
+Then we need to add data series, and add optional data series titles if we want them to show on the legend.
+*/
+
+ my_plot.plot(dan_times, "Dan").stroke_color(red).fill_color(red);
+ my_plot.plot(elaine_times, "Elaine").stroke_color(blue).fill_color(blue);
+
+/*`Finally, we can write the SVG to a file of our choice.
+*/
+
+ my_plot.write("./demo_1d_vector.svg");
+//] [/demo_1d_vector_2]
+
+ return 0;
+} // int main()
+
+/*
+
+Output:
+//[demo_1d_vector_output
+
+Compiling...
+demo_1d_vector.cpp
+Linking...
+Embedding manifest...
+Autorun "j:\Cpp\SVG\debug\demo_1d_vector.exe"
+Build Time 0:04
+
+//] [/demo_1d_vector_output]
+*/
Added: sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_2d_bars.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_2d_bars.cpp 2008-05-24 12:27:14 EDT (Sat, 24 May 2008)
@@ -0,0 +1,58 @@
+// demo 2d_bar.cpp
+
+// Copyright Paul A. Bristow 2008
+// Distributed under the Boost Software License, Version 1.0.
+// For more information, see http://www.boost.org
+// -----------------------------------------------------------------
+
+// Demonstrate the bars (& columns or sticks).
+
+#include <boost/svg_plot/svg_2d_plot.hpp>
+#include <map>
+
+using std::map;
+using namespace boost::svg;
+
+int main()
+{
+ map<double, double> map1; // Two sample data series.
+ map<double, double> map2;
+
+ // This is random data used purely for example purposes.
+ map1[1.] = 3.2; // map[x] = y
+ map1[2.] = 5.4;
+ map1[7.3] = 9.1;
+ map1[8.] = 7.3;
+ map1[5.5] = 5.4;
+ map1[9.3] = 9.9;
+
+ map2[-3.1] = -6.1;
+ map2[5.4] = 4.7;
+
+ svg_2d_plot my_plot1;
+
+ my_plot1.title("X Times")
+ .legend_on(true)
+ .x_range(-1, 11)
+ .background_border_color(black);
+
+ my_plot1.plot(map1, "Series 1").fill_color(blue).shape(none).line_on(false).bar(x_stick).bar_width(5).bar_color(red);
+ my_plot1.write("./demo_2d_bar_x_stick.svg");
+ my_plot1.plot(map1, "Series 1").shape(none).line_on(false).bar(x_block).bar_width(10);
+ my_plot1.write("./demo_2d_bar_x_block.svg");
+
+ svg_2d_plot my_plot2;
+
+ my_plot2.title("Y Times")
+ .legend_on(true)
+ .x_range(-10, 10)
+ .background_border_color(blue);
+
+ my_plot2.plot(map2, "Series 2").fill_color(green).shape(none).line_on(false).bar(y_stick).bar_width(5).bar_color(cyan);
+ my_plot2.write("./demo_2d_bar_y_stick.svg");
+ my_plot2.plot(map2, "Series 2").shape(none).line_on(false).bar(y_block).bar_width(10).bar_color(green).bar_area_fill(magenta);
+ my_plot2.write("./demo_2d_bar_y_block.svg");
+
+ return 0;
+} // int main()
+
Added: sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_2d_histogram.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/visualization/libs/svg_plot/example/demo_2d_histogram.cpp 2008-05-24 12:27:14 EDT (Sat, 24 May 2008)
@@ -0,0 +1,163 @@
+// demo 2d_histogram.cpp
+
+// Copyright Paul A. Bristow 2008
+// Distributed under the Boost Software License, Version 1.0.
+// For more information, see http://www.boost.org
+// -----------------------------------------------------------------
+
+// Demonstrate drawing histogram as SVG files.
+// The *area* under each bin shows the number of cases or value in that bin or class.
+// (For bar charts the *height shows the value*).
+// Bins can be the same (most common) or different widths.
+// Intervals must not overlap and bins must be adjacent.
+// Various suggestions for choosing good bins have been made,
+// some a common bin width. The example below has different bin widths
+// reflecting very roughly the log of the journey time.
+
+#include <iostream>
+ using std::cout; using std::endl;
+#include <iomanip>
+ using std::setw;
+
+#include <boost/svg_plot/svg_2d_plot.hpp>
+using namespace boost::svg;
+
+#include <map>
+ using std::map;
+
+int main()
+{
+ map<double, double> times; // Example data series.
+ // http://en.wikipedia.org/wiki/Histogram
+ // Histogram of travel time (min), US 2000 census.
+
+ // Interval(x) Quantity (y) Width Quantity/width bin
+ times[0] = 4180.; // 5 836 0 to 5 min
+ times[5] = 13687.; // 5 2737 5 to 10 min
+ times[10] = 18618.; // 5 3723 10 to 15 min
+ times[15] = 19634.; // 5 3926 15 to 20 min
+ times[20] = 17981.; // 5 3596 20 to 25 min
+ times[25] = 7190.; // 5 1438 25 to 30 min
+ times[30] = 16369.; // 5 3273 30 to 35 min
+ times[35] = 3212.; // 5 642 35 to 40 min
+ times[40] = 4122.; // 5 824 40 to 45 min
+ times[45] = 9200.; // 5 613 45 to 60 min
+ times[60] = 6461.; // 5 215 60 to 90 min
+ times[90] = 3435.; // 5 57 90 to 150 min
+ times[150] = 0.; // Final bin end (15) with zero value.
+
+ map<double, double> time2s; // Guess (wild!) at 2050 data series.
+ time2s[0] = 2000.; // 5 836 0 to 5 min
+ time2s[5] = 5000.; // 5 2737 5 to 10 min
+ time2s[10] = 10000.; // 5 3723 10 to 15 min
+ time2s[15] = 11000.; // 5 3926 15 to 20 min
+ time2s[20] = 12000.; // 5 3596 20 to 25 min
+ time2s[25] = 13000.; // 5 1438 25 to 30 min
+ time2s[30] = 15000.; // 5 3273 30 to 35 min
+ time2s[35] = 16000.; // 5 642 35 to 40 min
+ time2s[40] = 18000.; // 5 824 40 to 45 min
+ time2s[45] = 12000.; // 5 613 45 to 60 min
+ time2s[60] = 11000.; // 5 215 60 to 90 min
+ time2s[90] = 10000.; // 5 57 90 to 150 min
+ time2s[150] = 0.; // Final bin end (15) with zero value.
+
+
+ // Check that the last element contains the expected last bin end.
+ std::map<double, double>::const_iterator last = times.end();
+ last--;
+ BOOST_ASSERT(last->first == 150); // expected last bin end,
+ BOOST_ASSERT(last->second == 0); // and that the value element is zero.
+
+ if (last->second != 0)
+ {
+ cout << "Last bin end " << last->first << " should have zero value! but is " << last->second << endl;
+ // Plot Implementation might advise, ignore or throw.
+ }
+
+ cout << "List all input data:" << endl;
+ cout << "Time Number of people" << endl;
+ for(std::map<double, double>::const_iterator it = times.begin();
+ it != times.end(); ++it)
+ {
+ cout << it->first << setw(6) << it->second << endl;
+ } // for it
+ cout << endl;
+
+ // Calculate and list the bins.
+ // (Not needed for the plot).
+ map<double, double> bins; //
+ cout << "Interval width Quantity quantity/width" << endl;
+ for(std::map<double, double>::const_iterator it = times.begin();
+ it != times.end(); ++it)
+ {
+ if (it == last)
+ {
+ break;
+ }
+
+ it++;
+ double next = it->first;
+ it--;
+ double width = (next - it->first); // width of bin
+ bins[it->first] = it->second/width; // quantity / width of bin
+ cout << setw(3) << next << setw(8) << setw(8) << width << it->second << setw(10) << it->second / width << endl;
+ } // for it
+ cout << endl;
+
+ cout << "Bin_start height" << endl;
+ for(std::map<double, double>::const_iterator it = bins.begin();
+ it != bins.end(); ++it)
+ {
+ cout << it->first << ' ' << it->second << endl;
+ } // for it
+ cout << endl;
+ cout << bins.size() << " bins" << endl;
+
+ cout << svg_color(red) << endl;
+ svg_color b(blank);
+ cout << b << endl;
+ b.write(cout);
+
+
+ // Plot the histogram.
+ svg_2d_plot my_plot1;
+
+ my_plot1.title("US 2000 Census Journey Times")
+ .legend_on(true) // Not very useful for a one series histogram.
+ .background_border_color(black)
+ .y_range(0, 5000.)
+ .y_label("thousands of people per one minute interval")
+ .x_range(0, 160.)
+ .x_label("journey time")
+ .x_label_units(" (min)")
+ .x_label_units_on(true)
+
+ // Axis settings.
+ .x_major_interval(50.)
+ .x_num_minor_ticks(4) // plus 1 major = 5 ticks per major step.
+ .y_major_interval(1000.)
+ .y_value_precision(4) // Without this, get y value labels in 2e3 format!
+ .y_num_minor_ticks(4) // plus 1 major = 5 ticks per major step.
+ ;
+
+ //my_plot1.plot(times, "Series 1").shape(none).line_on(false).histogram(column); // Need to supress line and shape plot options.
+ // TODO change shape and line to off automatically when histogram is chosen?
+ //my_plot1.plot(times, "Journey time").shape(none).line_on(false).line_color(red).area_fill(pink). histogram(column);
+ //my_plot1.plot(time2s, "2050 times? ").shape(none).line_on(false).line_color(blue).area_fill(lightblue).histogram(column);
+ // Overlay hides plot details underneath.
+ //my_plot1.write("./demo_2d_histogram_x.svg");
+
+ my_plot1.plot(time2s, "2050 times? ").shape(none).line_on(false).line_color(blue).area_fill(blank).histogram(column);
+ my_plot1.write("./demo_2d_histogram_x2.svg");
+
+
+ return 0;
+} // int main()
+
+
+/*
+
+Output:
+
+*/
+
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk