Skip to content

Commit 8d75c3a

Browse files
committed
bar: implement attributes base, offset and width
1 parent 83fdb44 commit 8d75c3a

File tree

3 files changed

+301
-69
lines changed

3 files changed

+301
-69
lines changed

src/traces/bar/calc.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,40 @@ module.exports = function calc(gd, trace) {
4040
var serieslen = Math.min(pos.length, size.length),
4141
cd = [];
4242

43+
// set position
4344
for(i = 0; i < serieslen; i++) {
4445

4546
// add bars with non-numeric sizes to calcdata
4647
// so that ensure that traces with gaps are
4748
// plotted in the correct order
4849

4950
if(isNumeric(pos[i])) {
50-
cd.push({p: pos[i], s: size[i], b: 0});
51+
cd.push({p: pos[i]});
52+
}
53+
}
54+
55+
// set base
56+
var base = trace.base;
57+
58+
if(Array.isArray(base)) {
59+
for(i = 0; i < Math.min(base.length, cd.length); i++) {
60+
cd[i].b = base[i];
61+
}
62+
for(; i < cd.length; i++) {
63+
cd[i].b = 0;
64+
}
65+
}
66+
else {
67+
var b = (base === undefined) ? 0 : base;
68+
for(i = 0; i < cd.length; i++) {
69+
cd[i].b = b;
70+
}
71+
}
72+
73+
// set size
74+
for(i = 0; i < cd.length; i++) {
75+
if(isNumeric(size[i])) {
76+
cd[i].s = size[i] - cd[i].b;
5177
}
5278
}
5379

src/traces/bar/plot.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,39 @@ module.exports = function plot(gd, plotinfo, cdbar) {
3434
.attr('class', 'points')
3535
.each(function(d) {
3636
var t = d[0].t,
37-
trace = d[0].trace;
37+
trace = d[0].trace,
38+
poffset = t.poffset,
39+
poffsetIsArray = Array.isArray(poffset),
40+
barwidth = t.barwidth,
41+
barwidthIsArray = Array.isArray(barwidth);
3842

3943
arraysToCalcdata(d);
4044

4145
d3.select(this).selectAll('path')
4246
.data(Lib.identity)
4347
.enter().append('path')
44-
.each(function(di) {
48+
.each(function(di, i) {
4549
// now display the bar
4650
// clipped xf/yf (2nd arg true): non-positive
4751
// log values go off-screen by plotwidth
4852
// so you see them continue if you drag the plot
53+
var p0 = di.p + ((poffsetIsArray) ? poffset[i] : poffset),
54+
p1 = p0 + ((barwidthIsArray) ? barwidth[i] : barwidth),
55+
s0 = di.b,
56+
s1 = s0 + di.s;
57+
4958
var x0, x1, y0, y1;
5059
if(trace.orientation === 'h') {
51-
y0 = ya.c2p(t.poffset + di.p, true);
52-
y1 = ya.c2p(t.poffset + di.p + t.barwidth, true);
53-
x0 = xa.c2p(di.b, true);
54-
x1 = xa.c2p(di.s + di.b, true);
60+
y0 = ya.c2p(p0, true);
61+
y1 = ya.c2p(p1, true);
62+
x0 = xa.c2p(s0, true);
63+
x1 = xa.c2p(s1, true);
5564
}
5665
else {
57-
x0 = xa.c2p(t.poffset + di.p, true);
58-
x1 = xa.c2p(t.poffset + di.p + t.barwidth, true);
59-
y1 = ya.c2p(di.s + di.b, true);
60-
y0 = ya.c2p(di.b, true);
66+
x0 = xa.c2p(p0, true);
67+
x1 = xa.c2p(p1, true);
68+
y0 = ya.c2p(s0, true);
69+
y1 = ya.c2p(s1, true);
6170
}
6271

6372
if(!isNumeric(x0) || !isNumeric(x1) ||

0 commit comments

Comments
 (0)