2016-06-11 3 views
2

.js 프로그래밍이 처음이고 자바 스크립트에서 Google 차트에 문제가 있습니다. 일부 데이터를 입력하는 간단한 HTML 양식을 사용하고 있습니다. 버튼을 클릭하면 스크립트가 실행됩니다.버튼 클릭시 Google 차트 다시 그리기/javascript

모든 단추를 클릭 할 때마다 코드를 다시 그리거나 업데이트하는 것이 좋습니다.

문제는 처음으로 Google 라이브러리가 처음로드를 완료하면 내 스크립트가로드 명령이 실행되는 지점에서 실행이 중지된다는 것입니다.

이 게시물 Google chart redraw onclick에서 해결책을 시도했지만 제대로 작동하지 않았습니다.

파일은 다음과 같습니다

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
</head> 
 
<body> 
 
<form id="form"> 
 
<!------------------------------------------------------------------------------------------------------> 
 
\t <div id="gender"> 
 
\t \t <h3>Gender</h3> 
 
\t \t <fieldset> 
 
\t \t \t <input type="radio" id="male" name="gender_select" class="optGender" value="männlich" checked><span class="optGender">male</span></input><br> 
 
\t \t \t <input type="radio" id="female" name="gender_select" class="optGender" value="weiblich" ><span class="optGender">female</span></input><br> 
 
\t \t \t Bodyweight: <input type="text" id="bodyweight"></input> 
 
\t \t </fieldset> 
 
\t </div> 
 
<!------------------------------------------------------------------------------------------------------> 
 
\t <div id="chart"> 
 
\t </div> 
 
<!------------------------------------------------------------------------------------------------------> 
 
\t <div id="squat"> 
 
\t \t <p> 
 
\t \t \t <h3>Squat</h3> 
 
\t \t \t <fieldset> 
 
\t \t \t \t <span class="spreps">Reps</span><input type="text" id="reps_squat" required><br> 
 
\t \t \t \t <span class="spweight">Weight</span><input type="text" id="weight_squat" required><br> 
 
\t \t \t \t <span class="spmax">Calculated Max</span><input type="text" id="total_squat" readonly><br> 
 
\t \t \t </fieldset> 
 
\t \t </p> 
 
\t </div> 
 
<!------------------------------------------------------------------------------------------------------> 
 

 
<!------------------------------------------------------------------------------------------------------> 
 
\t <div id="bench"> 
 
\t \t <p> \t 
 
\t \t \t <h3>Benchpress</h3> 
 
\t \t \t <fieldset> 
 
\t \t \t \t <span class="spreps">Reps</span><input type="text" id="reps_bench" required><br> 
 
\t \t \t \t <span class="spweight">Weight</span><input type="text" id="weight_bench" required><br> 
 
\t \t \t \t <span class="spmax">Calculated Max</span><input type="text" id="total_bench" readonly><br> 
 
\t \t \t </fieldset> 
 
\t \t </p> 
 
\t </div> 
 
<!------------------------------------------------------------------------------------------------------> 
 

 
<!------------------------------------------------------------------------------------------------------> 
 
\t <div id="deadlift"> 
 
\t \t <p> 
 
\t \t \t <h3>Deadlift</h3> 
 
\t \t \t <fieldset> 
 
\t \t \t \t <span class="spreps">Reps</span><input type="text" id="reps_deadlift" required><br> 
 
\t \t \t \t <span class="spweight">Weight</span><input type="text" id="weight_deadlift" required><br> 
 
\t \t \t \t <span class="spmax">Calculated Max</span><input type="text" id="total_deadlift" readonly><br> 
 
\t \t \t </fieldset> 
 
\t \t </p> 
 
\t </div> \t 
 
<!------------------------------------------------------------------------------------------------------> 
 
\t <p> 
 
\t \t <input type="button" alt="Berechnen" class="button" id="btn_calcTotal" value="Total berechnen" onclick="initialize()"> 
 
\t </p> 
 
<!------------------------------------------------------------------------------------------------------> 
 
\t <div id="total"> 
 
\t \t <p> 
 
\t \t <h3>calculated Total</h3> 
 
\t \t \t <input type="text" id="total_all" readonly> 
 

 
\t \t </p> 
 
\t </div> 
 

 
<!------------------------------------------------------------------------------------------------------> 
 
\t <!--Load the AJAX API--> 
 
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
 
<!------------------------------------------------------------------------------------------------------> 
 
\t <script> 
 
\t \t function initialize() { 
 
\t // delete old entries 
 
\t document.getElementById("total_squat").value = ""; 
 
\t document.getElementById("total_bench").value = ""; 
 
\t document.getElementById("total_deadlift").value = ""; 
 
\t document.getElementById("total_all").value = ""; 
 
\t 
 
\t // read weight 
 
\t var dWeight = parseFloat(document.getElementById("bodyweight").value); 
 
\t 
 
\t // read gender & get weightclass 
 
\t var sGender = ""; 
 
\t \t if (document.getElementById("male").checked == true) { 
 
\t \t \t sGender = document.getElementById("male").value; 
 
\t \t } 
 
\t \t else { 
 
\t \t \t sGender = document.getElementById("female").value; 
 
\t \t } 
 
\t 
 
\t // read exercise values 
 
\t var irepsSquat = parseFloat(document.getElementById("reps_squat").value.replace(',','.')); 
 
\t var dweightSquat = parseFloat(document.getElementById("weight_squat").value.replace(',','.')); 
 

 
\t var irepsBench = parseFloat(document.getElementById("reps_bench").value.replace(',','.')); 
 
\t var dweightBench = parseFloat(document.getElementById("weight_bench").value.replace(',','.')); 
 

 
\t var irepsDeadlift = parseFloat(document.getElementById("reps_deadlift").value.replace(',','.')); 
 
\t var dweightDeadlift = parseFloat(document.getElementById("weight_deadlift").value.replace(',','.')); 
 
\t 
 
\t // check if reps & weight are valid -> calculate (Squat) 
 
\t if (check(irepsSquat) || check(dweightSquat)) { 
 
\t \t window.alert("Bitte Wiederholungen & Übungsgewicht eintragen"); 
 
\t \t return; 
 
\t } 
 
\t else { 
 
\t \t var total_sq = calculate(irepsSquat, dweightSquat); 
 
\t } 
 
\t // check if reps & weight are valid -> calculate (Benchpress) 
 
\t if (check(irepsBench) || check(dweightBench)){ 
 
\t \t window.alert("Bitte Wiederholungen & Übungsgewicht eintragen"); 
 
\t \t return; 
 
\t } 
 
\t else { 
 
\t \t var total_bp = calculate(irepsBench, dweightBench); 
 
\t } 
 
\t // check if reps & weight are valid -> calculate (Deadlift) 
 
\t if (check(irepsBench) || check(dweightBench)){ 
 
\t \t window.alert("Bitte Wiederholungen & Übungsgewicht eintragen"); 
 
\t \t return; 
 
\t } 
 
\t else { 
 
\t \t var total_dl = calculate(irepsDeadlift, dweightDeadlift); 
 
\t } 
 
\t 
 
\t // calculate total & wilk points -> write it 
 
\t document.getElementById("total_squat").value = total_sq; 
 
\t document.getElementById("total_bench").value = total_bp; 
 
\t document.getElementById("total_deadlift").value = total_dl; 
 
\t 
 
\t var total_all = (parseFloat(total_sq) + parseFloat(total_bp) + parseFloat(total_dl)).toFixed(2); 
 
\t document.getElementById("total_all").value = total_all; 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t 
 
\t // Load the Visualization API 
 
    google.charts.load("current", {packages:['corechart']}); 
 
\t 
 
    // Set a callback to run when the Google Visualization API is loaded. 
 
    \t google.charts.setOnLoadCallback(loadChartData(total_all)); 
 
} 
 

 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Checks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 
function check(val) { 
 
\t if (isNaN(val)) \t { 
 
\t \t return true; 
 
\t } 
 
\t else { 
 
\t \t return false; 
 
\t } 
 
} 
 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 

 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ calculate 1RM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 
function calculate(iReps, dWeightE) { 
 
\t \t \t return (dWeightE/(1.0278-(0.0278*iReps))).toFixed(2); 
 
} 
 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 

 
function loadChartData(total_all) { 
 
\t var dnorm1 = 300; 
 
\t var dnorm2 = 450; 
 
\t 
 
\t // Create the data table 
 
\t var data = new google.visualization.DataTable(); 
 
\t data.addColumn('string', 'label'); 
 
\t data.addColumn('number', 'Weight'); 
 
\t data.addRows([ 
 
\t ['Own Total', parseFloat(total_all)], 
 
\t ['Kadernorm 1', parseFloat(dnorm1)], 
 
\t ['Kadernorm 2', parseFloat(dnorm2)] 
 
\t ]); 
 
\t drawChart(data); 
 
} 
 

 
function drawChart(data) { 
 
\t // Instantiate new chart 
 
\t var chart = new google.visualization.ColumnChart(document.getElementById('chart')); 
 
\t 
 
\t // Set chart options 
 
\t var options = {'title':'Comparison of own Total vs. national team levels in kg', 
 
\t \t \t \t 'width':700, 
 
\t \t \t \t 'height':400, 
 
\t \t \t \t }; 
 

 
\t // Draw chart, passing in some options 
 
\t chart.draw(data, options); 
 

 
} 
 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 
\t </script> 
 
</form> 
 
</body> 
 
</html>

답변

1

setOnLoadCallback은하지 return 무엇을하는 function의 결과를 전달하는 function
에 대한 참조를합니다 ..

하지만 실제로 필요하지는 않습니다. setOnLoadCallback,210 는 load

에서 callback 포함하지만 페이지가로드

가, 나중에 차트를 다시 그릴 같은 것을 시도 할 때이 만

<script> 
    var isInit = false; 
    function initialize() { 

     ... 

     if (isInit) { 
     loadChartData(total_all); 
     } else { 
     google.charts.load('current', { 
      callback: function() { 
      isInit = true; 
      loadChartData(total_all); 
      }, 
      packages:['corechart'] 
     }); 
     } 
    } 

    ... 
+0

하이 화이트 햇 ... 호출됩니다 불행하게도 이것은 코드가 어떻게 작동 하는지를 변경하지 않습니다. 내 코드를 너의 코드로 바꿨다. 단추를 두 번째로 클릭하면 차트 자체가 다시 그려지지 않습니다 ... 아마도 문은 한 번만 수행됩니다. 차트 개체를 "파괴"할 수있는 방법이 있습니까? 또는 다른 이벤트로 차트를 새로 고침/다시 그릴 수 있습니까? – IDMatt

+0

, 대답 편집 ... – WhiteHat

+0

당신이 그것을하는 방법을 안다면 쉽게 종류 : D 조 남자, 그것은 지금 작동 – IDMatt