【问题标题】:how to customize hover tooltip如何自定义悬停工具提示
【发布时间】:2022-01-06 17:26:36
【问题描述】:

我有一个包含多条曲线的图,如附件所示。

var data = [
    {
        type: 'scatter',
        mode: 'lines+markers',
        name: 'Main.app.folder.section31.floor17.room8.box56.label6.nameA',
        x: [1,2,3,4,5],
        y: [2.02825,1.63728,6.83839,4.8485,4.73463],
        showlegend: false
    },
    {
        x: [1,2,3,4,5],
        y: [3.02825,2.63728,4.83839,3.8485,1.73463],
        name: 'Main.app.folder.section31.floor17.room8.box56.label6.different',
        showlegend: false
    },
    {
        type: 'scatter',
        mode: 'lines+markers',
        name: 'Main.app.folder.section31.floor17.room8.box56.label6.unknown',
        x: [1,2,3,4,5],
        y: [5.02825,4.63728,3.83839,2.8485,0.73463],
        hovertemplate: '(%{x},%{y})',
        showlegend: false
    },

]; 

var layout = {
    title: "Set hover text with hovertemplate",
};

Plotly.newPlot('myDiv', data, layout);

https://codepen.io/mmakrzem/pen/mdOpWLd

每条曲线都有一个很长的名称,所以我想自定义悬停模板,使其显示 (x,y) ...

对于我的示例,我希望看到类似 ...bel6.nameA...different...l6.unknown 的内容。而现在我正在为所有地块获得Main.app.fol...,所以我无法区分它们。我还尝试将悬停模板设置为 (x,y),但随后会显示整个名称,而且长度太长了。

【问题讨论】:

    标签: javascript plotly plotly.js


    【解决方案1】:

    对于每个跟踪,您可以使用 customdata 参数添加要显示的字符串,并将其设置为与传递给每个跟踪的 x 和 y 数组长度相同的字符串数组(例如,@ 987654325@)。

    然后您可以修改hovertemplate 以包含%{customdata} 并通过添加字符串<extra></extra> 来完全隐藏长跟踪名称。

    您的悬停模板将如下所示:'%{x},%{y} %{customdata}<extra></extra>'

    如果你想看一下,我的 codepen 是 here

    var data = [
        {
            type: 'scatter',
            mode: 'lines+markers',
            name: 'Main.app.folder.section31.floor17.room8.box56.label6.nameA',
            x: [1,2,3,4,5],
            y: [2.02825,1.63728,6.83839,4.8485,4.73463],
            customdata:Array(5).fill('...nameA'),
            hovertemplate: '%{x},%{y} %{customdata}<extra></extra>',
            showlegend: false
        },
        {
            x: [1,2,3,4,5],
            y: [3.02825,2.63728,4.83839,3.8485,1.73463],
            name: 'Main.app.folder.section31.floor17.room8.box56.label6.different',
            customdata:Array(5).fill('...different'),
            hovertemplate: '%{x},%{y} %{customdata}<extra></extra>',
            showlegend: false
        },
      {
            type: 'scatter',
            mode: 'lines+markers',
            name: 'Main.app.folder.section31.floor17.room8.box56.label6.unknown',
            x: [1,2,3,4,5],
            y: [5.02825,4.63728,3.83839,2.8485,0.73463],
            customdata:Array(5).fill('...unknown'),
            hovertemplate: '%{x},%{y} %{customdata}<extra></extra>',
            showlegend: false
        },
      
    ];
    
    var layout = {
        title: "Set hover text with hovertemplate",
    };
    
    Plotly.newPlot('myDiv', data, layout);
    

    【讨论】:

    • 谢谢。我不喜欢将数组插入 customdata 的想法,因为我有很多数据点,但我确实发现我可以创建自己的属性并引用它,所以我最终这样做了:{ type: 'scatter ',模式:'lines+markers',名称:'Main.app.folder.section31.floor17.room8.box56.label6.unknown',短名称:'...unknown',x:[1,2,3, 4,5], y: [5.02825,4.63728,3.83839,2.8485,0.73463], hovertemplate: '(%{x},%{y})%{data.shortname}', showlegend:假},
    • 这是一个很好的解决方案 - 我不知道您可以在 plotly.js 中创建自己的属性
    猜你喜欢
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多