2012-09-26 6 views
2

저는 초보자입니다. 두 배열의 외적을 간단하게 구성하는 데 어려움을 겪고 있음을 인정하게되어 유감입니다. 저는 큐비즘과 실제로 일하고 있습니다. 아주 단순한 작업을하고 있습니다. 각 유형에 대해 몇 가지 집계를 만듭니다.입체주의, d3 데이터 합류

내 시도 :

var aggs = [ 
    {title: 'mean', metric: function(d) { return cube.metric("sum(" + d + (value))").divide(cube.metric("sum(" + d + ")")); }}, 
    {title: 'count', metric: function(d) { return cube.metric("sum(" + d + ")").divide(step/1e3); }} 
] 

d3.json(origin + "/1.0/types", function(types) {  
    d3.select("body").insert("div", ".bottom") 
    .attr("class", "group") 
    .selectAll(".typegrp") 
    .data(types) 
    .enter() 
    .append("div") 
    .attr("class", "typegrp") 
    .selectAll("div") 
    .data(aggs) 
    .enter() 
    .append("div") 
    .attr("class","horizon") 
    .call(context.horizon() 
     .title(function(d) { return d.title; }) 
     .metric(function(d) { return d.metric(FOOOO); }) 
    ); 
}); 

이해가되지 않는 부분은 "FOOOO"부분입니다. '내부'컨텍스트 ('aggs'의 행)에서 '외부'컨텍스트 ('유형'의 행)에 어떻게 액세스합니까?

미리 감사드립니다.

편집 : 다음은 nick이 제안한 완전한 작동 예제입니다.

var aggs = [ 
      {title: 'mean', metric: function(d) { return cube.metric("sum(" + d + "(value))").divide(cube.metric("sum(" + d + ")")); }}, 
      {title: 'sum', metric: function(d) { return cube.metric("sum(" + d + "(value))"); }}, 
      {title: 'count', metric: function(d) { return cube.metric("sum(" + d + ")"); }}, 
      {title: 'max', metric: function(d) { return cube.metric("max(" + d + "(value))"); }}, 
      {title: 'min', metric: function(d) { return cube.metric("min(" + d + "(value))"); }} 
     ] 

d3.json(origin + "/1.0/types", function(types) {  
    d3.select("body") 
    .insert("div", ".bottom") 
    .attr("class", "group") 
    .selectAll(".typegrp") 
    .data(types) 
    .enter() 
    .append("div") 
    .attr("class", "typegrp") 
    .each(function(type) { 
     d3.select(this) 
     .selectAll("div.horizon") 
     .data(aggs) 
     .enter().append("div") 
     .attr("class","horizon") 
     .each(function(agg) { 
      d3.select(this) 
      .call(context.horizon() 
       .title(agg.title + " " + type) 
       .metric(agg.metric(type)) 
      ); 
     }); 
    }); 
}); 
+1

"내가 이해할 수없는 부분은"FOOOO "부분입니다." -이 멋진 견적입니다 :) – nrabinowitz

답변

2

내가 입체파를 사용하지 않은,하지만 난 여기에 두 가지 옵션이 있다고 생각 :

  1. 당신이 정말로 두 집합 메트릭이있는 경우 쉬운 옵션 - 데이터에 가입하지 않는다 그냥 별도로 추가하십시오.

    var typeGroups = d3.select("body").insert("div", ".bottom") 
        .attr("class", "group") 
        .selectAll(".typegrp") 
        .data(types); 
    
    typeGroups.enter() 
        .append("div") 
        .attr("class", "typegrp"); 
    
    typeGroups 
        .append("div") 
        .attr("class","horizon") 
        .call(context.horizon() 
         .title('mean') 
         .metric(function(d) { return cube.metric(...); }) 
        ); 
    
    typeGroups. 
        .append("div") 
        .attr("class","horizon") 
        .call(context.horizon() 
         .title('count') 
         .metric(function(d) { return cube.metric(...); }) 
        ); 
    
  2. 경미하게 더 열심히 옵션

    은 - .each 호출 내에서 aggs 배열에 가입 : 이것은 당신이 context.horizon()를 호출시 type 변수에 액세스 할 수 있습니다

    typeGroups.enter() 
        .append("div") 
        .attr("class", "typegrp") 
        .each(function(type) { 
         d3.select(this).selectAll("div.horizon") 
          .data(aggs) 
          .enter().append("div") 
          .attr("class","horizon") 
          .each(function(agg) { 
           d3.select(this) 
            .call(context.horizon() 
             .title(agg.title) 
             .metric(agg.metric(type)) 
            ); 
          }); 
        }); 
    

    .

+0

멋진! 나는 마이크가 각자없이 그것을 할 수 있다고 (http://bost.ocks.org/mike/join/) 생각한 것처럼, 나는 each()를 사용하지 않고 그것을하려고 노력했다. 당신의 두 번째 해결책은 정확히 내가 멍청이였던 것이다. 모든 사람이보고 싶다면 완전한 예제로 원래 게시물을 편집 할 것입니다. –