"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/netviz/plot_view.h" (19 Feb 2021, 5287 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "plot_view.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.4.4_vs_1.4.5.

    1 /**
    2  * plotview.h:
    3  *
    4  * This source file is public domain, as it is not based on the original tcpflow.
    5  *
    6  * Author: Michael Shick <mike@shick.in>
    7  *
    8  */
    9 
   10 
   11 #ifndef PLOT_VIEW_H
   12 #define PLOT_VIEW_H
   13 
   14 #ifdef HAVE_LIBCAIRO
   15 #ifdef HAVE_CAIRO_H
   16 #include <cairo.h>
   17 #elif defined HAVE_CAIRO_CAIRO_H
   18 #include <cairo/cairo.h>
   19 #endif
   20 #ifdef HAVE_CAIRO_PDF_H
   21 #include <cairo-pdf.h>
   22 #elif defined HAVE_CAIRO_CAIRO_PDF_H
   23 #include <cairo/cairo-pdf.h>
   24 #endif
   25 
   26 #include <vector>
   27 #include <string>
   28 #include <math.h>
   29 
   30 #ifndef M_PI
   31 #define M_PI        3.14159265358979323846
   32 #endif
   33 
   34 
   35 class plot_view {
   36 public:
   37     plot_view() :
   38         title("graph of things"), subtitle("x vs y"),
   39         x_label("x axis"), y_label("y axis"), x_tick_labels(), y_tick_labels(),
   40         right_tick_labels(),
   41         legend(), width(161.803), height(100.000), title_on_bottom(false),
   42         title_font_size(8.0), x_axis_font_size(8.0), y_axis_font_size(8.0),
   43         title_max_width_ratio(0.8), title_y_pad_factor(2.0), subtitle_y_pad_factor(0.2),
   44         subtitle_font_size_factor(0.4), axis_thickness_factor(0.002),
   45         tick_length_factor(0.0124), tick_width_factor(0.002),
   46         x_tick_label_pad_factor(4.0), y_tick_label_pad_factor(2.0), right_tick_label_pad_factor(2.0),
   47         x_tick_font_size(3.0), y_tick_font_size(3.0), right_tick_font_size(3.0), pad_left_factor(0.148),
   48         pad_top_factor(0.2), pad_bottom_factor(0.2), pad_right_factor(0.148),
   49         legend_chip_factor(1.2), legend_font_size(2.5),
   50         x_axis_decoration(AXIS_NO_DECO), y_axis_decoration(AXIS_NO_DECO) {}
   51 
   52     typedef enum {
   53         AXIS_NO_DECO = 0, AXIS_SPAN_ARROW, AXIS_SPAN_STOP
   54     } axis_decoration_t;
   55 
   56     class rgb_t {
   57     public:
   58         rgb_t() : r(0.0), g(0.0), b(0.0) {}
   59         rgb_t(const double r_, const double g_, const double b_) :
   60         r(r_), g(g_), b(b_) {}
   61         double r;
   62         double g;
   63         double b;
   64         static const double epsilon;    // 1/256.0 (not inline due to -Wgnu)
   65     };
   66 
   67     class legend_entry_t {
   68     public:
   69         legend_entry_t(const rgb_t color_, const std::string label_) :
   70         color(color_), label(label_) {}
   71         rgb_t color;
   72         std::string label;
   73     };
   74 
   75     class bounds_t {
   76     public:
   77         bounds_t() :
   78             x(0.0), y(0.0), width(0.0), height(0.0) {}
   79         bounds_t(const double x_, const double y_, const double width_,
   80                 const double height_) :
   81             x(x_), y(y_), width(width_), height(height_) {}
   82         double x;
   83         double y;
   84         double width;
   85         double height;
   86     };
   87 
   88     std::string title, subtitle;
   89     std::string x_label, y_label;
   90     std::vector<std::string> x_tick_labels, y_tick_labels, right_tick_labels;
   91     std::vector<legend_entry_t> legend;
   92     // width and height are in pt
   93     double width, height;
   94     bool title_on_bottom;
   95     double title_font_size;
   96     double x_axis_font_size, y_axis_font_size;
   97     // Title text will be shrunk if needed such that it takes up no more
   98     // than this ratio of the image width
   99     double title_max_width_ratio;
  100     // multiple of title height to be allocated above graph
  101     double title_y_pad_factor;
  102     // multiple of the subtitle height that will separate the subtitle from
  103     // the title
  104     double subtitle_y_pad_factor;
  105     // multiple of the title font size for the subtitle font size
  106     double subtitle_font_size_factor;
  107     // axis scale
  108     double axis_thickness_factor;
  109     // size of scale ticks, in pt
  110     double tick_length_factor, tick_width_factor;
  111     // multiple of label dummy text length to allocate for spacing
  112     double x_tick_label_pad_factor, y_tick_label_pad_factor, right_tick_label_pad_factor;
  113     double x_tick_font_size, y_tick_font_size, right_tick_font_size;
  114     // non-dynamic padding for the right and bottom of graph
  115     double pad_left_factor, pad_top_factor, pad_bottom_factor, pad_right_factor;
  116     // legend
  117     double legend_chip_factor;
  118     double legend_font_size;
  119     // axis decoration
  120     axis_decoration_t x_axis_decoration, y_axis_decoration;
  121 
  122     static const double text_line_base_width;
  123     static const double span_arrow_angle;
  124     static const double span_stop_angle;
  125     static const std::vector<std::string> size_suffixes;
  126 
  127     virtual ~plot_view() = 0;
  128     // render everything common to all plots (everything but the data)
  129     void render(cairo_t *cr, const bounds_t &bounds);
  130     // called by render(); subclass-specific data rendering
  131     virtual void render_data(cairo_t *cr, const bounds_t &bounds) = 0;
  132 
  133     // format a byte count for humans ( 12 MB etc)
  134     static std::string pretty_byte_total(uint64_t byte_count, uint8_t precision);
  135     static std::string pretty_byte_total(uint64_t byte_count);
  136     static std::vector<std::string> build_size_suffixes();
  137 };
  138 
  139 inline plot_view::~plot_view() {}
  140 
  141 inline bool operator==(const plot_view::rgb_t &a, const plot_view::rgb_t &b)
  142 {
  143     return fabs(a.r - b.r) < plot_view::rgb_t::epsilon &&
  144         fabs(a.g - b.g) < plot_view::rgb_t::epsilon &&
  145         fabs(a.b - b.b) < plot_view::rgb_t::epsilon;
  146 }
  147 
  148 inline bool operator!=(const plot_view::rgb_t &a, const plot_view::rgb_t &b)
  149 {
  150     return !(a == b);
  151 }
  152 
  153 inline bool operator<(const plot_view::rgb_t &a, const plot_view::rgb_t &b)
  154 {
  155     return a.r < b.r || a.g < b.g || a.b < b.b;
  156 }
  157 
  158 #endif
  159 #endif