2012-01-08 38 views
2

我的問題是圍繞必須單擊按鈕兩次來填充所需的結果。我正在使用HighCharts繪製圖表,但在正確顯示圖表之前必須調用updateTime3Period()函數兩次。下面我已經包括了我的所有代碼,除了updateTime6Period()函數以外,因爲它在大多數方面與updateTime3Period()相同。他們都有同樣的問題。我想點擊一下按鈕,然後填充所需的圖表。我爲這篇冗長的文章道歉。先謝謝你!注意:如果updateTime3Period()被點擊兩次,這會起作用。onClick需要兩次點擊來填充結果,HighCharts

HTML

<div id="timelinePeriods"> 
    <ul class="timeSelection"> 
     <li><a href="#" onclick="updateTime6Period();" > Past 6 Periods</a></li> 
     <li><a href="#" onclick="updateTime3Period();"> Past 3 Periods</a></li> 
    </ul> 
</div> 

JS/AJAX爲updateTime3Period:被稱爲上面

function updateTime3Period() { 
    timeFrameUpdate = 'Past 3 Periods'; 
    displayParam(); 

    $.ajax({ 
     url: 'PHP/getValues.php', 
     type: 'post', 
     data: { 
      type: "A", 
      type2: B, 
      type3: C, 
      type4: D, 
      type5: E, 
      type6: F, 
      type7: "getChartCurr" 
     }, 

     success: function(response2) { 
      obj2 = JSON.parse(response2); 

     } 

    }); 

    $.ajax({ 
     url: 'PHP/getValues.php', 
     type: 'post', 
     data: { 
      type: "A", 
      type2: B, 
      type3: C, 
      type4: D, 
      type5: E, 
      type6: F, 
      type7: "getChartPrev" 
     }, 

     success: function(response3) { 
      obj3 = JSON.parse(response3); 

     } 

    }); 

    updateCharts(obj2, obj3, measureUpdate); 
} 

功能(相同的文件)

function displayParam() { 
    document.getElementById("params").innerHTML = timeFrameUpdate; 
} 

function updateCharts(data1, data2, measureData) { 

} else if (timeFrameUpdate == 'Past 6 Periods') { 
    updateSixMonthPeriodChart(data1, data2, measureData); 
} else if (timeFrameUpdate == 'Past 3 Periods') { 
    updateThreeMonthPeriodChart(data1, data2, measureData); 
} 

HighCharts圖

function updateThreeMonthPeriodChart(data1, data2, measureData) { 

    var measureValue = data1["graph"]; 
    var measureValuePrev = data2["graphPrev"]; 
    var changeValue = new Array(); 

    for (i = 0; i < 3; i++) { 
     measureValue[i] = parseInt(measureValue[i]); 
     changeValue[i] = (parseInt(measureValue[i]) - parseInt(measureValuePrev[i]))/parseInt(measureValuePrev[i]) 
    } 
    var chart1; // globally available 
    $(document).ready(function() { 
     chart1 = new Highcharts.Chart({ 
      chart: { 
       renderTo: 'myChart', 
      }, 
      title: { 
       text: 'Sales and Percent Change vs. Last Year - Past 3 Periods' 
      }, 
      xAxis: { 
       categories: [1, 2, 3], 
       title: { 
        text: 'Period' 
       }, 
      }, 
      yAxis: [{ 
       labels: { //Right y-axis 
        formatter: function() { 
         return Highcharts.numberFormat(this.value, 1, '.', ',') + '%'; 
        } 
       }, 
       title: { 
        text: '% Change' 
       }, 
       opposite: true 
      }, 
      { //Left y-axis 
       labels: { 
        formatter: function() { 
         return '$' + Highcharts.numberFormat(this.value, 0, '', ','); 
        } 
       }, 
       title: { 
        text: 'Sales ($)' 
       } 
      }, 
      ], 
      series: [{ 
       name: 'Sales', 
       data: [measureValue[0], measureValue[1], measureValue[2]], 
       color: '#363534', 
       //Charcoal 
       yAxis: 1, 
       type: 'column' 
      }, 
      { 
       name: '% Change', 
       data: [changeValue[0], changeValue[1], changeValue[2]], 
       color: '#E17000', 
       //Pumpkin 
       //yAxis: 2, 
       type: 'spline' 
      }] 
     }); 
    }); 
} 

回答

0

這可能是你的Ajax請求的原因,它並沒有在第一click.in下一個完整單擊它從抓得到的結果和運行速度更快。

試試這個:

function updateTime3Period() { 
    timeFrameUpdate = 'Past 3 Periods'; 
    displayParam(); 

     $.ajax({ 
     url : 'PHP/getValues.php', 
     type : 'post', 
     data : { 
      type : "A", 
      type2 : B, 
      type3 : C, 
      type4 : D, 
      type5 : E, 
      type6 : F, 
      type7 : "getChartCurr" 
     }, 

     success : function (response2) { 
      obj2 = JSON.parse(response2); 
        $.ajax({ 
          url : 'PHP/getValues.php', 
          type : 'post', 
          data : { 
         type : "A", 
         type2 : B, 
         type3 : C, 
         type4 : D, 
         type5 : E, 
         type6 : F, 
         type7 : "getChartPrev" 
         }, 
          success : function (response3) { 
         obj3 = JSON.parse(response3); 
         updateCharts(obj2,obj3,measureUpdate); 
          } 
        }); 
     } 

    }); 
} 
+0

謝謝!只需點擊一下即可完美工作。做得好。 – 2012-01-08 05:08:33

+0

很高興聽到,歡迎您 – MDP 2012-01-08 05:14:08