libmoost
/home/mhx/git/github/libmoost/include/moost/utils/histogram.hpp File Reference
#include <algorithm>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <limits>
#include "foreach.hpp"
Include dependency graph for histogram.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  moost::utils::histogram< FloatType >
struct  moost::utils::histogram< FloatType >::data_info
struct  moost::utils::histogram< FloatType >::range_info
struct  moost::utils::histogram< FloatType >::stats

Namespaces

namespace  moost
 

Creates a unique temporary directory; removed on scope exit.


namespace  moost::utils

Detailed Description

Copyright © 2008-2013 Last.fm Limited

This file is part of libmoost.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

This is the (slightly hacky) implementation of a class that can output an ASCII histogram graph to a stream. Its main focus is ease of use.

Usually, all you have to do is to provide the dimensions of your graph, i.e. width and height in characters, and one or more data vectors. Each data vector can be represented by a different character in the graph.

The histogram class takes care of scaling the data, using an optimum (well, I guess it could be made even better, but it's good enough for a start) range, drawing nice tick marks and providing some statistics on the data.

Here's a short example to show how easy this class is to use. You'll notice that most of the code is actually used to create the data vectors.

#include <iostream>
#include <vector>

#include <boost/random.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/normal_distribution.hpp>

#include "histogram.hpp"

template <class ListType, class RngType, class DistributionType>
void random_fill(ListType& list, RngType& rng, DistributionType& dist)
{
   boost::variate_generator<RngType&, DistributionType> die(rng, dist);
   std::generate(list.begin(), list.end(), die);
}

int main()
{
   boost::mt19937 rng;
   std::vector<float> normal(5000), uniform(2000);

   boost::uniform_real<float> ud(2.3e-5, 2.9e-5);
   random_fill(uniform, rng, ud);

   boost::normal_distribution<float> nd(2.0e-5, 0.4e-5);
   random_fill(normal, rng, nd);

   moost::utils::histogram<float> hist("s", 90, 16);
   hist.set_display_range(0.01, 0.99);

   hist.add(uniform.begin(), uniform.end(), "uniform", "#");
   hist.add(normal.begin(), normal.end(), "normal", "/");

   hist.draw(std::cout);

   return 0;
}

This code will produce the following graph on your console:

-------------------------------------------------------------------------------------------
                                                //
                                   /            
                             /     /            
                             /   / 


                      // /////////////////////////#/#//#//##///#/#//////
/                 
/               
/            
/      // / ////////////////////////////////////########################
/      // //////////////////////////////////////########################
/  / 

-------------------------------------------------------------------------------------------
'   '   '   '   |   '   '   '   '   |   '   '   '   '   |   '   '   '   '   |   '   '   '
                15                  20                  25                  30 us

 [#] uniform (28.57 %): 25.97 ± 1.729 us [23 .. 29 us]
 [/] normal (71.43 %): 19.91 ± 4.042 us [5.541 .. 33.88 us]

 overall: 21.64 ± 4.473 us [5.541 .. 33.88 us]

-------------------------------------------------------------------------------------------

Definition in file histogram.hpp.