Only 3 steps on xAxis with type xAxis in Highcharts

寵の児 提交于 2019-12-24 02:39:15

问题


Is there a way to get only three steps on the xAxis, one at start, one in the middle and one in the end. I've played around with xAxis.labels.steps but I couldn't get an reliable result.

Note the xAxis type is datetime.


回答1:


There is at least three ways to achieve that (which are automatically):

  1. Use tickPositioner (the best IMHO): you have full access to decide where tick should be set (for example: [min, average, max]).

  2. Use tickPixelInterval: useful only when you have fixed width of the chart, and when interval will be multiple of ncie numbers (like 0 - 1 - 2, or 100 - 200 - 300)

  3. Use tickInterval: useful only when you know range of xAxis, for example 0-10, so you can set tickInterval: 5

And jsfFiddle with compare: http://jsfiddle.net/FQ68Y/3/




回答2:


You can do this with the tickPositions option or the tickPositioner function:

http://api.highcharts.com/highcharts#xAxis.tickPositioner

tickPositions: [0, 1, 2]

or something like:

 tickPositioner: function () {
            var positions = [],
                tick = Math.floor(this.dataMin),
                increment = Math.ceil((this.dataMax - this.dataMin) / 2);

            for (; tick - increment <= this.dataMax; tick += increment) {
                positions.push(tick);
            }
            return positions;
        }



回答3:


As the other answers mention there is the tickPositioner field. For some reason it dont work out of the box. I had to add an info property to the returning array to get the xAxis to display formatted dates instead of just the milliseconds, as found in this fiddle.

tickPositioner: function (min, max) {
  var ticks = [min, min + (max - min) / 2, max];
  ticks.info = {
    unitName: 'hour',
    higherRanks: {}
  };
  return ticks;
}


来源:https://stackoverflow.com/questions/19809531/only-3-steps-on-xaxis-with-type-xaxis-in-highcharts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!