2013-04-22 3 views
0

두 번째 데이터 계열을 막 대형 차트에 추가하는 데 도움이 필요합니다. 현재 내 데이터 집합의 글로브 키의 바를 채우고 있습니다. 이것은 첫 번째 시리즈가 될 것입니다. 두 번째 시리즈는 입니다. 추가하려면 어떻게해야합니까?페어링 된 막 대형 차트 작성 d3.js

Play with my JSFiddle here.

var w = 300; 
var h = 200; 

var colors = ["#377EB8", "#4DAF4A"]; 

var dataset = [ 
      {"keyword": "payday loans", "glob": 1500000, "local": 673000, "cpc": "14.11"}, 
      {"keyword": "title loans", "glob": 165000, "local": 165000, "cpc": "12.53" }, 
      {"keyword": "personal loans", "glob": 550000, "local": 301000, "cpc": "6.14"} 
     ]; 

var data = [[1500000, 165000, 550000], 
     [673000, 165000, 301000]]; 

var tdata = d3.transpose(dataset.glob, dataset.local); 

var series = 2; // Global & Local 

var x0Scale = d3.scale.ordinal() 
      .domain(d3.range(dataset.length)) 
      .rangeRoundBands([0, w], 0.05); 
var x1Scale = d3.scale.ordinal() 
      .domain(d3.range(dataset.length)) 
      .rangeRoundBands([0, w], 0.05); 

var yScale = d3.scale.linear() 
      .domain([0, d3.max(dataset, function(d) {return d.glob;})]) 
      .range([0, h]); 

var glob = function(d) { 
return d.glob; 
}; 
var cpc = function(d) { 
return d.cpc; 
}; 

var commaFormat = d3.format(','); 

//SVG element 
var svg = d3.select("#searchVolume") 
     .append("svg") 
     .attr("width", w) 
     .attr("height", h);  

// Graph Bars 
svg.selectAll("rect") 
.data(dataset, cpc, glob) 
.enter() 
.append("rect") 
.attr("x", function(d, i){ 
    return x0Scale(i); 
}) 
.attr("y", function(d) { 
    return h - yScale(d.glob); 
}) 
.attr("width", x0Scale.rangeBand()) 
.attr("height", function(d) { 
    return yScale(d.glob); 
}) 
.attr("fill", colors[1]) 
.on("mouseover", function(d) { 
    //Get this bar's x/y values, then augment for the tooltip 
    var xPosition = parseFloat(d3.select(this).attr("x")) + x0Scale.rangeBand()/3; 
    var yPosition = parseFloat(d3.select(this).attr("y")) + yScale; 

    //Update Tooltip Position & value 
    d3.select("#tooltip") 
     .style("left", xPosition + "px") 
     .style("top", yPosition + "px") 
     .select("#cpcVal") 
     .text(d.cpc); 
    d3.select("#tooltip").classed("hidden", false); 
}) 
.on("mouseout", function() { 
    //Remove the tooltip 
    d3.select("#tooltip").classed("hidden", true); 
}); 

//Create labels 
svg.selectAll("text") 
    .data(dataset, glob) 
    .enter() 
.append("text") 
.text(function(d) { 
    return commaFormat(d.glob); 
}) 
.attr("text-anchor", "middle") 
.attr("x", function(d, i) { 
    return x0Scale(i) + x0Scale.rangeBand()/2; 
}) 
.attr("y", function(d) { 
    return h - yScale(d.glob) + 14; 
}) 
.attr("font-family", "sans-serif") 
.attr("font-size", "11px") 
.attr("fill", "white"); 
+0

는이 그룹화 막대 차트의 예를 살펴 보았다위한 운동으로 남아 있습니다 //bl.ocks을 .org/mbostock/3887051 –

+0

나는 그것을 보았다. 그러나 그들은 CSV에서 데이터를 가져오고 있으며 다른 데이터 소스를 사용하고있을 때 이해하는 데 어려움이 있습니다 – EnigmaRM

+0

d3으로 처음 시작할 때 데이터 처리에 어려움을 겪었습니다. Mike Bostock과의 이러한 교류는 많은 도움이되었습니다. http://stackoverflow.com/questions/11672438 사례에 대한 구체적인 내용은 그가 내가 준 데이터 구조로 작업하는 데 시간을 낭비하지 않았다는 관찰만큼 중요하지 않습니다. 그는 그것을 d3가 사용하기에 더 쉬운 구조로 변형하려고했습니다. 매핑, for 루프 및 array.forEach 기능을 사용하면 d3에서 조인을 만들기 전에 접근 가능한 구조로 데이터를 가져올 수있는 풍부한 리소스가 있습니다. –

답변

4

그것이 SVG 그룹 데이터의 각각의 세트 (<g>) 요소를 생성하고, 각 그룹에 각 부분을 추가하는 가장 쉬운 방법. 대략 예를 들어 : HTTP :

var sets = svg.selectAll(".set") 
    .data(dataset) 
    .enter().append("g") 
      .attr("class","set") 
    .attr("transform",function(d,i){ 
     return "translate(" + x0Scale(i) + ",0)" 
    }) 
    .on("mouseover", function(d,i) { 
     //Create x value from data index 
     var xPosition = parseFloat(x0Scale(i) + x0Scale.rangeBand()/6); 
     var yPosition = 0; 
     //Update Tooltip Position & value 
     d3.select("#tooltip") 
      .style("left", xPosition + "px") 
      .style("top", yPosition + "px") 
      .select("#cpcVal") 
      .text(d.cpc); 
     d3.select("#tooltip").classed("hidden", false); 
    }) 
    .on("mouseout", function() { 
     //Remove the tooltip 
     d3.select("#tooltip").classed("hidden", true); 
    }); 

sets.append("rect") 
    .attr("class","glob") 
    .attr("width", x0Scale.rangeBand()/2) 
    .attr("y", function(d) { 
     return yScale(d.glob); 
    }) 
    .attr("height", function(d){ 
     return h - yScale(d.glob); 
    }) 
    .attr("fill", colors[1]) 

sets.append("rect") 
    .attr("class","local") 
    .attr("width", x0Scale.rangeBand()/2) 
    .attr("y", function(d) { 
     return yScale(d.local); 
    }) 
    .attr("x", x0Scale.rangeBand()/2) 
    .attr("height", function(d){ 
     return h - yScale(d.local); 
    }) 
    .attr("fill", colors[0]) 

텍스트 요소

http://jsfiddle.net/WXMwv/1/

독자 :

+0

텍스트를'sets.append ("rect")'에 추가했습니다. 파이어 버그로 요소를 검사하면 정확한 텍스트가 올바른 바에 바인딩되어 있지만 화면에 나타나지 않는 것을 볼 수 있습니다. 왜 이것이 될지 아십니까? – EnigmaRM

+0

동일한 방법으로 세트에 추가하십시오 : http://jsfiddle.net/WXMwv/3/. 레이어를 레이어하려면 먼저 svg를 추가해야합니다 (맨 아래부터 맨 위). – minikomi

관련 문제