Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abc99d0

Browse files
committed
working prototype with triagles showing direction of increases and decreases and rectangles displaying the intermediate and total sum
1 parent 70fa028 commit abc99d0

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

src/traces/bar/calc.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@ var colorscaleCalc = require('../../components/colorscale/calc');
1414
var arraysToCalcdata = require('./arrays_to_calcdata');
1515
var calcSelection = require('../scatter/calc_selection');
1616

17+
function includes(list, item) {
18+
for(var i = 0; i < list.length; i++) {
19+
if(list[i] === item) return true;
20+
}
21+
return false;
22+
}
23+
1724
module.exports = function calc(gd, trace) {
1825
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
1926
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
20-
var size, pos;
27+
var size, pos, i;
28+
29+
var isWaterfall = trace.type === 'waterfall';
2130

2231
if(trace.orientation === 'h') {
2332
size = xa.makeCalcdata(trace, 'x');
@@ -32,13 +41,13 @@ module.exports = function calc(gd, trace) {
3241
var cd = new Array(serieslen);
3342

3443
// set position and size (as well as for waterfall total size)
35-
for(var i = 0; i < serieslen; i++) {
44+
for(i = 0; i < serieslen; i++) {
3645
cd[i] = {
3746
p: pos[i],
3847
s: size[i]
3948
};
4049

41-
if(trace.type === 'waterfall') {
50+
if(isWaterfall) {
4251
cd[i].sum = (i === 0) ? 0 : cd[i - 1].sum + cd[i - 1].s;
4352
}
4453

@@ -47,6 +56,32 @@ module.exports = function calc(gd, trace) {
4756
}
4857
}
4958

59+
if(isWaterfall) {
60+
var newCD = [];
61+
var n = 0;
62+
for(i = 0; i < serieslen; i++) {
63+
newCD[n] = {
64+
p: cd[i].p,
65+
s: cd[i].s,
66+
sum: cd[i].sum,
67+
isReport: false
68+
}; n++;
69+
70+
if(includes(trace.report.after, i)) {
71+
// console.log("Found at: ", i);
72+
newCD[n] = {
73+
p: cd[i].p + 0.5,
74+
s: cd[i].s,
75+
sum: cd[i].sum,
76+
isReport: true
77+
}; n++;
78+
}
79+
}
80+
// console.log("newCD=", newCD);
81+
82+
cd = newCD;
83+
}
84+
5085
// auto-z and autocolorscale if applicable
5186
if(hasColorscale(trace, 'marker')) {
5287
colorscaleCalc(gd, trace, {

src/traces/bar/cross_trace_calc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ function initBase(gd, pa, sa, calcTraces) {
152152

153153
if(trace.type === 'waterfall') {
154154
for(j = 0; j < cd.length; j++) {
155-
cd[j].b += cd[j].sum;
155+
if(cd[j].isReport === true) {
156+
cd[j].s += cd[j].sum;
157+
} else {
158+
cd[j].b += cd[j].sum;
159+
}
156160
}
157161
}
158162
}

src/traces/bar/plot.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ module.exports = function plot(gd, plotinfo, cdbar, barLayer) {
3838
var cd0 = cd[0];
3939
var trace = cd0.trace;
4040

41+
var isWaterfall = trace.type === 'waterfall';
42+
4143
if(!plotinfo.isRangePlot) cd0.node3 = plotGroup;
4244

4345
var pointGroup = Lib.ensureSingle(plotGroup, 'g', 'points');
@@ -119,11 +121,18 @@ module.exports = function plot(gd, plotinfo, cdbar, barLayer) {
119121
y1 = fixpx(y1, y0);
120122
}
121123

124+
var shape;
125+
126+
if(isWaterfall && cd[i].isReport === false) {
127+
shape = 'M' + x0 + ',' + y0 + 'L' + (0.5 * (x1 + x0)) + ',' + y1 + 'V' + y0 + 'Z';
128+
} else {
129+
shape = 'M' + x0 + ',' + y0 + 'V' + y1 + 'H' + x1 + 'V' + y0 + 'Z';
130+
}
131+
122132
Lib.ensureSingle(bar, 'path')
123-
.style('vector-effect', 'non-scaling-stroke')
124-
.attr('d',
125-
'M' + x0 + ',' + y0 + 'V' + y1 + 'H' + x1 + 'V' + y0 + 'Z')
126-
.call(Drawing.setClipUrl, plotinfo.layerClipId, gd);
133+
.style('vector-effect', 'non-scaling-stroke')
134+
.attr('d', shape)
135+
.call(Drawing.setClipUrl, plotinfo.layerClipId, gd);
127136

128137
appendBarText(gd, bar, cd, i, x0, x1, y0, y1);
129138

test/image/mocks/waterfall_display-positive-negative-contributions.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"name": "first",
1919
"type": "waterfall",
2020
"report" : {
21-
"after": [2, 5],
21+
"after": [1, 4],
2222
"label": ["intermediate sum", "total"]
2323
},
2424
"orientation": "v"
@@ -41,7 +41,7 @@
4141
"name": "second",
4242
"type": "waterfall",
4343
"report" : {
44-
"after": [2, 5],
44+
"after": [1, 2, 4],
4545
"label": ["intermediate sum", "total"]
4646
},
4747
"orientation": "v"

0 commit comments

Comments
 (0)