1

slope我只是想在下面的数据中添加两个新变量,即intersect. data.csv 文件仅包含列出的列 - LevelsX和. 我想在下面的数据中添加上面提到的两个新变量。YSize

数据 :

    [
          {
            "key": "Level1",
            “价值观”:[
              {
                “x”:118,
                “y”:106,
                “尺寸”:1.113207547
              },
              {
                “x”:111,
                “y”:137,
                “尺寸”:0.810218978
              },
              {
                “x”:144,
                “y”:195,
                “大小”:0.738461538
              },
              {
                “x”:116,
                “y”:129,
                “大小”:0.899224806
              },
              {
                “x”:117,
                “y”:119,
                “大小”:0.983193277
              },
              {
                “x”:145,
                “y”:122,
                “大小”:1.18852459
              }
            ],
             “坡度”:0.52289599949494,
            “拦截”:0.2795214697252959
          },
          {
            "key": "Level2",
            “价值观”:[
              {
                “x”:172,
                “y”:193,
                “尺寸”:0.89119171
              },
              {
                “x”:138,
                “y”:114,
                “大小”:1.210526316
              },
              {
                “x”:106,
                “y”:189,
                “大小”:0.560846561
              },
              {
                “x”:123,
                “y”:141,
                “大小”:0.872340426
              },
              {
                “x”:129,
                “y”:110,
                “大小”:1.172727273
              },
              {
                “x”:162,
                “y”:198,
                “尺寸”:0.818181818
              }
            ],
             “坡度”:0.52289599949494,
            “拦截”:0.2795214697252959
          },
          {
            "key": "Level3",
            “价值观”:[
              {
                “x”:191,
                “y”:104,
                “大小”:1.836538462
              },
              {
                “x”:177,
                “y”:186,
                “尺寸”:0.951612903
              },
              {
                “x”:106,
                “y”:140,
                “大小”:0.757142857
              },
              {
                “x”:131,
                “y”:161,
                “大小”:0.813664596
              },
              {
                “x”:111,
                “y”:128,
                “大小”:0.8671875
              },
              {
                “x”:149,
                “y”:122,
                “大小”:1.221311475
              },
              {
                “x”:200,
                “y”:126,
                “尺寸”:1.587301587
              }
            ],
             “坡度”:0.52289599949494,
            “拦截”:0.2795214697252959
          }
        ]

尝试使用以下功能:

    d3.csv(“数据.csv”,函数(数据){

                    data.forEach(函数(d){
                        dx = +dx
                        dy = +dy
                        d.size = +d.size
                    })

                    //var type = ['基本手机', '特色手机', '智能手机']

                    var 巢 = d3.nest()
                                .key(function(d) {return d.type;})
                                .rollup(function(v) {return v.map(function(d) {delete d.type; return d; d.slope = Math.random(), d.intersent = Math.random()})})
                                .条目(数据);

                   d3.select('body').append('pre')
                   .text(JSON.stringify(nest, null, ' '));
                })  

4

2 回答 2

1
function(d) {delete d.type; return d; d.slope = Math.random(), d.intersent = Math.random()}

相当于

function(d) {delete d.type; return d;}

return 之后的内容不会被执行。尝试

function(d) {delete d.type; d.slope = Math.random(), d.intersent = Math.random(); return d; }
于 2013-11-07T10:55:05.363 回答
0

从 获取输出后nest.entries(),您可以迭代该数组并添加您的属性:

nest.forEach(function (d) { 
    d.slope = Math.random(); 
    d.intercept = Math.random(); 
})
于 2013-11-07T11:32:48.333 回答