简体   繁体   English

如何让 Highcharts 的 yAxis 显示类别而不是数值?

[英]How can I get the yAxis of Highcharts to display categories instead of number values?

I have this fiddle JSfiddle我有这个小提琴JSfiddle

Here is the reproduced code:这是复制的代码:

$(function () {
$('#container').highcharts({
    chart: {
        type: 'bar'
    },
    xAxis: {
        categories: ['Heroku', 'Ruby','Lisp','Javascript','Python','PHP']
    },
    yAxis: {
        categories: ['low','medium','high'],
        title: {
            text: 'expertise',
            align: 'high'
        },
        labels: {
            overflow: 'justify'
        }
    },
    tooltip: {
        valueSuffix: ' millions'
    },
    plotOptions: {
        bar: {
            dataLabels: {
                enabled: true
            }
        }
    },
    series: [{
        data: ['low','high','low','medium','medium']
    }]
});
});

If you look at the fiddle the yAxis does not render and has a value of for every x category.如果您查看小提琴,则 yAxis 不会呈现并且对于每个 x 类别都有一个值。 I've been looking at the highcharts api, but I can't seem to get this right.我一直在查看 highcharts api,但我似乎无法做到这一点。 The code makes sense to me but I'm obviously doing something wrong.代码对我来说很有意义,但我显然做错了什么。 Can someone point out why the YAxis is not displaying correctly?有人能指出为什么 YAxis 显示不正确吗?

As mentioned in my comment, you need to supply the numeric value of the category, not the category name.正如我在评论中提到的,您需要提供类别的数值,而不是类别名称。

In the case of categories, the numeric value is the array index.对于类别,数值是数组索引。

Also, in your case, the way you are trying to plot the values, I would add an empty category at the beginning, otherwise your first category of low gets plotted as 0 , which doesn't seem right.此外,在您的情况下,您尝试绘制值的方式,我会在开头添加一个空类别,否则您的第一个low类别将被绘制为0 ,这似乎不正确。

So,所以,

categories: ['low','medium','high']

Becomes成为

categories: ['','low','medium','high'],

And

data: ['low','high','low','medium','medium']

Becomes成为

data: [1,3,1,2,2]

Updated fiddle:更新的小提琴:

Check this fiddle: http://jsfiddle.net/navjot227/k64boexd/2/检查这个小提琴: http : //jsfiddle.net/navjot227/k64boexd/2/

Trick is to utilize the formatter function.技巧是利用格式化程序功能。 You can use a similar formatter function on y-axis labels too if that's desired.如果需要,您也可以在 y 轴标签上使用类似的格式化程序功能。 Though it seems like you need it for data labels for this problem.尽管您似乎需要它来解决此问题的数据标签。

$(function() {
  $('#container').highcharts({
    chart: {
      type: 'bar'
    },
    xAxis: {
      categories: ['Heroku', 'Ruby', 'Lisp', 'Javascript', 'Python', 'PHP']
    },
    yAxis: {

      title: {
        text: 'expertise',
        align: 'high'
      },
      labels: {
        overflow: 'justify',

      }
    },
    tooltip: {
      valueSuffix: ' millions'
    },
    plotOptions: {
      bar: {
        dataLabels: {
          enabled: true,
          formatter: function() {
            if (this.y == 0) {

              return 'low'
            } else if (this.y == 1) {
              return 'medium'
            } else {
              console.log(this.y);
              return 'high'
            }
          }

        }
      }
    },
    series: [{
      data: [0, 2, 0, 1, 1]
    }]
  });
});

In my opinion, it is kinda unlikely for a line graph to have a y-axis category, since it speaks more of amount or value.在我看来,折线图不太可能有 y 轴类别,因为它更多地涉及数量或价值。 In your case, "low, medium, and high" speaks of ranges, with which a certain value can be assigned to any of it.在您的情况下,“低、中和高”表示范围,可以将某个值分配给其中的任何一个。

Thus, Highcharts accepts series data in numeric form.因此,Highcharts 接受数字形式的系列数据。 But you can work around it by setting ['low', 'medium', 'high'] in the category attribute of yAxis , then setting series data as an array of number corresponding to the index of the category, ie [0,1,1,2,...] and tweaking the tooltip to display the category instead of the y value using formatter attribute.但是您可以通过在yAxiscategory属性中设置['low', 'medium', 'high']yAxis ,然后将系列data设置为与类别索引相对应的数字数组,即[0,1,1,2,...]并使用formatter属性调整工具提示以显示类别而不是y值。

Here is the code:这是代码:

$(function() {
  yCategories = ['low', 'medium', 'high'];
  $('#container').highcharts({
    title: {
      text: 'Chart with category axes'
    },

    xAxis: {
      categories: ['Heroku', 'Ruby','Lisp','Javascript','Python','PHP']
    },

    yAxis: {
      categories: yCategories
    },

    tooltip: {
      formatter: function() {
        return this.point.category + ': ' + yCategories[this.y];
      }
    },

    series: [{
      data: [0, 1, 2, 2, 1]
    }]
  });
});

Here is a working example : JSFiddle这是一个工作示例: JSFiddle

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

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