You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly.extendTraces无法更新hovertext问题求助

解决Plotly.extendTraces更新hovertext报错问题

核心原因

Plotly.extendTraces 仅支持扩展已存在的数组类型属性。如果初始化图表时,目标trace未定义hovertext数组,后续调用该方法添加hovertext就会触发cannot extend missing or non-array attribute: hovertext错误。

具体修复步骤

1. 初始化时显式声明hovertext空数组

创建图表的初始trace必须包含hovertext: [],让Plotly识别该属性为数组类型:

// 初始化图表示例
Plotly.newPlot('temperatureChart', [
  {
    x: [],
    y: [],
    hovertext: [], // 必须显式定义为空数组
    type: 'scatter',
    mode: 'lines+markers'
  }
]);

2. 正确调用extendTraces更新hovertext

后续更新时,传入的hovertext需为二维数组(外层对应trace,内层对应新增数据点),同时指定要更新的trace索引:

// 模拟实时更新温度数据
const currentTemp = Math.random() * 10 + 20;
const currentTime = new Date();

Plotly.extendTraces('temperatureChart', {
  x: [[currentTime.getTime()]],
  y: [[currentTemp]],
  hovertext: [[`温度: ${currentTemp.toFixed(1)}°C<br>时间: ${currentTime.toLocaleString()}`]]
}, [0]); // [0] 表示更新第一个trace(索引从0开始)

3. 常见排查点

  • 确认初始trace的hovertext是数组类型,不能是字符串或未定义
  • 检查extendTraces中hovertext的结构是否为二维数组,和x、y的格式保持一致
  • 验证第二个参数(trace索引数组)是否匹配你要更新的目标trace

内容的提问来源于stack exchange,提问作者tux007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 06:20:02