2011-03-12 50 views
0

我正在嘗試構建我的第一張圖表,我儘可能將我的y軸數據拉入,但我仍然不確定如何從我的數據源構建我的x軸。我目前的代碼硬編碼值,但這些值應該來自結果[i]。季節。這是我的代碼,有人可以幫忙嗎?我的第一張高圖

var chart; 

$(document).ready(function() { 



    chart = new Highcharts.Chart({ 
     chart: { 
      renderTo: 'graphcontainer', 
      defaultSeriesType: 'line', 
      events: { 
       load: requestData 
      } 
     }, 
     legend: { 
      enabled: false 
     }, 
     colors: [ 
      '#c9243f' 
     ], 
     credits: { 
      enabled: false 
     }, 
     title: { 
      text: 'Regular Season Wins', 

      style: { 
       fontWeight: 'normal' 
      } 
     }, 
     xAxis: { 
      categories: [ 
         '92', 
         '93', 
         '94', 
         '09', 
         '10', 
         '11', 
         '12', 
         '13', 
         '14', 
         '15' 
        ], 
      title: { 
       text: 'Season' 
      } 
     }, 
     yAxis: { 
      min: 0, 
      max: 16, 
      tickInterval: 2, 
      title: { 
       text: 'Games Won' 
      } 
     }, 
     tooltip: { 
      formatter: function() { 
       return '' + 
          this.x + ': ' + this.y + ' wins'; 
      } 
     }, 
     series: [{ data: []}] 
    }); 
}); 

function requestData() 
{ 
    $.post('/Gameplan/Team/GetRegularSeasonWins', { strTeam: "Atlanta Falcons", strSeason: "2016" }, function (result) { 

     $.each(result, function (i) { 
      chart.series[0].addPoint(result[i].Wins, false); 
     }); 

     chart.redraw(); 
    }); 
} 

我的數據訪問:

public List<RegularSeasonWins> GetRegularSeasonWins(string strTeam, string strSeason) 
    { 
     List<RegularSeasonWins> lstRegularSeasonWins = new List<RegularSeasonWins>(); 

     lstRegularSeasonWins = (from w in _database.RegularSeasonWins(strTeam, strSeason) 

           select new RegularSeasonWins 
           { 
            Team = w.Team, 
            Coach = w.coach, 
            Season = w.seasonshort, 
            Wins = w.won 
           }).ToList(); 

     return lstRegularSeasonWins;              
    } 
+0

你在做asp.net mvc嗎?使用asp.net-mvc標籤,然後 – gideon 2011-03-13 07:55:45

回答

1

您應該能夠通過修改代碼來設置該系列如下:

function requestData() { 
    $.post('/Gameplan/Team/GetRegularSeasonWins', { 
     strTeam: "Atlanta Falcons", 
     strSeason: "2016" 
    }, function (result) { 

     var categories = []; 
     $.each(result, function (i) { 
      chart.series[0].addPoint(result[i].Wins, false); 

      categories.push(results[i].Season) 
     }); 
     chart.xAxis[0].setCategories(categories); 
     chart.redraw(); 
    }); 
} 

但在一般情況下,如果你有麻煩與highcharts,他們有great documentation解釋每個配置選項和功能,以及JSFiddle的例子。

+0

我已經嘗試過類似於此的東西,但是這不會拉動四季,我只是從0-9取代x軸。 – user517406 2011-03-13 11:02:29

+0

結果中實際存儲了什麼值[i]。季節。你確定他們是'92','93','94'等嗎?或者他們是92,93,94,...? – NT3RP 2011-03-13 15:51:53

+0

我只是在玩一個[示例highcharts小提琴](http://jsfiddle.net/nt3r/CAKQH/9396/)。如果返回的數據是'92,93,94',我認爲編輯應該修復一些東西('chart.xAxis [0] .setCategories(categories)') – NT3RP 2011-03-13 15:58:07

相關問題