简体   繁体   English

高图限制了将在yAxis上的类别中显示的项目数量

[英]Highcharts limit the amount of items that will be displayed in categories on the yAxis

I have the following chart in Highcharts, I wanted to limit the amount of items that will be displayed on the yAxis, for example 7 items, which always show the first and last items of the variable categories . 我在Highcharts中有以下图表,我想限制将在yAxis上显示的项目数量,例如7个项目,它们始终显示变量categories的第一个和最后一个项目。

 $(function () { var categories = ["Pos 01", "Pos 02", "Pos 03", "Pos 04", "Pos 05", "Pos 06", "Pos 07", "Pos 08", "Pos 09", "Pos 10", "Pos 11", "Pos 12", "Pos 13", "Pos 14", "Pos 15", "Pos 16", "Pos 17"]; var plan = [{x: 1534095420000, y:15},{x:1534097580000, y:14},{x:1534099020000,y:13},{x:1534119900000,y:12},{x:1534149780000,y:11},{x:1534174620000,y:10},{x:1534176420000,y:9},{x:1534189020000,y:8},{x:1534313940000,y:7},{x:1534317900000,y:6},{x:1534337700000,y:5},{x:1534373880000,y:4},{x:1534374120000,y:3},{x:1534375560000,y:2}, {x:1534377720000,y:1},{x:1534378200000,y:0},{x:1534378200000,y:0},{x:1534414200000,y:0},{x:1534414620000,y:1}]; var series =[{ name: "Plan", id: "plan", data: plan }]; // Create the chart window.chart = new Highcharts.Chart('container',{ colors: ["#7cb5ec"], chart: { type: "spline", }, exporting: { enabled: false }, title: { text: 'Graphic' }, yAxis: { categories: categories, title: { text: 'Position' }, labels: { format: '{value}' }, }, xAxis: { title: { text: 'Date' }, type: 'datetime', tickInterval: 3600000, }, plotOptions: { spline: { findNearestPointBy: 'xy', marker: { enabled: true } } }, tooltip: { split: false, useHTML: true, style: { pointerEvents: 'all' }, formatter: function () { return this.series.yAxis.categories[this.point.y]; } }, "series": series }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://code.highcharts.com/stock/highstock.js"></script> <script src="http://code.highcharts.com/stock/modules/exporting.js"></script> <div id="container" style="height: 600px; min-width: 500px"></div> 

It can be done in many ways depending on a few things: - what data do you have, - how many ticks do you want to have, - whether you want to display labels between ticks or on ticks, - how do you want to display labels if a number of categories is not divisible by a number of ticks, - whether you want to have equal gaps between ticks etc. 可以通过多种方式完成此操作,具体取决于几件事:-您拥有什么数据,-您想要拥有多少刻度,-是否要在刻度之间或刻度之间显示标签,-如何显示如果多个类别不能被多个刻度所整除,则标签;-是否希望刻度之间有相等的间隔等。

We can play with formatter function, categories, tickAmount property, yAxis.max property and many other things. 我们可以使用格式化程序功能,类别,tickAmount属性,yAxis.max属性和许多其他东西。 But in most cases we will have to give up from setting categories ridigly. 但是在大多数情况下,我们将不得不放弃设置类别的方式。

One of examples: 示例之一:

I set yAxis.max 我设置了yAxis.max

chart: {
  type: "spline",
  events: {
    load: function() {
      var chart = this,
        max = chart.series[0].dataMax;

      chart.update({
        yAxis: {
            max: max
        }
      });
    }
  }
},

I set tickAmount to 6 and used formatter function 我将tickAmount设置为6并使用了格式化程序功能

yAxis: {
  //categories: categories,
  title: {
    text: 'Position'
  },
  tickAmount: 6,
  labels: {
    formatter: function() {
        var value = this.value + 1;
                if (this.value < 9) {
        return 'Pos 0' + value;
      } else {
        return 'Pos ' + value;
      }
    }
  }
},

And I formatted tooltip: 我格式化了工具提示:

tooltip: {
  split: false,
  useHTML: true,
  style: {
    pointerEvents: 'all'
  },
  formatter: function() {
    var y = this.y + 1;
    if (y < 10) {
      return 'Pos 0' + y;
    } else {
      return 'Pos ' + y;
    }
  }
},

js Fiddle js小提琴

I know that the last tick is not the last element of your category array, but it would be easier to "edit" your data and work on proper data than try to adapt ticks to your strange category array and data that doesn't fit categories. 我知道最后一个刻度不是类别数组的最后一个元素,但是“编辑”您的数据并处理适当的数据比尝试使刻度适应您奇怪的类别数组和不适合类别的数据要容易得多。

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

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