2016-08-04 3 views
0

php로 생성 된 일부 json에서 생성 된 d3 차트가 있습니다. 모든 것은 잘 작동합니다 - PHP는 json을 올바르게 포맷하고 차트가 제대로 표시됩니다. 그것은 here을 볼 수있다, 나는 아래의 자바 스크립트 부분에 대한 전체 코드를 포함하고 있습니다 :d3의 두 데이터 소스에 대한 툴팁 문제

내 문제는 내가 두 툴팁을 가지고 내가 JSON의 두 개의 하위 섹션에서 그리는, 포함 할 것입니다
  var margin = {top: 50, right: 150, bottom: 50, left: 150}, 
      w = 2500 - margin.left - margin.right, 
      h = 500 - margin.top - margin.bottom; 


var json = <?php echo $json; ?>; 

console.log(json); 

d3.json("MM_chart.json", function(error, json) { 
    if (error) throw error; 

      var items = json.items; 

      var locations = json.locations; 

      var directions = json.stage_directions; 

      console.log(items); 
      console.log(locations); 
      console.log(directions); 

      var stage_directions = ([44, 49, 114, 140, 209, 217, 249, 257, 265, 277, 305, 334, 358, 381, 398, 409, 440, 470, 491, 547, 555, 564, 572, 587, 614, 630, 640, 691, 704, 725, 743, 775, 793, 818, 823, 841, 845, 868, 872, 888, 902, 910, 920, 925, 963, 993, 1005, 1023, 1031, 1047, 1061, 1096, 1125, 1133, 1143, 1178, 1210, 1228, 1281, 1293, 1336, 1349, 1376, 1395, 1439, 1446, 1454, 1538, 1554, 1562, 1578, 1598, 1610, 1618, 1642, 1646, 1716, 1725, 1743, 1781, 1791, 1797, 1843, 1863, 1887, 1915, 1923, 1939 ,1972, 1989, 2019, 2031, 2039, 2045, 2073, 2085, 2101, 2123]); 

      var x = d3.scale.linear() 
      .domain([0, 2200]) 
      .range([0, w]); 

      var y = d3.scale.ordinal() 
      .domain(locations.map(function(d) {return d["location"];})) 
      .rangeBands([0, h]); 

      var yAxis = d3.svg.axis() 
      .scale(y) 
      .orient("left"); 

      var xAxis = d3.svg.axis() 
      .scale(x) 
      .orient("bottom"); 

      var tip = d3.tip() 
      .attr('class', 'd3-tip') 
      .offset([-10, 0]) 
      .html(function(d, i) {return console.log(d.line_text) + "<strong>(" + d.starting_line + ")</strong> <i>" + d.character + "</i>: " + d.line_text;}); 

      var stage_tip = d3.tip() 
      .attr('class', 'd3-tip') 
      .offset([-10, 0]) 
      .html(function(d, i) {return console.log(d.direction_text) + "<strong>(" + d.line_after + ") s.d.</strong> <i>" + d.direction_text;}); 

      var svg = d3.select("body").append("svg") 
      .attr("width", w + margin.left + margin.right) 
      .attr("height", h + margin.top + margin.bottom) 
      .append("g") 
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 


      svg.append("text") 
      .attr("transform", "translate(" + (w/2) + " ," + (h + margin.bottom - 5) +")") 
      .style("text-anchor", "middle") 
      .text("Line Number"); 

      var stage_bars = svg.selectAll(".stage_bar") 
       .data(directions) 
       .enter() 
       .append("rect") 
       .attr("class", "overlay") 
       .attr("class", function(d, i) {return "stage_bar " + d.direction_text;}) 
       .attr("x", function(d, i) {return d.line_after;}) 
       .attr("width", 1) 
       .attr("height", h) 
       .style("fill", "black") 
       .style("opacity",0.3) 
       .call(stage_tip) 
       .on('mouseover', stage_tip.show) 
       .on('mouseout',stage_tip.hide); 

      svg.append("rect") 
      .attr("class", "overlay") 
      .attr("x",1) 
      .attr("width", 1133) 
      .attr("height", h) 
      .style ("fill", "red") 
      .style ("opacity", 0.1); 

      svg.append("rect") 
      .attr("class", "overlay") 
      .attr("x",1133) 
      .attr("width", 857) 
      .attr("height", h) 
      .style ("fill", "yellow") 
      .style ("opacity", 0.1); 

      svg.append("rect") 
      .attr("class", "overlay") 
      .attr("x",1990) 
      .attr("width", 134) 
      .attr("height", h) 
      .style ("fill", "green") 
      .style ("opacity", 0.1); 

      svg.append("rect") 
      .attr("class", "overlay") 
      .attr("x",5) 
      .attr("y", 10) 
      .attr("width", 485) 
      .attr("height", 155) 
      .style ("fill", "aliceblue") 
//    .style ("opacity", 0.1); 

      svg.append("g") 
      .attr("class", "x axis") 
      .attr("transform", "translate(0," + h + ")") 
      .call(xAxis); 

      svg.append("g") 
      .attr("class", "y axis") 
      .call(yAxis) 
      //.call(bars); 

      var bars = svg.selectAll(".bar") 
      .data(items) 
      .enter() 
      .append("rect") 
      .attr("class", function(d, i) {return "bar " + d.character;}) 
      .attr("x", function(d, i) {return d.starting_line;}) 
      .attr("y", function(d, i) {return y(d.location);}) 
      .attr("width", function(d, i) {return d.duration;}) 
      .attr("height", 15) 
      .style("fill", function(d,i) {return "#" + d.color;}) 
      .call(tip) 
      .on('mouseover', tip.show) 
      .on('mouseout', tip.hide); 


        var listCharacters = d3.set(items.map(function(d) {return d.character})).values(); 

        listColors = []; 

        listPositions = []; 

        for (l = 0; l < listCharacters.length; l++) { 
         for (var key in items) { 
          if (listCharacters[l] === items[key].character) { 
           if (listColors.indexOf(items[key].color) > -1) {} else { 
            listColors.push(items[key].color); 
            var xlegend = (Math.floor(l/10) * 100); 
            var ycounter; 
            var ylegend; 
            var oldxlegend; 

            if (l === 0) { 
             ycounter = 1; 
            } 

            if (ycounter < 10) { 
             listPositions.push(ycounter * 15); 
             ycounter++; 
            } else { 
             listPositions.push(ycounter * 15); 
             ycounter = 1; 
            } 

           } 
          } else {} 
         } 
        } 


console.log(listCharacters); 
console.log(listColors); 
console.log(listPositions); 

      var legend = svg.selectAll(".legend") 
      .data(listCharacters) 
      .enter().append("g") 
      .attr("class", "legend") 
      .attr("transform", function(d, i) { return "translate(" + (Math.floor(i/10) * 105) + ", " + listPositions[i] + ")"; }) 

      legend.append("rect") 
      .attr("x", 10) 
      .attr("width", 10) 
      .attr("height", 10) 
      .attr("fill", function(d,i) {return "#" + listColors[i];}); 



      legend.append("text") 
      .attr("x", 22) 
      .attr("y", 5) 
      .attr("dy", ".35em") 
      .text(function(d,i){ return listCharacters[i]}); 

      svg.append("g") 
      .attr("class", "legend") 
      .call(legend) 

     }); 

. 항목과 방향에 대한 바의 두 통화에서

  var tip = d3.tip() 
      .attr('class', 'd3-tip') 
      .offset([-10, 0]) 
      .html(function(d, i) {return console.log(d.line_text) + "<strong>(" + d.starting_line + ")</strong> <i>" + d.character + "</i>: " + d.line_text;}); 

      var stage_tip = d3.tip() 
      .attr('class', 'd3-tip') 
      .offset([-10, 0]) 
      .html(function(d, i) {return console.log(d.direction_text) + "<strong>(" + d.line_after + ") s.d.</strong> <i>" + d.direction_text;}); 

을 그리고 선언 : 툴팁은 다음과 같은 두 개의 VAR 선언에 의해 정의된다

var stage_bars = svg.selectAll(".stage_bar") 
     .data(directions) 
     .enter() 
     .append("rect") 
     .attr("class", "overlay") 
     .attr("class", function(d, i) {return "stage_bar " + d.direction_text;}) 
     .attr("x", function(d, i) {return d.line_after;}) 
     .attr("width", 1) 
     .attr("height", h) 
     .style("fill", "black") 
     .style("opacity",0.3) 
     .call(stage_tip) 
     .on('mouseover', stage_tip.show) 
     .on('mouseout',stage_tip.hide); 

    var bars = svg.selectAll(".bar") 
     .data(items) 
     .enter() 
     .append("rect") 
     .attr("class", function(d, i) {return "bar " + d.character;}) 
     .attr("x", function(d, i) {return d.starting_line;}) 
     .attr("y", function(d, i) {return y(d.location);}) 
     .attr("width", function(d, i) {return d.duration;}) 
     .attr("height", 15) 
     .style("fill", function(d,i) {return "#" + d.color;}) 
     .call(tip) 
     .on('mouseover', tip.show) 
     .on('mouseout', tip.hide); 

내 문제입니다 바의 툴팁 (사람 해당 통화 팁)은 작동하지만 stage_bars (스테이지 팁이라고하는 것)에 대한 툴팁은 그렇지 않습니다. 문제는 tip과 stage_tip에서 같은 클래스를 호출하지만 tip이 아닌 다른 것으로 선언하면 아무런 차이가 없다고 생각합니다.

제 질문은 두 가지 툴팁을 모두 사용하는 것입니다. 이것은 가능한가? 나는 그것을하는 것처럼 보이지만 코드가 명확하지 않은 예제를 보았습니다.

+0

제 3 자 플러그인입니까? 그것이 내가 생각하는 것 인 경우, 그것은 '팁'이어야합니다. 왜 당신은 자신의 툴팁을 만들지 않습니까? 자신과 같은 상황에 적응하는 것이 쉽고 편합니다. –

답변

0

실제로 솔루션은 실제로 간단합니다. stage_bars 변수를 svg.append ("rect") 문 앞에 선언 했으므로 스테이지 방향을 나타내는 선이 다른 객체 아래에 있음을 의미합니다. 따라서 마우스를 툴팁 위에 올려 놓지 마십시오. 이러한 svg.append 문 다음에 var 선언을 자리로 이동하면 문제가 해결됩니다.

관련 문제