Friday, May 20, 2011

PV Function Samples (Part 2)

pv.normalize

Description from Protovis docs : Returns a normalized copy of the specified array, such that the sum of the returned elements sum to one. If the specified array is not an array of numbers, an optional accessor function f can be specified to map the elements to numbers. For example, if array is an array of objects, and each object has a numeric property "foo", the expression



  • pv.normalize(array, function(d) d.foo) 

returns a normalized array on the "foo" property. If an accessor function is not specified, the identity function is used. Accessor functions can refer to this.index.


Sample Script

<html>
    <head>
    <script type="text/javascript"  src="protovis-r3.2.js"></script>
    </head>
    <body>
    <script type="text/javascript+protovis">  
    document.getElementById("pvfunction").innerHTML += pv.normalize([123]);
    </script>
    <div id="pvfunction">
    </div>    
    </body>
</html>

Script Result

0.16666666666666666,0.3333333333333333,0.5


pv.cross
Description from Protovis docs : Given two arrays a and b, returns an array of all possible pairs of elements [ai, bj]. The outer loop is on array a, while the inner loop is on b, such that the order of returned elements is [a0, b0], [a0, b1], ... [a0, bm], [a1, b0], [a1, b1], ... [a1, bm], ... [an, bm]. If either array is empty, an empty array is returned.


Sample Script

<html>
    <head>
    <script type="text/javascript"  src="protovis-r3.2.js"></script>
    </head>
    <body>
    <script type="text/javascript+protovis"
    var array1 = pv.range(0,12,1);
    var array2 = [200];
    var array3 = pv.cross(array1, array2);
    for(i=0; i<array3.length;i++)
    {
        document.getElementById("pvfunction").innerHTML 
            += "index " + i + " = [" + array3[i] + "]<BR />";
    }
    </script>
    <div id="pvfunction">
    </div>    
    </body>
</html>


Script Result

index 0 = [0,200]
index 1 = [1,200]
index 2 = [2,200]
index 3 = [3,200]
index 4 = [4,200]
index 5 = [5,200]
index 6 = [6,200]
index 7 = [7,200]
index 8 = [8,200]
index 9 = [9,200]
index 10 = [10,200]
index 11 = [11,200]

No comments:

Post a Comment