source: trunk/lib/StatsPlotter.cc @ 1513

Last change on this file since 1513 was 1513, checked in by Peter Johansson, 9 years ago

remove trailing whitespace

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