在 Protovis 中处理多个数据对象(javascript 信息可视化)

发布于 2024-09-25 23:00:27 字数 1346 浏览 3 评论 0原文

数字感到非常沮丧,这些数组看起来如下所示,为每个对象绘制三个单独的饼图(pv.Wedge)...

myData = [{dept:"Accounting",sal:90000,equ:10000,trvl:267,extra:5000},
              {dept:"Sales",sal:20000,equ:10000,trvl:3049,extra:7000},
              {dept:"Consulting",sal:90000,equ:58000,trvl:983,extra:17000}];

我对尝试修剪并移交给 Protovis 一组仅包含一组数据对象中的 文档告诉我在 Protovis 中几乎不需要做循环,但我似乎无法正确操作/解析 myData,所以可惜我诉诸于显式循环。

我尝试了许多不同类型的循环,但我得到的最好的循环是在空白区域下打印数字,我希望在其中显示饼图。如果有人能给我一些提示,告诉我应该做什么来实现这一目标,我将不胜感激。目前我陷入了 -

function getData(dept) {
   var getvals = new Array();
     for(idx in dept) {
       for(i in idx) {
           if(idx[i]=="^[0-9]+$") {
             getme.push(idx[i]); 
       }
   }      
 }
   return getvals;   

}

// myData = myData.map(function(d) {
//    var valonly = new Array();
//    for(key in d) {
//       if(isNaN(d[key])==false) {
//          valonly.push(d[key]);
//       }
//    }
//    return valonly;
// });


var vis = new pv.Panel()
  .width(w)
  .height(h)
  .data(getData(myData))
vis.add(pv.Wedge)
  //.data(pv.normalize(getData(myData)))
  .left(100) 
  .bottom(100)
  .outerRadius(r)
  .angle(function(d) d * 2 * Math.PI)
vis.add(pv.Label)
  .bottom(0)
  .left(100)
  .textAlign("center")
  //.text(data.dept + " - " + "total: " + hrsum);


vis.render();

I am extremely frustrated with trying to prune and hand over to Protovis a set of arrays only containing numbers from a set of data objects that looks something like below to draw up three separate pie charts (pv.Wedge) for each object...

myData = [{dept:"Accounting",sal:90000,equ:10000,trvl:267,extra:5000},
              {dept:"Sales",sal:20000,equ:10000,trvl:3049,extra:7000},
              {dept:"Consulting",sal:90000,equ:58000,trvl:983,extra:17000}];

From the documentation I'm told there's little looping one needs to do in Protovis, yet I can't seem to get the myData manipulated/parsed correctly, so alas I've resorted to explicit looping.

I've tried many different kinds of loops but the best I've gotten is a print out of the numbers under an empty space where I would like the pie charts to appear. I would be grateful if someone could give me a hint as to what I should be doing to achieve this. Presently I am stuck at -

function getData(dept) {
   var getvals = new Array();
     for(idx in dept) {
       for(i in idx) {
           if(idx[i]=="^[0-9]+$") {
             getme.push(idx[i]); 
       }
   }      
 }
   return getvals;   

}

// myData = myData.map(function(d) {
//    var valonly = new Array();
//    for(key in d) {
//       if(isNaN(d[key])==false) {
//          valonly.push(d[key]);
//       }
//    }
//    return valonly;
// });


var vis = new pv.Panel()
  .width(w)
  .height(h)
  .data(getData(myData))
vis.add(pv.Wedge)
  //.data(pv.normalize(getData(myData)))
  .left(100) 
  .bottom(100)
  .outerRadius(r)
  .angle(function(d) d * 2 * Math.PI)
vis.add(pv.Label)
  .bottom(0)
  .left(100)
  .textAlign("center")
  //.text(data.dept + " - " + "total: " + hrsum);


vis.render();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2024-10-02 23:00:28

以下作品。我按照您定义的方式处理数据。如果楔形的值本身位于数组中,则使用起来会更容易。也就是说,从对象中提取数据很有趣。 def 创建一个局部变量。我选择将其用于值和总计,而不是标准化,因为这样可以更轻松地稍后添加标签。可能有一种更优雅的方法可以做到这一点,但是您应该能够看到一种无需循环的方法。

var myData = [{dept:"Accounting",sal:90000,equ:10000,trvl:267,extra:5000}, 
    {dept:"Sales",sal:20000,equ:10000,trvl:3049,extra:7000}, 
    {dept:"Consulting",sal:90000,equ:58000,trvl:983,extra:17000}]; 

var vis = new pv.Panel() 
    .width(200) 
    .height(200)
    .data(myData);

vis.add(pv.Wedge)
    .def("values", function(d) pv.entries(d).filter(function(e) !isNaN(e.value)))
    .def("total", function(d) pv.sum(this.values(), function(e) e.value))
    .data(function(d) this.values())
    .left(100)  
    .bottom(100) 
    .outerRadius(90)
    .angle(function(d) (d.value / this.total()) * 2 * Math.PI )
.anchor("center").add(pv.Label)
    .text(function(d) d.key);

vis.add(pv.Label)
    .bottom(0)
    .left(50) 
    .textAlign("center")
    .text(function(d) d.dept); 

vis.render(); 

The following works. I worked with the data as you had it defined. It could be easier to work with if the values for the wedges were themselves in an array. That said, it was interesting teasing out the data from the object. def creates a local variable. I chose to use that for values and total rather than normalize as it then made it easier to add lables later on. There's possibly a more elegant way of doing this, but you should be able to see one approach without looping.

var myData = [{dept:"Accounting",sal:90000,equ:10000,trvl:267,extra:5000}, 
    {dept:"Sales",sal:20000,equ:10000,trvl:3049,extra:7000}, 
    {dept:"Consulting",sal:90000,equ:58000,trvl:983,extra:17000}]; 

var vis = new pv.Panel() 
    .width(200) 
    .height(200)
    .data(myData);

vis.add(pv.Wedge)
    .def("values", function(d) pv.entries(d).filter(function(e) !isNaN(e.value)))
    .def("total", function(d) pv.sum(this.values(), function(e) e.value))
    .data(function(d) this.values())
    .left(100)  
    .bottom(100) 
    .outerRadius(90)
    .angle(function(d) (d.value / this.total()) * 2 * Math.PI )
.anchor("center").add(pv.Label)
    .text(function(d) d.key);

vis.add(pv.Label)
    .bottom(0)
    .left(50) 
    .textAlign("center")
    .text(function(d) d.dept); 

vis.render(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文