Javascript y 轴的 Highcharts 文本标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4987457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:05:53  来源:igfitidea点击:

Highcharts text labels for y-axis

javascripthighcharts

提问by Wesley Tansey

I'm using Highcharts and would like to display a simple column graph, but instead of using numeric values for the y-axis, I would like to use text values.
For example, instead of [0,5,10,15,20]I would like to use [Very Low,Low,Medium,High,Very High].

我正在使用 Highcharts 并想显示一个简单的柱状图,但我想使用文本值而不是 y 轴的数值。
例如,[0,5,10,15,20]我想使用[Very Low,Low,Medium,High,Very High].

I noticed it's somewhat possible to do this with plot bands, but that still shows the numeric y-axis labels and just puts the text beside them. I want to only show the text labels.

我注意到使用绘图带可以做到这一点,但这仍然显示数字 y 轴标签,只是将文本放在它们旁边。我只想显示文本标签。

回答by NT3RP

You can change the labels by using a label formatter. Assuming your data is formed appropriately, you can do something like the following:

您可以使用标签格式化程序更改标签。假设您的数据格式正确,您可以执行以下操作:

var yourLabels = ["Very Low", "Low", "Medium", "High", "Very High"];
var yourChart = new Highcharts.Chart({
    //...
    yAxis: {        
        labels: {
            formatter: function() {
                return yourLabels[this.value];
            }
        }
    }
    //...
});

回答by Ricardo Alvaro Lohmann

Declare an object which will be used to switch the values you want to change, like the following.

声明一个对象,用于切换要更改的值,如下所示。

var change = {
    0: 'Very Low',
    5: 'Low',
    10: 'Medium',
    15: 'High',
    20: 'Very High'
};

Then on your chart options use labels formatter to switch it.

然后在您的图表选项上使用标签格式化程序来切换它。

yAxis: {
    labels: {
        formatter: function() {
            var value = change[this.value];
            return value !== 'undefined' ? value : this.value;
        }
    }
}