Add a Specific Y-Axis Label with Highcharts

徘徊边缘 提交于 2019-12-13 01:14:34

问题


I have a graph with a labelled Y-Axis with points 1 through 100. In addition to the regularly spaced labels (0, 10, 20, etc), I want to add a label for an arbitrary point, say 47. Is this possible with highcharts?


回答1:


Based on your comment, you can add a custom tick with tickPositioner function, with a code like this

$(function () {
    $('#container').highcharts({
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        yAxis: {
            tickInterval: 20, //sets the interval ticks
            tickPositioner: function(){
                var ticks = this.tickPositions; // gets the tick positions
                ticks.push(47); // adds the custom tick
                ticks.sort(function(a, b) {
                    return a - b; // sorts numerically the ticks 
                });
                return ticks; // returns the new ticks
            }
        },
        series: [{
            data: [29.9, 71.5, 86.4, 29.2, 44.0, 76.0, 93.5, 98.5, 16.4, 94.1, 95.6, 54.4]
        }]
    });
});

JSFiddle demo




回答2:


You can add a specific line to the yAxis with the plotLines option.

yAxis: {
    plotLines: [{
        value: 47,
        color: 'rgb(216, 216, 216)',
        width: 1,
    }]
},

http://jsfiddle.net/nicholasduffy/wa6ukyyp/1/

EDIT:

This seems a bit of a hack, but you could fake the label.

yAxis: {
    plotLines: [{
        value: 47,
        color: 'rgb(216, 216, 216)',
        width: 1,
        label : {
            text: '47',
            x: -30,
            y: 2
        }
    }]
},

http://jsfiddle.net/nicholasduffy/wa6ukyyp/2/




回答3:


I think the solution I found that comes closest is to create a line of zero thickness and label the line:

    plotLines: [{
      value: cutoff,
      color: 'rgb(216, 216, 216)',
      width: 0,
      label: {
        text: 'label_name',
        style: {
          color: 'grey',
          fontweight: 'bold'
        }
      }
    }],

The label_name goes pretty close to the Y-Axis, and it gets the point across without putting a tooltip onto the graph



来源:https://stackoverflow.com/questions/32018948/add-a-specific-y-axis-label-with-highcharts

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