source: trunk/lib/StatsPlotter.cc @ 1290

Last change on this file since 1290 was 1290, checked in by Peter Johansson, 11 years ago

closes #457. Lifting out plotting functions in Stats into a new class
StatsPlotter?. In same manner lifting out print functions from
Node/Directory/File? into a new hierarchy
NodePrinter/DirectoryPrinter/FilePrinter?. To accomplish these splits
without to much recoding some friendships are introduced. These new
classes are only needed for svndigest (not svncopyright) and are
therefore placed in a library named libsvndigest together with Graph
class and first_page functions. Remaining functionality, shared
between svncopyright and svndigest, are located in libsvndigest_core
and linked into both binaries.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1// $Id: StatsPlotter.cc 1290 2010-11-12 04:01:35Z peter $
2
3/*
4  Copyright (C) 2005 Peter Johansson
5  Copyright (C) 2006, 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2010 Peter Johansson
7
8  This file is part of svndigest, http://dev.thep.lu.se/svndigest
9
10  svndigest is free software; you can redistribute it and/or modify it
11  under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 3 of the License, or
13  (at your option) any later version.
14
15  svndigest is distributed in the hope that it will be useful, but
16  WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "StatsPlotter.h"
25
26#include "Colors.h"
27#include "Configuration.h"
28#include "Functor.h"
29#include "Graph.h"
30#include "Stats.h"
31#include "utility.h"
32
33#include <algorithm>
34#include <cassert>
35#include <cstdlib>
36#include <iostream>
37#include <iterator>
38#include <map>
39#include <numeric>
40#include <string>
41#include <sstream>
42#include <unistd.h>
43#include <utility>
44#include <vector>
45
46namespace theplu{
47namespace svndigest{
48
49
50  StatsPlotter::StatsPlotter(const Stats& stats)
51    : stats_(stats)
52  {
53  }
54
55
56  StatsPlotter::~StatsPlotter(void)
57  {
58  }
59
60 
61  std::string StatsPlotter::plot(const std::string& filename,
62                                 const std::string& linetype) const
63  {
64    const std::string& format = Configuration::instance().image_format();
65    if (format=="none")
66      return filename;
67    plot(filename, linetype, format);
68    const std::string& anchor_format = 
69      Configuration::instance().image_anchor_format();
70
71    if (format!=anchor_format)
72      plot(filename, linetype, anchor_format);
73    return filename;
74  }
75
76
77  void StatsPlotter::plot(const std::string& filename,
78                          const std::string& linetype,
79                          const std::string& format) const
80  {
81    Graph gp(filename+"."+format, format);
82    const Author2Vector* stat=NULL;
83    if (linetype=="total")
84      stat = &stats_.total_stats();
85    else if (linetype=="code")
86      stat = &stats_.code_stats();
87    else if (linetype=="comments")
88      stat = &stats_.comment_or_copy_stats();
89    else if (linetype=="empty")
90      stat = &stats_.other_stats();
91    assert(stat);
92    assert(stat->size());
93    assert(stat->find("all")!=stat->end());
94    const SumVector& total=stats_.get_vector(*stat, "all");   
95    double yrange_max=1.03 * stats_.max_element(total) +1.0;
96    gp.ymax(yrange_max);
97
98    typedef std::vector<std::pair<std::string, SumVector> > vec_type;
99    vec_type author_cont;
100    author_cont.reserve(stat->size());
101    for (std::set<std::string>::const_iterator i=stats_.authors_.begin(); 
102         i != stats_.authors_.end(); ++i) {
103      assert(stat->find(*i)!=stat->end());
104      const SumVector& vec = stats_.get_vector(*stat,*i);
105      if (stats_.max_element(vec)) {
106        author_cont.push_back(std::make_pair(*i,vec));
107      }
108    }
109
110    LessReversed<SumVector> lr;
111    PairSecondCompare<std::string, SumVector, LessReversed<SumVector> > 
112      compare(lr);
113    std::sort(author_cont.begin(), author_cont.end(), compare);
114
115    vec_type::iterator end(author_cont.end());
116    vec_type::iterator i(author_cont.begin());
117    const vec_type::size_type maxauthors=8;
118    int authskip=author_cont.size()-maxauthors;
119    if (authskip>1) {
120      // only use others if there is more than 1 author to be skipped,
121      // there is no reason to add only 1 author to others.
122      vec_type::iterator j(i);
123      i+=authskip;
124      SumVector others;
125      sum(j, i, others, PairValuePlusAssign<std::string, SumVector>());
126      unsigned char r, g, b;
127      std::string label("others");
128      Colors::instance().get_color(label, r,g,b);
129      gp.current_color(r,g,b);
130      gp.plot(others, label, others.back());
131    }
132    for ( ; i!=end; ++i) {
133      unsigned char r, g, b;
134      Colors::instance().get_color(i->first,r,g,b);
135      gp.current_color(r,g,b);
136      gp.plot(i->second, i->first, stats_.get_back(*stat, i->first));
137    }
138    gp.current_color(255,0,0);
139    gp.plot(total, "total", stats_.get_back(*stat, "all"));
140  }
141
142
143  void StatsPlotter::plot_summary(const std::string& filename) const
144  {
145    const std::string& format = Configuration::instance().image_format();
146    if (format=="none")
147      return;
148    plot_summary(filename, format);
149    const std::string& anchor_format = 
150      Configuration::instance().image_anchor_format();
151
152    if (format!=anchor_format)
153      plot_summary(filename, anchor_format);
154  }
155
156
157  void StatsPlotter::plot_summary(const std::string& filename, 
158                                  const std::string& format) const
159  {
160    Graph gp(filename+"."+format, format);
161    const SumVector& total = stats_.get_vector(stats_.total_stats(), "all");
162    double yrange_max=1.03*stats_.max_element(total)+1;
163    gp.ymax(yrange_max);
164   
165    const SumVector& x(stats_.get_vector(stats_.code_stats(), "all"));
166    gp.current_color(255,255,0);
167    gp.plot(x, "code", x.back());
168
169    const SumVector& y = stats_.get_vector(stats_.comment_or_copy_stats(), 
170                                           "all");
171    gp.current_color(0,0,255);
172    gp.plot(y, "comment", y.back());
173
174    const SumVector& z = stats_.get_vector(stats_.other_stats(), "all");
175    gp.current_color(0,255,0);
176    gp.plot(z, "other", z.back());
177
178    gp.current_color(255,0,0);
179    gp.plot(total, "total", total.back());
180  }
181
182}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.