javascript - 如何在plotly.js 的堆积面积图中显示值和名称?

标签 javascript charts plotly stacked-area-chart

在下面链接中@emackey提供的解决方案中,它仅显示解决方案中的值,而不显示跟踪的名称。如何同时显示名称,而不仅仅是值?

How do I make stacked area chart in plotly.js with correct values?

// This code was provided by emackey in stackoverflow

var stacksDiv = document.getElementById("myDiv");
var traces = [
    {x: [1,2,3], y: [2,1,4], fill: 'tozeroy'},
    {x: [1,2,3], y: [1,1,2], fill: 'tonexty'},
    {x: [1,2,3], y: [3,0,2], fill: 'tonexty'}
];
function stackedArea(traces) {
    var i, j;
    for(i=0; i<traces.length; i++) {
        traces[i].text = [];
        traces[i].hoverinfo = 'text';
        for(j=0; j<(traces[i]['y'].length); j++) {
            traces[i].text.push(traces[i]['y'][j].toFixed(0));
        }
    }
    for(i=1; i<traces.length; i++) {
        for(j=0; j<(Math.min(traces[i]['y'].length, traces[i-1]['y'].length)); j++) {
            traces[i]['y'][j] += traces[i-1]['y'][j];
        }
    }
    return traces;
}

Plotly.newPlot(stacksDiv, stackedArea(traces), {title: 'stacked and filled line chart'});
// This code was provided by emackey in stackoverflow


<script src="https://cdn.plot.ly/plotly-1.2.1.min.js"></script>
<div id="myDiv" style="width: 480px; height: 400px;"></div>

最佳答案

您只需更改一行:

traces[i].hoverinfo = 'text';

应该变成

traces[i].hoverinfo = 'text+name';

关于javascript - 如何在plotly.js 的堆积面积图中显示值和名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47060301/

相关文章:

plotly - 用 plotly express 叠加两个直方图

javascript - 带有链接 .png 的 SVG 不呈现

javascript - 为什么将 javascript 命名空间作为函数?

android - Android中使用AChartEngine绘制动态折线图时遇到的问题

python - Matplotlib:每个时间序列子图绘制多行

r - 在条形图中的主网格线之间添加网格线

javascript - node_modules/rxjs-compat/operator/shareReplay.d.ts(2,10) : 中出现错误

javascript - 如果输入步进值 > 0 addClass 到父 div,否则removeClass

css - 使用 CSS 自定义 Google 图表

javascript - donut chart 中的过渡颜色