2014-03-19 19 views
0

我在處理Highchart時遇到了一個奇怪的錯誤。HighChart沒有從服務器端獲取數據?在parseJSON中出錯

它做了靜態數據都不錯,但真正得到做文章,而從服務器端做。

代碼我使用,

function GetTab1() { 
    debugger; 
    $('#dvException').html(''); 
    $('#dvException').invisible(); 
    debugger; 
    var apiUrl = '../api/Home/GetTab?dumy1=1'; 
    $.ajax({ 
     url: apiUrl, 
     type: 'GET', 
     contentType: 'application/json; charset=utf-8', 
     data: {}, 
     success: function (data) { 
      if (data.length > 0) { 
       var Mychartdata = $.parseJSON(data); 
       var chartdata = [['Firefox', 45], ['IE', 26], ['Safari', 8], ['Opera', 6], ['Others', 0]]; 
       // Radialize the colors 
       Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function (color) { 
        return { 
         radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 }, 
         stops: [ 
           [0, color], 
           [1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken 
          ] 
        }; 
       }); 

       // Build the chart 
       $('#container').highcharts({ 
        chart: { 
         plotBackgroundColor: null, 
         plotBorderWidth: null, 
         plotShadow: false 
        }, 
        title: { 
         text: 'My Title' 
        }, 
        tooltip: { 
         pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' 
        }, 
        plotOptions: { 
         pie: { 
          allowPointSelect: true, 
          cursor: 'pointer', 
          dataLabels: { 
           enabled: true, 
           color: '#000000', 
           connectorColor: '#000000', 
           formatter: function() { 
            return '<b>' + this.point.name + '</b>: ' + this.point.y; 
           } 
          } 
         } 
        }, 
        series: [{ 
         type: 'pie', 
         name: 'Browser share', 
         data: Mychartdata 
        }] 
       }); 

}; 

[HttpGet] 
     public string GetTab(int dumy1) 
     { 
      string str = string.Empty; 
      DataTable dt = new DataTable(); 
      dt = GetDataTableForPieChart(); 
      str = GetJson(dt); 

      return str; 
     } 
private DataTable GetDataTableForPieChart() 
     { 
      DataTable nt = new DataTable("new table"); 
      nt.Columns.Add("key"); 
      nt.Columns.Add("Value", typeof(int)); 
      nt.Rows.Add("Firefox", 45.0); 
      nt.Rows.Add("IE", 26.8); 
      nt.Rows.Add("Safari", 8.5); 
      nt.Rows.Add("Opera", 6.2); 
      nt.Rows.Add("Others", 0.7); 
      return nt; 
     } 

    public string GetJson(DataTable dt) 
     { 
      System.Web.Script.Serialization.JavaScriptSerializer serializer = new 

      System.Web.Script.Serialization.JavaScriptSerializer(); 
      List<Dictionary<string, object>> rows = 
       new List<Dictionary<string, object>>(); 
      Dictionary<string, object> row = null; 

      foreach (DataRow dr in dt.Rows) 
      { 
       row = new Dictionary<string, object>(); 
       foreach (DataColumn col in dt.Columns) 
       { 
        row.Add(col.ColumnName.Trim(), dr[col]); 
       } 
       rows.Add(row); 
      } 
      return serializer.Serialize(rows); 
     } 

變得很迷茫?一切都在從服務器,但圖表格式正確做工精細,即使獲取數據也沒有創造......但同樣做的所有罰款與靜態數據..

+0

我得到我的錯誤..我得到它爲我的列名...現在只是爲了解決它 –

回答

0

我只是改變了的getJSON method.Previously它不是序列化properly.Need到排除它的列名稱。

public string GetJson(DataTable dt) 
     { 
      System.Web.Script.Serialization.JavaScriptSerializer serializer = new 

      System.Web.Script.Serialization.JavaScriptSerializer(); 
      List<object[]> obj = new List<object[]>(); 

      foreach (DataRow dr in dt.Rows) 
      { 
       obj.Add(dr.ItemArray); 
      } 
      return serializer.Serialize(obj); 
     } 
相關問題