简体   繁体   English

highcharts-在yAxis中隐藏一些标签

[英]highcharts - Hide some labels in yAxis

I have a chart with a yAxis, that has a minimum of -5, and max of 5. 我有一个带有yAxis的图表,该图表的最小值为-5,最大值为5。

The yAxis has these labels: -5 , -2.5 , 0 , 2.5 , 5 . 的Y轴具有这些标签: -5-2.502.55

My config is so close - I have the right amount of grid/plot lines, but I want to hide a couple of the text labels in the yAxis ( not the actual lines relating to the label). 我的配置是如此接近-我有适当数量的网格线/绘图线,但是我想在yAxis中隐藏几个文本标签而不是与标签相关的实际行)。

In other words, I want to remove or hide the -2.5 and 2.5 labels. 换句话说,我要删除或隐藏-2.52.5标签。

I've tried various methods in the yAxis, eg step, but it's not achieving what I want. 我已经尝试了yAxis中的各种方法,例如step,但是没有达到我想要的。

yAxis: {
  labels: {
    step: 5
  }
}

JSFiddle 的jsfiddle

Any ideas how to achieve this? 任何想法如何实现这一目标?

I nearly didn't post this question because I found a (non-SO) answer - perhaps this will help others. 我几乎没有发布此问题,因为我找到了一个(非SO)答案-也许这会对其他人有所帮助。

I don't know if this is the most elegant approach for highcharts, but you can use the label formatter to achieve this. 我不知道这是否是用于高级图表的最优雅的方法,但是您可以使用标签formatter来实现。

In my case, instead of this: 就我而言,代替此:

labels: {
  formatter: function () {
    return this.value+'%';
  }
}

We can add a conditional to check the label's value, and only return something if it's what we want. 我们可以添加一个条件来检查标签的值,并且仅在需要时才返回值。 All together: 全部一起:

yAxis: {

  //...

  labels: {
    formatter: function () {

      if (this.value !== -2.5 && this.value !== 2.5) {
        return this.value+'%';
      }

    },
    step: 1
  },

  //...

},

Example

Warning: hard coding some values and depending on them in this way is risky if you have dynamic data. 警告:如果您有动态数据,则对一些值进行硬编码并以此方式依赖它们是有风险的。 For this instance we don't have dynamic data, they will be fixed, so it's safe for us. 对于这种情况,我们没有动态数据,它们被固定,因此对我们来说是安全的。 Another approach could be to iterate over each value/label and only return every X child as you require. 另一种方法是遍历每个值/标签,并仅根据需要返回每个X子级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM