2012-09-11 655 views
0

你好我有一些問題,當我從Jquery加載時將數據部分加載到Flot中,但是當我手動輸入數據值時它工作正常。Flot with JSON not working properly

<script type="text/javascript"> 

    $(document).ready(function() { 
     var kcArray = []; 


     function onDataReceived(series) { 

      $.each(series, function(i, marker) { 
      var dq = [[new Date(1346976001000), 3904.8408203125], [new Date(1346976011000), 3919.70678710938], [new Date(1346976021000), 3933.97265625], [new Date(1346976031000), 3956.583984375], [new Date(1346976041000), 3975.85083007813], [new Date(1346976051000), 3960.72924804688], [new Date(1346976061000), 3962.5751953125], [new Date(1346976071000), 4001.56372070313], [new Date(1346976081000), 4005.63549804688], [new Date(1346976091000), 3994.11499023438], [new Date(1346976101000), 3992.95141601563], [new Date(1346976111000), 3972.32470703125], [new Date(1346976121000), 3957.19848632813], [new Date(1346976131000), 3943.83935546875], [new Date(1346976141000), 3939.63305664063], [new Date(1346976151000), 3944.61474609375], [new Date(1346976161000), 3960.82592773438], [new Date(1346976171000), 3967.658203125], [new Date(1346976181000), 3975.17578125], [new Date(1346976191000), 4001.0302734375], [new Date(1346976201000), 4015.51171875], [new Date(1346976211000), 4004.38500976563], [new Date(1346976221000), 3974.08227539063], [new Date(1346976231000), 3941.20751953125], [new Date(1346976241000), 3936.21801757813], [new Date(1346976251000), 3945.08984375], [new Date(1346976261000), 3933.44970703125], [new Date(1346976271000), 3933.52319335938], [new Date(1346976281000), 3939.19970703125], [new Date(1346976291000), 3954.7646484375], [new Date(1346976301000), 3962.11254882813], [new Date(1346976311000), 3956.17358398438], [new Date(00), 3992.56396484375], [new Date(1346976331000), 4008.015625], [new Date(1346976341000), 4039.65112304688], [new Date(1346976351000), 4045.77319335938], [new Date(1346976361000), 4010.93359375], [new Date(1346976371000), 3990.35888671875], [new Date(1346976381000), 3984.05810546875], [new Date(1346976391000), 3986.58666992188], [new Date(1346976401000), 3988.79345703125], [new Date(1346976411000), 3996.03857421875], [new Date(1346976421000), 4017.88989257813], [new Date(1346976431000), 4011.49584960938], [new Date(1346976441000), 4031.03857421875], [new Date(1346976451000), 4038.71362304688], [new Date(1346976461000), 4048.50927734375], [new Date(1346976471000), 4036.84204101563], [new Date(1346976481000), 3998.3544921875], [new Date(1346976491000), 3981.95434570313], [new Date(1346976501000), 3980.75952148438], [new Date(1346976511000), 3961.21630859375], [new Date(1346976521000), 3966.2451171875], [new Date(1346976531000), 3969.21728515625], [new Date(1346976541000), 3981.84155273438], [new Date(1346976551000), 3988.89111328125], [new Date(1346976561000), 4003.67626953125], [new Date(1346976571000), 4010.57202148438], [new Date(1346976581000), 3994.47241210938], [new Date(1346976591000), 3968.3349609375], [new Date(1346976601000), 3971.16723632813], [new Date(1346976611000), 3967.21752929688], [new Date(1346976621000), 3961.94604492188], [new Date(1346976631000), 3956.22509765625], [new Date(1346976641000), 3958.71850585938], [new Date(1346976651000), 3942.47021484375], [new Date(1346976661000), 3945.68188476563], [new Date(1346976671000), 3962.16723632813], [new Date(1346976681000), 3981.14794921875], [new Date(1346976691000), 3999.26635742188], [new Date(1346976701000), 3989.92456054688], [new Date(1346976711000), 3980.75903320313], [new Date(1346976721000), 3965.29516601563], [new Date(1346976731000), 3986.69189453125], [new Date(1346976741000), 4004.6005859375], [new Date(1346976751000), 4020.123046875], [new Date(1346976761000), 4023.11865234375], [new Date(1346976771000), 4035.29150390625], [new Date(1346976781000), 4052.357421875], [new Date(1346976791000), 4038.36108398438], [new Date(1346976801000), 4062.8505859375], [new Date(1346976811000), 4064.73315429688], [new Date(1346976821000), 4051.31616210938], [new Date(1346976831000), 4021.96899414063], [new Date(1346976841000), 3997.01147460938], [new Date(1346976851000), 3977.91577148438], [new Date(1346976861000), 3974.49682617188], [new Date(1346976871000), 3961.978515625], [new Date(1346976881000), 4001.267578125], [new Date(1346976891000), 4007.3154296875], [new Date(1346976901000), 3996.1484375], [new Date(1346976911000), 4042.66772460938], [new Date(1346976921000), 4022.11743164063], [new Date(1346976931000), 4019.2509765625], [new Date(1346976941000), 4023.57934570313], [new Date(1346976951000), 3999.955078125], [new Date(1346976961000), 3998.23413085938], [new Date(1346976971000), 3983.35693359375], [new Date(1346976981000), 3977.345703125], [new Date(1346976991000), 3990.88525390625]]; 
       var chartData = { label: marker.label, data: dq }; 
       kcArray.push(chartData); 
       //alert(dq); 
       //$('#content').html(marker.data); 
      }); 
      // and plot all we got 
      $.plot($('#content'), kcArray); 

     } 

     $.ajax({ 
      url: '/Charts/GetPowerChart', 
      method: 'GET', 
      dataType: 'json', 
      success: onDataReceived 
     }); 

    }); 

</script> 

每當我打開這個大我的圖形負載

然而,每當我改變DQ到marker.data沒有什麼作品標籤的作品,但數據不會在圖表上加載我手工負載中輸入的數據和marker.data是完全一樣的:

<script type="text/javascript"> 

    $(document).ready(function() { 
     var kcArray = []; 


     function onDataReceived(series) { 

      $.each(series, function(i, marker) { 
       var dq = marker.data; 
       var chartData = { label: marker.label, data: dq }; 
       kcArray.push(chartData); 
       //alert(dq); 
       //$('#content').html(marker.data); 
      }); 
      // and plot all we got 
      $.plot($('#content'), kcArray); 

     } 

     $.ajax({ 
      url: '/Charts/GetPowerChart', 
      method: 'GET', 
      dataType: 'json', 
      success: onDataReceived 
     }); 

    }); 

</script> 

有人可以幫忙嗎?

+1

最有可能的,手動DQ和marker.data看起來是一樣的,但實際上不是 - 你能告訴我們marker.data的確切內容嗎? – Ryley

回答

0

這是我如何能夠解決這個問題:

的JS:

var data24 = []; 

function onDataReceived(seriesData) { 
    //alert(seriesData.seriesData); 
    var p = $.plot(placeholder, seriesData.seriesData, options); 
} 

var fullDate = new Date(); 
var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1); 
var currentDate = twoDigitMonth + "/" + fullDate.getDate() + "/" + fullDate.getFullYear(); 


$.ajax({ 
    url: '/Charts/LineChart?SeriesToGraph=5,7,9,11&DatePull=' + currentDate, 
    method: 'GET', 
    dataType: 'json', 
    success: onDataReceived 
}); 

這裏是我做的MVC方:

public JsonResult LineChart(string SeriesToGraph, string DatePull) { 
    chartJson js = new chartJson(); 
    IList<testData> dt = new List<testData>(); 

    //string DatePull = "09-12-2012";    
    //string SeriesToGraph = "1,2,3,4"; 

    foreach (var series in SeriesToGraph.Split(',')) { 
    IList<clsGraphData> dc = _mainRepository.graphRepository.GetGraphData(int.Parse(series), DatePull); 
    IList<string[]> rw = new List<string[]>(); 

    string Label = string.Empty; 

    foreach (var item in dc) { 
     Label = item.Name; 
     rw.Add(new string[] { DateTime.Parse(item.DatapointDate.ToString(), System.Globalization.CultureInfo.InvariantCulture).Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture), item.DatapointValue.ToString() }); 
    } 

    testData data = new testData(); 
    data.label = Label; 
    data.data = rw; 

    dt.Add(data); 
    } 
    js.seriesData = dt; 

    return Json(js, JsonRequestBehavior.AllowGet); 
} 
相關問題