gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:08:25 27 4
gpt4 key购买 nike

在下面链接中@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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com