How to update Protovis data with Javascript function?

104 views Asked by At

I am drawing a chart using "Protovis Mean & Deviation Example". In my html file I included this protovis code:

<script type="text/javascript+protovis">

/* Convert from tabular format to array of objects. */
var cols = nba.shift();
nba = nba.map(function(d) pv.dict(cols, function() d[this.index]));    
cols.shift();

/* The color scale ranges 3 standard deviations in each direction. */
var x = pv.dict(cols, function(f) pv.mean(nba, function(d) d[f])),
s = pv.dict(cols, function(f) pv.deviation(nba, function(d) d[f])),
fill = pv.dict(cols, function(f) pv.Scale.linear(0, 50,100).range('red', 'yellow', 'green'));

var w = 50, h = 20;

var vis = new pv.Panel()
.width(cols.length * w)
.height(nba.length * h)
.top(70)
.left(100.5)
.right(.5)
.bottom(.5);

vis.add(pv.Panel)
.data(cols)
.left(function() this.index * w)
.width(w)
.add(pv.Panel)
.data(nba)
.top(function() this.index * h)
.height(h)
.strokeStyle("white")
.lineWidth(1)
.fillStyle(function(d, f) fill[f](d[f]))
.title(function(d, f) d.Name + "'s " + f + ": " + d[f]);

vis.add(pv.Label)
.data(cols)
.left(function() this.index * w + w / 2)
.top(0)
.textAngle(-Math.PI / 2)
.textBaseline("middle");

vis.add(pv.Label)
.data(nba)
.left(0)
.top(function() this.index * h + h / 2)
.textAlign("right")
.textBaseline("middle")
.text(function(d) d.Name);

vis.render();

</script>

Now, to provide protovis with valid data, I have written a Javascript function. Relevant code from my javascript function is below:

        for(var k=0; k< xlabelValues.length; k++){          //xAxis header values are in xlabelValues
            nba[0][k+1] = xlabelValues[k];              
        }

        for(var k=0; k< ylabelValues.length; k++){           //yAxis header values are in ylabelValues
            nba[k+1] = [];
            nba[k+1][0] = ylabelValues[k];
            for(var e=1; e<nba[0].length; e++){             
                zValue = hash3D[nba[0][e]+","+ylabelValues[k]];  //hash3D contains numeric values for (x,y) as KEY
                if(typeof zValue == "undefined"){
                    nba[k+1][e] = 0;
                }else{
                    nba[k+1][e] = zValue;
                }
            }
        }

This function populates "nba" data structure for protovis. The data structure is valid as I require. A sample of such data structure is below:

var nba = [
["","January","Feburary","March","April","May","June","July","August","Sep temebr","October","November","December"],
["Event 1",79,38.6,30.2,10.8,22,0.491,7.5,9.8,0.765,1.1,3.5,0.317],
["Event 2",81,37.7,28.4,9.7,19.9,0.489,7.3,9.4,0.78,1.6,4.7,0.344],
["Event 3",82,36.2,26.8,9.8,20.9,0.467,5.9,6.9,0.856,1.4,4.1,0.351],
["Event 4",81,37.7,25.9,9.6,20,0.479,6,6.7,0.89,0.8,2.1,0.359]
];

PROBLEM: Since, protovis script is on my main.html file in the script tag, therefore, how should I pass protovis this "nba" data structure? I only want protovis code be executed after I have populated "nba" with my javascript function.

I hope I have cleared my problem, looking forward for suggestions and solutions. Thank you very much for your time.

1

There are 1 answers

0
Paul Endymion On

I just found your post way too late I guess. But I wanted to take time to answer in case it can be usefull for someone else.

All of protovis or d3 function basically works with json or csv stored data. Here : var nba = json.

I guess the function does automatically populate :

var cols = data.shift();
    nba = data.map(function(d) pv.dict(cols, function() d[this.index]));
    cols.shift();

So the only thing you have to do is to change the datas and add this script to you html or php page as the nba.js file along with this script.