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

时间:2017-11-01 17:17:47

标签: 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'});
&#13;
// 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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您只需更改一行:

traces[i].hoverinfo = 'text';

应该成为

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