Highcharts工具提示格式化程序

时间:2013-06-07 19:12:23

标签: highcharts tooltip

对于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'
                }
            }

        }]
    });
});

1 个答案:

答案 0 :(得分:39)

虽然我不确定为什么你的解决方案不起作用,但我可以提出另一种解决方案。

tooltip formatter功能可以访问许多不同的参数。您可以使用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;
}