javascript - How do I make stacked area chart in plotly.js with correct values? -
there example of stacked area chart:
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) { for(var i=1; i<traces.length; i++) { for(var 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 , filled line chart'});
but stacking done manually, values not correct:
when mouse on first vertical line, see values 2, 3 , 6. if in source code, correct values 2, 1 , 3.
is there way stacking area charts correct values?
the original values can used text labels hover info, prior values being summed stacked chart.
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 , filled line chart'});
<script src="https://cdn.plot.ly/plotly-1.2.1.min.js"></script> <div id="mydiv" style="width: 480px; height: 400px;"></div>
Comments
Post a Comment