2016-06-15 2 views
2

업데이트 :클릭 이벤트 핸들러 함수에 jQuery 변수 추가

전체 스크립트.

저는 HighCharts를 사용하여 그래프를 생성하기 위해 일부 Json 데이터를 처리하는 스크립트를 가지고 있습니다.

[{"name":"Room","data":[343,405,406,407]}, 
{"name":"Temp C","data":[50,50,50,50]}, 
{"name":"UniqueID","data": 
    ["GLAZH03431464336298", 
    "GLAZH04051465483111", 
    "GLAZH04061464783558", 
    "GLAZH04071465484869"] 
}] 

내 스크립트에서 나는 다음과 같은 한 :

$(function() { 
    var categories = []; 
    var data = []; 
    var uniqueid = []; 
    var chart; 

    $(document).ready(function() { 
     $.getJSON("chart.php", function(json) { 
      $.each(json, function(i, el) { 
       if (el.name == "Room") 
        categories = el.data; 
       else if (el.name == "UniqueID") { 
        uniqueid = el.data; 
       } else data.push(el); 
      }); 

      console.log(uniqueid, "UniqueID"); 

      $('#container1').highcharts({ 
       chart: { 
        renderTo: 'container', 
        type: 'line', 
        zoomType: 'x', 
        marginTop: 40, 
        marginRight: 30, 
        marginBottom: 50, 
        plotBackgroundColor: '#FCFFC5' 
       }, 
       title: { 
        text: '<?php echo $_SESSION[' 
        _amember_user '][' 
        hotel '];?> Room hot water temperatures ', 
        x: -20, //center 
        style: { 
         fontFamily: 'Tahoma', 
         color: '#000000', 
         fontWeight: 'bold', 
         fontSize: '10px' 
        } 
       }, 
       subtitle: { 
        text: 'Zoom: hold cursor over chart, hold left mouse button and drag, release button', 
        x: -20 
       }, 
       xAxis: { 
        categories: categories, 

        labels: { 
         enabled: false 
        } 
       }, 

       yAxis: { 
        plotLines: [{ 
         value: '<?php echo $row_Rooms[' 
         Hotmax '];?>', 
         color: '#FF0000', 
         width: 1, 
         zIndex: 10, 
         label: { 
          text: 'Maximum <?php echo $row_Rooms[' 
          Hotmax '];?> °C', 
          align: 'center', 
          x: -10, 
          y: -5, 
          style: { 
           color: '#FF0000' 
          } 
         } 
        }, { 
         value: '<?php echo $row_Rooms[' 
         HotMin '];?>', 
         color: '#0000CC', 
         width: 1, 
         zIndex: 10, 
         label: { 
          text: 'Minimum <?php echo $row_Rooms[' 
          HotMin '];?> °C', 
          align: 'center', 
          x: -10, 
          y: 20, 
          style: { 
           color: '#0000CC' 
          } 
         } 
        }], 

        showFirstLabel: false, 
        lineColor: '#999', 
        lineWidth: 0.2, 
        tickColor: '#666', 
        tickWidth: 1, 
        tickLength: 2, 
        tickInterval: 10, 
        gridLineColor: '#ddd', 
        title: { 
         text: 'Temperature °C', 
         style: { 
          fontFamily: 'Tahoma', 
          color: '#000000', 
          fontWeight: 'bold', 
          fontSize: '10px' 
         } 
        }, 
       }, 

       legend: { 
        enabled: false, 
        itemStyle: { 
         font: '11px Trebuchet MS, Verdana, sans-serif', 
         color: '#000000' 
        }, 
        itemHoverStyle: { 
         color: '#000000' 
        }, 
        itemHiddenStyle: { 
         color: '#444' 
        } 
       }, 

       colors: [ 
        '#009900', 

       ], 

       plotOptions: { 
        style: { 
         textShadow: false 
        }, 
        column: { 
         color: '#ff00ff' 
        }, 
        series: { 

         cursor: 'pointer', 
         point: { 
          events: { 
           click: function() { 
            window.top.location.href = "water.php?room=" 
            this.uniqueid; 
            console.log(uniqueid); 
           } 
          } 
         }, 
         lineWidth: 1, 
         dataLabels: { 
          enabled: false, 
          rotation: 0, 
          color: '#000000', 
          align: 'center', 
          //format: '{point.y:.1f}', // one decimal 
          y: 0, // 10 pixels down from the top 
          style: { 
           textShadow: false, 
           fontSize: '10px', 
           fontFamily: 'Verdana, sans-serif' 
          } 
         } 
        } 

       }, 

       tooltip: { 
        enabled: true, 
        crosshairs: [false, true], 
        positioner: function() { 
         return { 
          x: 5, 
          y: -5 
         }; 
        }, 
        shadow: false, 
        borderWidth: 0, 
        backgroundColor: 'rgba(255,255,255,0.8)', 

        formatter: function() { 
         return 'Room: <b>' + this.x + 
          '</b> is <b>' + this.y + ' °C</b>'; 
        } 
       }, 

       credits: { 
        enabled: false 
       }, 

       series: data 
      }); 
     }); 
    }); 
}); 

는 내가하고 싶은 것이 "을 UniqueID"통과하고 URL의 끝에 그것을 추가입니다 JSON 데이터는 세 가지 요소가 있습니다 이벤트는 다음과 같이 클릭하십시오 :

click: function() { 
    window.top.location.href = "water.php?room=" + this.uniqueid; 
} 

"console.log (uniqueid,"UniqueID "); 디버거의 콘텐츠 스크립트에 체크, 나는 참조 : 내가 클릭 이벤트에 대한 내 URL의 끝을 UniqueID의 내용을 추가 할 수있는 방법

Array ["GLAZH03431464336298", 
     "GLAZH04051465483111", 
     "GLAZH04061464783558", 
     "GLAZH04071465484869"] 

내 질문입니다. 내가 무엇을하더라도 나는 이것을 작동시킬 수 없다.

+0

uniqueId는 참조 할 필요가있는 하나의 값이거나 고유 한 ID가 json 응답의 각 값에 대해 별도로 저장되어 있어야합니까? – agmcleod

+0

안녕하세요 agmcleod, 네, uniqueid 각 값에 대해 별도로 저장해야합니다. 그래프의 어느 지점을 선택 하느냐에 따라 URL은 "room =? uniqueid"로 실행되고 "$ _GET [ 'uniqueid']를 사용하여 다른 스크립트에 의해 처리됩니다. – DCJones

+0

아 좋아요. uniqueid 나 카테고리를 객체에 붙이지 않거나'var'을 가지지 않는다면 전역 변수가됩니다. 그 나쁜. $ .each 호출에서 데이터 배열을 사용하여 수행중인 작업을 보여주기 위해 게시물을 업데이트 할 수 있고 클릭을 바인딩하는 방법을 알면 추가 지원을받을 수 있습니다. – agmcleod

답변

1

코드를 jsFiddle에 다시 적어 둡니다. Javascript 줄 # 140 console.log(this.uniqueid);을 참조하십시오.이 코드는 브라우저 콘솔에 undefined이라는 문자가 인쇄되는 이유입니다.

하이 차트의 고유 ID에 온도를 연결할 수 없다는 문제가 있습니다. here 행 # 38을 참조하십시오. 다음 JSON을 구성하고 # 302 행의 Highcharts.series.data으로 설정하려고합니다. 이렇게하면 라인 # 156에서 각 포인트의 temperature, roomuniqueId의 값을 얻을 수 있습니다.

[{ 
    y: 50, 
    uniqueid: 'GLAZH03431464336298' 
}, { 
    y: 71.5, 
    uniqueid: 'GLAZH04051465483111' 
}] 
0

은 항상 당신의 데이터가 대신 그 함께 병합 객체 구축, 같은 순서 & 길이 될 것이다 결과의 데이터 세트를 감안할 때 : 당신은 속성의 방을 잡을 수 있는지, 당신의 클릭 이벤트에 다음

$.getJSON("chart.php", function(json) { 
    $.each(json, function(i, el) { 
     var collection = []; 
     if (el.name == "Room" || el.name == "UniqueID") 
      collection = el.data; 
     } else { 
      collection = el; 
     } 

     for (var j = 0; j < collection.length; j++) { 
      data[j] = data[j] || {}; 
      data[j][(el.name || "y").toLowerCase()] = collection[j]; 
     } 
    }); 

    // high charts code 
}); 

& uniqueid. 하이 차트를 사용하지 않았으므로 클릭 이벤트에서 어떤 속성이 유지 될지 확신하지 못합니다. 여기서 중단 점을 설정하고 객체를 검사 할 수 있습니다.

관련 문제