0

我正在尝试构建我的第一个图表,我已经将我的 y 轴数据拉入,但我仍然不确定如何从我的数据源构建我的 x 轴。我当前的代码硬编码了其中的值,但这些值应该来自 result[i].Season。这是我的代码,有人可以帮忙吗?

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;                                                     
    }
4

1 回答 1

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 时遇到问题,他们有很好的文档解释了每个可用的配置选项和功能,以及 JSFiddle 示例。

于 2011-03-13T07:54:04.990 回答