【问题标题】:How to add custom tooltip items to Apexcharts?如何将自定义工具提示项添加到 Apexcharts?
【发布时间】:2026-01-17 07:35:01
【问题描述】:

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

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

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

  • 价格:(x 值)
  • 数字:(y 值)
  • 产品:“名称”,
  • 信息:'信息',
  • 网站:“名称”

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

小提琴: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();

【问题讨论】:

    标签: javascript tooltip apexcharts


    【解决方案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'
            },
            {
              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 添加到options 对象。访问传递给函数的w 对象以获取正确的数据。他们有一个类似的例子here。一旦您可以在该对象中识别出您感兴趣的数据,就可以使用它返回 HTML 以显示在工具提示中。

    【讨论】:

      最近更新 更多