2012-02-20 26 views
0

我使用這個特殊的FLOT例如建立與我們的一些數據的圖表上:獲取默認的圖表顯示FLOT圖表

http://people.iola.dk/olau/flot/examples/ajax.html

我已經修改了標記位 - 而不是按鈕,我有不同的JSON文件複選框,以這種方式:

<span> 
    <input class="fetchSeries" type="checkbox"> Service One 
    <a href="@Server.MapPath("~/Scripts/flot/servicedataone.json")"></a> 
</span> 

我想有一個與JSON文件加載頁面時的一個圖表預填充。我嘗試了各種各樣的東西,包括在頁面加載時默認選中其中一個複選框,但沒有運氣。

任何幫助表示讚賞!下面的代碼:

<script type="text/javascript"> 
    $(document).ready(function() { 
     var options = { 
      lines: { show: true }, 
      points: { show: true }, 
      yaxis: { 
      }, 
      xaxis: { 
      mode: "time" 
      }, 
      grid: { hoverable: true } 
     }; 
     var data = []; 
     var placeholder = $("#placeholder"); 

     $.plot(placeholder, data, options); 

     // fetch one series, adding to what we got 
     var alreadyFetched = {}; 

     $("input.fetchSeries").click(function() { 
      var button = $(this); 

      // find the URL in the link right next to us 
      var dataurl = button.siblings('a').attr('href'); 

      // then fetch the data with jQuery 
      function onDataReceived (series) { 
       // extract the first coordinate pair so you can see that 
       // data is now an ordinary Javascript object 
       var firstcoordinate = '(' + series.data[0][0] + ', ' + series.data[0][1] + ')'; 

       //button.siblings('span').text('Fetched ' + series.label + ', first point: ' + firstcoordinate); 

       // let's add it to our current data 
       if (!alreadyFetched[series.label]) { 
        alreadyFetched[series.label] = true; 
        data.push(series); 
       } 

       // and plot all we got 
       $.plot(placeholder, data, options); 
      } 

      $.ajax({ 
       url: dataurl, 
       method: 'GET', 
       dataType: 'json', 
       success: onDataReceived 
      }); 
     }); 
    }); 
</script> 

回答

1

這個怎麼樣 - 你設置後您$("input.fetchSeries").click,觸發它,通過調用$("input.fetchSeries:first").click()。您需要修改:first部分以匹配您實際想要啓動的任何複選框。

它應該是這樣的:http://jsfiddle.net/ryleyb/bdSrc/

+0

不,沒有工作:( – mynameisneo 2012-02-22 08:03:37

+0

我測試了這一點,做工精細:http://jsfiddle.net/ryleyb/bdSrc/唯一的區別是,在的jsfiddle我生成的數據,並從jsfiddle反彈它,因爲我沒有一個服務器來獲取數據。 – Ryley 2012-02-22 16:58:59

+0

得到它的工作..非常感謝! – mynameisneo 2012-02-28 04:37:51