如何将自定义工具提示项添加到 Apexcharts?

How to add custom tooltip items to Apexcharts?

我正在使用 ApexChart,我想向工具提示(悬停时显示)添加更多信息。我已经阅读了文档,但我真的不知道该怎么做。

我的问题是我不明白我应该在哪里添加额外的数据以及如何让它显示在工具提示中?

现在我只在工具提示中看到默认值,即系列名称和“y”值。我想看:

如何将其添加到工具提示中?这是我到目前为止得到的:

Fiddle: https://jsfiddle.net/9m0r2ont/1/

代码:

var options = {
  chart: {
    height: 380,
    width: "100%",
    type: "scatter",
  },
  series: [
    {
      name: "Series 1",
      data: [
        {
          x: 100,
          y: 50,
          product: 'name',
          info: 'info',
          site: 'name'
        },
        {
          x: 150,
          y: 55,
          product: 'name',
          info: 'info',
          site: 'name'
        }
      ]
    }
  ],
  xaxis: {
    type: "numeric"
  }
};

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

这样试试:

var options = {
  chart: {
    height: 380,
    width: "100%",
    type: "scatter",
  },
  series: [
    {
      name: "Series 1",
      data: [
        {
          x: 100,
          y: 50,
          product: 'name',
          info: 'info',
          site: 'name'
        },
        {
          x: 150,
          y: 55,
          product: 'name',
          info: 'info',
          site: 'name'
        },
        {
          x: 130,
          y: 44,
          product: 'name',
          info: 'info',
          site: 'name'
        }
      ]
    }
  ],
  xaxis: {
    type: "numeric"
  },
  tooltip: {
    custom: function({series, seriesIndex, dataPointIndex, w}) {
      var data = w.globals.initialSeries[seriesIndex].data[dataPointIndex];
      
      return '<ul>' +
      '<li><b>Price</b>: ' + data.x + '</li>' +
      '<li><b>Number</b>: ' + data.y + '</li>' +
      '<li><b>Product</b>: \'' + data.product + '\'</li>' +
      '<li><b>Info</b>: \'' + data.info + '\'</li>' +
      '<li><b>Site</b>: \'' + data.site + '\'</li>' +
      '</ul>';
    }
  }
};

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
<div id="chart">
</div>

为此,您需要添加一个 custom tooltip to the options object. Access the w object passed into the function to get the correct data. They have a similar example here。一旦您可以在该对象中识别出您感兴趣的数据,就可以将其用于 return HTML 以显示在工具提示中。