如何使用Javascript函数更新Protovis数据

How to update Protovis data with Javascript function?

本文关键字:Protovis 数据 更新 函数 何使用 Javascript      更新时间:2023-09-26

我正在使用"Protovis Mean&Deviation Example"绘制图表。在我的html文件中,我包含了这个protovis代码:

<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>

现在,为了给protovis提供有效的数据,我编写了一个Javascript函数。我的javascript函数中的相关代码如下:

        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;
                }
            }
        }

此函数填充protovis的"nba"数据结构。根据我的要求,数据结构是有效的。这种数据结构的示例如下:

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]
];

问题:既然protovis脚本在我的main.html文件的script标记中,那么,我应该如何传递protovis这个"nba"数据结构呢?我只想在用javascript函数填充"nba"之后执行protovis代码。

我希望我已经解决了我的问题,期待着建议和解决方案。非常感谢您抽出时间。

我只是觉得你的帖子太晚了。但我想花点时间回答,以防对其他人有用。

所有protovis或d3函数基本上都与json或csv存储的数据一起工作。此处:var nba = json.

我想这个函数会自动填充:

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

因此,您唯一需要做的就是更改数据,并将此脚本作为nba.js文件与此脚本一起添加到html或php页面中。