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

如何在Slate中创建tooltip以显示bar chart的series标题?

解决堆叠条形图Legend过多,通过Tooltip显示Series标题的方法

核心思路

先隐藏占据空间的Legend,再通过自定义Tooltip让鼠标悬停在条形上时显示对应Series的标题,既保持界面整洁,又不丢失交互信息。

主流图表库实现方法

ECharts

  • 隐藏Legend:在配置项中设置 legend: { show: false }
  • 自定义Tooltip内容,确保显示Series名称:
option = {
  tooltip: {
    trigger: 'item',
    formatter: (params) => `${params.seriesName}: ${params.value}`
  },
  legend: { show: false },
  xAxis: { /* 你的X轴配置 */ },
  yAxis: { /* 你的Y轴配置 */ },
  series: [/* 你的Series数据数组 */]
};

Highcharts

  • 关闭Legend显示:配置 legend: { enabled: false }
  • 自定义Tooltip模板,明确展示Series名称:
Highcharts.chart('container', {
  tooltip: {
    formatter: function() {
      return `<b>${this.series.name}</b>: ${this.y}`;
    }
  },
  legend: { enabled: false },
  // 其他图表配置(X轴、Y轴、Series等)
});

Plotly(Python示例)

  • 隐藏Legend:在布局中设置 showlegend: false
  • 自定义Hover模板,指定显示Series名称:
import plotly.express as px

# 示例数据
df = px.data.tips()
fig = px.bar(df, x="day", y="total_bill", color="time", barmode="stack")

# 隐藏Legend
fig.update_layout(showlegend=False)
# 自定义Tooltip内容
fig.update_traces(hovertemplate='%{fullData.name}: %{y}<extra></extra>')

fig.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 17:31:09