如何在 Highcharts 中将注释文本添加到 x 和 y 轴?

How to add annotation text to x and y axis in Highcharts?

我有一个气泡图,需要在 x 轴和 y 轴上显示注释文本。我整理了一张图片来展示我希望通过使用注释实现的目标:

Bubble Chart

我是新手,如果有人能提供帮助,我将不胜感激。

https://jsfiddle.net/Sam786/qwormkt2/1/

let chart = Highcharts.chart('bubble_chart', {
            chart: {
                type: 'bubble',
                zoomType: 'xy'
            },
            title: { text: "" },
            xAxis: {
                title: {
                    text: "106. How disruptive has the COVID-19 pandemic been to your ability to do your work?"
                },
                plotLines: [{
                    value: 4,
                    color: 'darkgray',
                    dashStyle: 'shortdash',
                    width: 2,
                    valueDecimals: 2,
                    // label: {
                    //     text: 'SEC Avg',
                    // }
                    zIndex: -1
                }],
            },
            yAxis: {
                title: {
                    text: "107. How has your work demands changed because of the COVID-19 pandemic?"
                },
                plotLines: [{
                    value: 2.5,
                    color: 'darkgray',
                    dashStyle: 'shortdash',
                    width: 2,
                    label: {
                        text: 'SEC Average',
                        verticalAlign: 'bottom'
                    }
                }]
            },
            credits: {enabled: false},
            tooltip: {
                useHTML: true,
                headerFormat: '',
                pointFormat:  '<strong>{point.org}</strong><br/>'+
                              'Respondents: <b>{point.z}</b><br/>'+
                              'X-Question Avg: <b>{point.x}</b><br/>'+
                              'Y-Question Avg: <b>{point.y}</b>',
                footerFormat: '',
                valueDecimals: 1
            },
            
            series: [{
                name: groups[0],
                color: Highcharts.getOptions().colors[0],
                data: []
            }, {
                name: groups[1],
                color: Highcharts.getOptions().colors[1],
                data: []
            }, {
                name: groups[2],
                color: Highcharts.getOptions().colors[2],
                data: []
            }]
        });

您可以使用 SVGRenderer 在图表内绘制形状和文本。这是来自官方 Highcharts 论坛的示例:http://jsfiddle.net/ajxyuax2/1/

chart.renderer.text('Some text', 80, 80)
  .attr({ zIndex: 10 })
  .css({ fontSize: '12px'})
  .add();

chart.renderer.rect(75, 65, 135, 40, 2)
  .attr({
    'stroke-width': 2,
    stroke: 'black',
    fill: '#CEF74A',
    zIndex: 4
  })
  .add();

有关所有可用选项,请参阅 documentation of SVGRenderer。您可以使用 text 方法显示文本,您可以使用 rect 方法绘制矩形。

您可以按照第二个答案中的建议使用 SVGRenderer 工具,但如果您想使用注释,您可以通过在 chart.event.load 回调中添加它们来自定义它们的位置。

演示:https://jsfiddle.net/BlackLabel/nc9fe1Lt/

  chart: {
    events: {
      load() {
        const chart = this;

        chart.addAnnotation({
          labels: [{
            text: 'test',
            point: {
              x: 0,
              y: chart.plotHeight + chart.spacing[2]
            },
            shape: 'rectangle'
          }, {
            text: 'test2',
            point: {
              x: chart.plotWidth,
              y: chart.plotHeight + chart.spacing[2]
            },
            shape: 'rectangle'
          }]
        })
      }
    }
  },

API: https://api.highcharts.com/class-reference/Highcharts.Chart#addAnnotation

API: https://api.highcharts.com/highcharts/chart.events.load