15

对于this.x,当我通过代码推送数据时,我正在获取索引位置。如果我单独填充数据,如以下代码,则this.x返回正确的项目。我该如何解决这个问题?

作品

xAxis: {
    categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},

series: [{
    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]        
}]

索引位置被推到this.x这里

var points = [{
    Name: 'good',
    Y: '15000'
}, {
    Name: 'baad',
    Y: '3000'
}, {
    Name: 'wow',
    Y: '2000'
}];

var chartData = {
    GetChartSeries: function (points, name) {

        var seriesData = [];
        if (points != null && points != 'undefined') {
            for (i=0; i<points.length; i++) {
                seriesData.push({
                    name: ""+points[i].Name,
                    y: parseFloat(points[i].Y)
                    //,color: ''
                });
            }
        }
        return seriesData;
    }
};


$(function () {
    $('#container').highcharts({
        chart: {
            type: 'column',
            margin: [ 50, 50, 100, 80],
            borderColor: '#A4A4A4',
            borderRadius: 5,
            borderWidth: 2
        },
        legend: { 
            enabled: false 
        },
        title: {
            text: 'Graduation Year Breakdown'
        },
        colors: ['#790000'],
        legend: {
            enabled: false
        },
        plotOptions: {
            series: {
                /*
                dataLabels: {
                    enabled: true,
                    color: 'red'
                },
                */
                borderRadius: 3,
                colorByPoint: true
            }
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ Highcharts.numberFormat(this.y, 0) +'</b><br/>'+
                    'in year: '+ this.x;
            }
        },
        xAxis: {
            categories: [],
            labels: {
                rotation: -45,
                align: 'right',
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Number of Students'
            }
        },
        series: [{
            //name: 'Population',
            data: chartData.GetChartSeries(points, ""),//[4000, 3400, 2000, 34000, 120000],                 
            dataLabels: {
                enabled: true,
                //rotation: -90,
                color: '#4F4F4F',
                align: 'center',//'right',
                //x: 4,
                //y: 10,
                style: {
                    fontSize: '12px',
                    //fontWeight: 'bold',
                    fontFamily: 'Verdana, sans-serif'
                }
            }

        }]
    });
});
4

1 回答 1

51

虽然我不确定您的解决方案为什么不起作用,但我可以提出一个替代解决方案。

工具提示格式化程序函数可以访问许多不同的参数。而不是this.x,您可以使用this.point.name.

例如:

formatter: function() {
    // If you want to see what is available in the formatter, you can
    // examine the `this` variable.
    //     console.log(this);

    return '<b>'+ Highcharts.numberFormat(this.y, 0) +'</b><br/>'+
        'in year: '+ this.point.name;
}
于 2013-06-09T16:21:22.027 回答