contrib/brl/bbas/bxml/bsvg/bsvg_plot.h
Go to the documentation of this file.
00001 #ifndef bsvg_plot_h_
00002 #define bsvg_plot_h_
00003 //:
00004 // \file
00005 // \brief Various plots - as SVG documents
00006 //
00007 // \author Ozge C. Ozcanli (Brown)
00008 // \date   April 21, 2009
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   Ozge C. Ozcanli - July 08, 09 - ported to vxl from local repository - minor fixes
00013 // \endverbatim
00014 
00015 #include "bsvg_document.h"
00016 #include "bsvg_element.h"
00017 
00018 //: Coordinate system of SVG is the same as an image coordinate system: upper left corner of the browser page is the origin
00019 //  Plot axes will be placed so that lower_left corner of the browser page will be the plot origin
00020 //  Plot only draws lines of x and y axes if they are added to the plot.
00021 //  scales the x and y axes according to x_min, x_max values so that it spans the document area
00022 
00023 const float default_stroke_width = 2.0f;
00024 const int default_font_size = 15;
00025 const float default_margin = 20.0f;
00026 
00027 class bsvg_plot : public bsvg_document
00028 {
00029  public:
00030 
00031     bsvg_plot(float w, float h) : bsvg_document(w, h), margin_(default_margin), font_size_(default_font_size) {}
00032     bsvg_plot(float w, float h, float viewBox_x, float viewBox_y, float viewBox_w, float viewBox_h) : bsvg_document(w,h,viewBox_x, viewBox_y, viewBox_w, viewBox_h), margin_(default_margin), font_size_(default_font_size) {}
00033 
00034     void add_axes(float x_min, float x_max, float y_min, float y_max, float stroke_width = default_stroke_width);
00035     //: assumes add_axes have been called
00036     void add_x_increments(float x_inc, float stroke_width = default_stroke_width);
00037     void add_y_increments(float y_inc, float stroke_width = default_stroke_width);
00038 
00039     void set_margin(float m) { margin_ = m; }
00040     void set_font_size(int s) { font_size_ = s; }
00041     void add_title(const vcl_string& t);
00042 
00043     void add_line(const vcl_vector<float>& xs, const vcl_vector<float>& ys, const vcl_string& color, float stroke_width = default_stroke_width);
00044 
00045     //: add equally spaced and equal width bars with the given heights
00046     void add_bars(const vcl_vector<float>& heights, const vcl_string& color);
00047     void add_bars(const vcl_vector<float>& heights, const vcl_vector<float>& x_labels, bool vertical_labels, const vcl_string& color);
00048     void add_bars(const vcl_vector<float>& heights, const vcl_vector<vcl_string>& x_labels, bool vertical_labels, const vcl_string& color);
00049 
00050     //:return the number of bars in the plot (counts the number of element with name "rect")
00051     int number_of_bars();
00052 
00053     //: add bars sequentially with a fixed interval and width.
00054     //  use margin_ as the width of each bar and leave margin_/3 intervals in between
00055     //  the total width of the plot needs to be adjusted during initialization to contain all desired number of bars
00056     int add_bar(const float height, const vcl_string& color);
00057     int add_bar(const float height, const float x_label, bool vertical_label, const vcl_string& color);
00058     int add_bar(const float height, const vcl_string& label, bool vertical_label, const vcl_string& color);
00059 
00060     bsvg_group* add_bars_helper(const vcl_vector<float>& heights, const vcl_string& color);
00061     bsvg_group* add_x_labels_helper(const vcl_vector<vcl_string>& x_labels, const vcl_string& color, bool vertical_labels);
00062 
00063     //: add splices for a pie chart 
00064     //  angle is zero at the x-axis and is positive counter-clockwise, use angles in range [0,2*pi]
00065     void add_splice(float center_x, float center_y, float radius, float start_angle, float end_angle, const vcl_string& color);
00066     //  angle is zero at the x-axis and is positive counter-clockwise, use angles in range [0,2*pi], colors red, green, blue are each in range [0,255]
00067     void add_splice(float center_x, float center_y, float radius, float start_angle, float end_angle, unsigned red, unsigned green, unsigned blue);
00068     
00069  protected:
00070 
00071     float margin_;
00072     int font_size_;
00073 
00074     float scale_factor_;
00075     float axes_orig_x_;
00076     float axes_orig_y_;
00077     float h2_x, h2_y;
00078 };
00079 
00080 #endif // bsvg_plot_h_