1 | /**
|
---|
2 | * @fileOverview A simple one dimensional histogram.
|
---|
3 | * @author <a href="mailto:thomas.bretz@epfl.ch">Thomas Bretz</a>
|
---|
4 | */
|
---|
5 | 'use strict';
|
---|
6 |
|
---|
7 |
|
---|
8 | if (!("Hist1D" in this)){
|
---|
9 | var Hist1D = {
|
---|
10 | /**
|
---|
11 | *
|
---|
12 | * @constructor
|
---|
13 | *
|
---|
14 | * @param {Interger} nx
|
---|
15 | *
|
---|
16 | * @param {Number} xmin
|
---|
17 | *
|
---|
18 | * @param {Number} xmax
|
---|
19 | *
|
---|
20 | * @returns
|
---|
21 | * A sub-classed array with the Hist1D functions added as properties.
|
---|
22 | *
|
---|
23 | * @example
|
---|
24 | * var hist = Hist1D(10, -0.5, 1.5);
|
---|
25 | *
|
---|
26 | */
|
---|
27 | Hist1D : function(nx, xmin, xmax)
|
---|
28 | {
|
---|
29 | /**
|
---|
30 | *
|
---|
31 | * Array
|
---|
32 | *
|
---|
33 | */
|
---|
34 | var arr = new Array(nx);
|
---|
35 |
|
---|
36 | /**
|
---|
37 | *
|
---|
38 | * @exports arr.get as Hist1D.get
|
---|
39 | *
|
---|
40 | */
|
---|
41 | arr.get = function(x)
|
---|
42 | {
|
---|
43 | var ix = parseInt(nx*(x-xmin)/(xmax-xmin));
|
---|
44 |
|
---|
45 | return arr[ix] ? arr[ix] : 0;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | *
|
---|
50 | * @exports arr.fill as Hist1D.fill
|
---|
51 | *
|
---|
52 | */
|
---|
53 | arr.fill = function(x, w)
|
---|
54 | {
|
---|
55 | if (!x || x===NaN)
|
---|
56 | return false;
|
---|
57 |
|
---|
58 | var ix = parseInt(nx*(x-xmin)/(xmax-xmin));
|
---|
59 | if (ix<0 || ix>=nx)
|
---|
60 | return false;
|
---|
61 |
|
---|
62 | if (!arr[ix])
|
---|
63 | arr[ix] = 0;
|
---|
64 |
|
---|
65 | arr[ix] += w ? w : 1;
|
---|
66 |
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | *
|
---|
72 | * @exports arr.print as Hist1D.print
|
---|
73 | *
|
---|
74 | */
|
---|
75 | arr.print = function(len)
|
---|
76 | {
|
---|
77 | if (!len)
|
---|
78 | len = 40;
|
---|
79 | if (len<6)
|
---|
80 | len = 6;
|
---|
81 |
|
---|
82 | var sum = arr.reduce(function(a,b){return a+b;}, 0);
|
---|
83 | var max = arr.reduce(function(a,b){return Math.max(a,b);}, 0);
|
---|
84 |
|
---|
85 | console.out("");
|
---|
86 | for (var ix=nx-1; ix>=0; ix--)
|
---|
87 | {
|
---|
88 | var entry = arr[ix] ? arr[ix] : 0;
|
---|
89 |
|
---|
90 | var line ="%3d [%3d%] ".$(ix, sum==0 ? 0 : 100*entry/sum);
|
---|
91 | if (arr[ix])
|
---|
92 | {
|
---|
93 | var val = parseInt(len*arr[ix]/max);
|
---|
94 | var entry = ("%"+val+"s").$("");
|
---|
95 | entry = entry.replace(/ /g, "*");
|
---|
96 | line += ("%-"+len+"s").$(entry)+" |";
|
---|
97 | }
|
---|
98 |
|
---|
99 | console.out(line);
|
---|
100 | }
|
---|
101 |
|
---|
102 | var entry = arr[ix] ? arr[ix] : 0;
|
---|
103 | console.out(" --------"+("%"+(len-5)+"s").$("")+"-------");
|
---|
104 |
|
---|
105 | var line =" %9d ".$(sum);
|
---|
106 | line += ("%"+len+"d").$(max);
|
---|
107 | console.out(line);
|
---|
108 | console.out("");
|
---|
109 | }
|
---|
110 |
|
---|
111 | return arr;
|
---|
112 | },
|
---|
113 | };
|
---|
114 | }
|
---|
115 | else{
|
---|
116 | console.out("multiple include of 'Hist1D.js'")
|
---|
117 | }
|
---|
118 |
|
---|