Get left and top position of highcharts scrollbar

痞子三分冷 提交于 2019-12-25 02:29:00

问题


I wanted to put custom label on highcharts which should be placed on left side of scrollbar. How can I get top and left position of scrollbar.?

I have put label with following code

chart.renderer.text('<span style="font-weight:600;"> 1-21 </span>', 20, 120)
        .css({
            color: 'green',
            fontSize: '12px'
        })
        .add();

回答1:


You can get position of the scrollbar using chart.xAxis[0].scrollbar.group.translateX and chart.xAxis[0].scrollbar.group.translateY, for example: https://codepen.io/anon/pen/KeBxNj?editors=1010

Snippet:

var chart = Highcharts.chart('container', {
  chart: {
    type: 'bar',
    marginLeft: 150,
    events: {
      load: function () {
        var scrollbar = this.xAxis[0].scrollbar,
            bbox;
        // Render:
        this.customLabel = this.renderer.text('<span style="font-weight:600;"> 1-21 </span>', 0, 0).attr({
          zIndex: 5
        }).add();
        // Get bbox
        bbox = this.customLabel.getBBox();

        // Position label
        this.customLabel.attr({
          x: scrollbar.group.translateX - bbox.width,
          y: scrollbar.group.translateY + bbox.height
        });
      }
    }
  },
  ...
});



回答2:


You can determine the scrollbar's left position using the chart.plotWidth + chart.plotLeft - 25. Also, the top position can be got using chart.plotTop + 10. The numeric values are just top and left paddings.

Please have a look at this codepen. https://codepen.io/samuellawrentz/pen/eKjdpN?editors=1010

Hope this helps :)



来源:https://stackoverflow.com/questions/50982955/get-left-and-top-position-of-highcharts-scrollbar

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