Skip to content

Commit 452e792

Browse files
committed
compute bar corners p0/p1/s0/s1 in crossTraceCalc
- so that bar and barpolar can DRY up their plot logic - need to rename histogram "hover positions" p0/p1 -> ph0/ph1
1 parent 183b734 commit 452e792

File tree

6 files changed

+64
-80
lines changed

6 files changed

+64
-80
lines changed

src/traces/bar/cross_trace_calc.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,22 +683,32 @@ function collectExtents(calcTraces, pa) {
683683
return String(Math.round(roundFactor * (p - pMin)));
684684
};
685685

686+
var poffset, poffsetIsArray;
687+
686688
for(i = 0; i < calcTraces.length; i++) {
687689
cd = calcTraces[i];
688690
cd[0].t.extents = extents;
691+
poffset = cd[0].t.poffset;
692+
poffsetIsArray = Array.isArray(poffset);
693+
689694
for(j = 0; j < cd.length; j++) {
690695
var di = cd[j];
691696
var p0 = di[posLetter] - di.w / 2;
697+
692698
if(isNumeric(p0)) {
693699
var p1 = di[posLetter] + di.w / 2;
694700
var pVal = round(di.p);
695701
if(extents[pVal]) {
696702
extents[pVal] = [Math.min(p0, extents[pVal][0]), Math.max(p1, extents[pVal][1])];
697-
}
698-
else {
703+
} else {
699704
extents[pVal] = [p0, p1];
700705
}
701706
}
707+
708+
di.p0 = di.p + ((poffsetIsArray) ? poffset[j] : poffset);
709+
di.p1 = di.p0 + di.w;
710+
di.s0 = di.b;
711+
di.s1 = di.s0 + di.s;
702712
}
703713
}
704714
}

src/traces/bar/plot.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,10 @@ module.exports = function plot(gd, plotinfo, cdbar, barLayer) {
3838
var bartraces = Lib.makeTraceGroups(barLayer, cdbar, 'trace bars').each(function(cd) {
3939
var plotGroup = d3.select(this);
4040
var cd0 = cd[0];
41-
var t = cd0.t;
4241
var trace = cd0.trace;
4342

4443
if(!plotinfo.isRangePlot) cd0.node3 = plotGroup;
4544

46-
var poffset = t.poffset;
47-
var poffsetIsArray = Array.isArray(poffset);
48-
4945
var pointGroup = Lib.ensureSingle(plotGroup, 'g', 'points');
5046

5147
var bars = pointGroup.selectAll('g.point').data(Lib.identity);
@@ -62,26 +58,21 @@ module.exports = function plot(gd, plotinfo, cdbar, barLayer) {
6258
// clipped xf/yf (2nd arg true): non-positive
6359
// log values go off-screen by plotwidth
6460
// so you see them continue if you drag the plot
65-
var p0 = di.p + ((poffsetIsArray) ? poffset[i] : poffset),
66-
p1 = p0 + di.w,
67-
s0 = di.b,
68-
s1 = s0 + di.s;
69-
7061
var x0, x1, y0, y1;
7162
if(trace.orientation === 'h') {
72-
y0 = ya.c2p(p0, true);
73-
y1 = ya.c2p(p1, true);
74-
x0 = xa.c2p(s0, true);
75-
x1 = xa.c2p(s1, true);
63+
y0 = ya.c2p(di.p0, true);
64+
y1 = ya.c2p(di.p1, true);
65+
x0 = xa.c2p(di.s0, true);
66+
x1 = xa.c2p(di.s1, true);
7667

7768
// for selections
7869
di.ct = [x1, (y0 + y1) / 2];
7970
}
8071
else {
81-
x0 = xa.c2p(p0, true);
82-
x1 = xa.c2p(p1, true);
83-
y0 = ya.c2p(s0, true);
84-
y1 = ya.c2p(s1, true);
72+
x0 = xa.c2p(di.p0, true);
73+
x1 = xa.c2p(di.p1, true);
74+
y0 = ya.c2p(di.s0, true);
75+
y1 = ya.c2p(di.s1, true);
8576

8677
// for selections
8778
di.ct = [(x0 + x1) / 2, y1];

src/traces/barpolar/plot.js

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ module.exports = function plot(gd, subplot, cdbar) {
2424
var barLayer = subplot.layers.frontplot.select('g.barlayer');
2525

2626
Lib.makeTraceGroups(barLayer, cdbar, 'trace bars').each(function(cd) {
27-
var cd0 = cd[0];
28-
var plotGroup = cd0.node3 = d3.select(this);
29-
var t = cd0.t;
30-
31-
var poffset = t.poffset;
32-
var poffsetIsArray = Array.isArray(poffset);
33-
27+
var plotGroup = cd[0].node3 = d3.select(this);
3428
var pointGroup = Lib.ensureSingle(plotGroup, 'g', 'points');
3529
var bars = pointGroup.selectAll('g.point').data(Lib.identity);
3630

@@ -41,26 +35,13 @@ module.exports = function plot(gd, subplot, cdbar) {
4135

4236
bars.exit().remove();
4337

44-
bars.each(function(di, i) {
38+
bars.each(function(di) {
4539
var bar = d3.select(this);
4640

47-
// TODO move this block to Bar.setPositions?
48-
//
49-
// now display the bar
50-
// clipped xf/yf (2nd arg true): non-positive
51-
// log values go off-screen by plotwidth
52-
// so you see them continue if you drag the plot
53-
//
54-
// this gets reused in ./hover.js
55-
var p0 = di.p0 = di.p + ((poffsetIsArray) ? poffset[i] : poffset);
56-
var p1 = di.p1 = p0 + di.w;
57-
var s0 = di.s0 = di.b;
58-
var s1 = di.s1 = s0 + di.s;
59-
60-
var rp0 = di.rp0 = radialAxis.c2p(s0);
61-
var rp1 = di.rp1 = radialAxis.c2p(s1);
62-
var thetag0 = di.thetag0 = angularAxis.c2g(p0);
63-
var thetag1 = di.thetag1 = angularAxis.c2g(p1);
41+
var rp0 = di.rp0 = radialAxis.c2p(di.s0);
42+
var rp1 = di.rp1 = radialAxis.c2p(di.s1);
43+
var thetag0 = di.thetag0 = angularAxis.c2g(di.p0);
44+
var thetag1 = di.thetag1 = angularAxis.c2g(di.p1);
6445

6546
var dPath;
6647

@@ -74,7 +55,7 @@ module.exports = function plot(gd, subplot, cdbar) {
7455
dPath = 'M0,0Z';
7556
} else {
7657
// this 'center' pt is used for selections and hover labels
77-
var rg1 = radialAxis.c2g(s1);
58+
var rg1 = radialAxis.c2g(di.s1);
7859
var thetagMid = (thetag0 + thetag1) / 2;
7960
di.ct = [
8061
xa.c2p(rg1 * Math.cos(thetagMid)),

src/traces/histogram/calc.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,17 @@ module.exports = function calc(gd, trace) {
176176
b: 0
177177
};
178178

179-
// pts and p0/p1 don't seem to make much sense for cumulative distributions
179+
// setup hover and event data fields,
180+
// N.B. pts and "hover" positions ph0/ph1 don't seem to make much sense
181+
// for cumulative distributions
180182
if(!cumulativeSpec.enabled) {
181183
cdi.pts = inputPoints[i];
182184
if(uniqueValsPerBin) {
183-
cdi.p0 = cdi.p1 = (inputPoints[i].length) ? pos0[inputPoints[i][0]] : pos[i];
185+
cdi.ph0 = cdi.ph1 = (inputPoints[i].length) ? pos0[inputPoints[i][0]] : pos[i];
184186
}
185187
else {
186-
cdi.p0 = roundFn(binEdges[i]);
187-
cdi.p1 = roundFn(binEdges[i + 1], true);
188+
cdi.ph0 = roundFn(binEdges[i]);
189+
cdi.ph1 = roundFn(binEdges[i + 1], true);
188190
}
189191
}
190192
cd.push(cdi);

src/traces/histogram/hover.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
2424
if(!trace.cumulative.enabled) {
2525
var posLetter = trace.orientation === 'h' ? 'y' : 'x';
2626

27-
pointData[posLetter + 'Label'] = hoverLabelText(pointData[posLetter + 'a'], di.p0, di.p1);
27+
pointData[posLetter + 'Label'] = hoverLabelText(pointData[posLetter + 'a'], di.ph0, di.ph1);
2828
}
2929

3030
return pts;

test/jasmine/tests/histogram_test.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ describe('Test histogram', function() {
206206
var out = calc(gd, fullTrace);
207207
delete out[0].trace;
208208

209-
// this is dumb - but some of the `p0` values are `-0` which doesn't match `0`
209+
// this is dumb - but some of the `ph0` values are `-0` which doesn't match `0`
210210
// even though -0 === 0
211211
out.forEach(function(cdi) {
212212
for(var key in cdi) {
@@ -238,10 +238,10 @@ describe('Test histogram', function() {
238238
expect(out).toEqual([
239239
// full calcdata has x and y too (and t in the first one),
240240
// but those come later from crossTraceCalc.
241-
{i: 0, b: 0, p: d70, s: 2, pts: [0, 1], p0: d70, p1: d70},
242-
{i: 1, b: 0, p: d71, s: 1, pts: [2], p0: d71, p1: d71},
243-
{i: 2, b: 0, p: d72, s: 0, pts: [], p0: d72, p1: d72},
244-
{i: 3, b: 0, p: d73, s: 1, pts: [3], p0: d73, p1: d73}
241+
{i: 0, b: 0, p: d70, s: 2, pts: [0, 1], ph0: d70, ph1: d70},
242+
{i: 1, b: 0, p: d71, s: 1, pts: [2], ph0: d71, ph1: d71},
243+
{i: 2, b: 0, p: d72, s: 0, pts: [], ph0: d72, ph1: d72},
244+
{i: 3, b: 0, p: d73, s: 1, pts: [3], ph0: d73, ph1: d73}
245245
]);
246246

247247
// All data on exact months: shift so bin center is on (31-day months)
@@ -255,10 +255,10 @@ describe('Test histogram', function() {
255255
var d70mar = Date.UTC(1970, 2, 2, 12);
256256
var d70apr = Date.UTC(1970, 3, 1);
257257
expect(out).toEqual([
258-
{i: 0, b: 0, p: d70, s: 2, pts: [0, 1], p0: d70, p1: d70},
259-
{i: 1, b: 0, p: d70feb, s: 1, pts: [2], p0: d70feb, p1: d70feb},
260-
{i: 2, b: 0, p: d70mar, s: 0, pts: [], p0: d70mar, p1: d70mar},
261-
{i: 3, b: 0, p: d70apr, s: 1, pts: [3], p0: d70apr, p1: d70apr}
258+
{i: 0, b: 0, p: d70, s: 2, pts: [0, 1], ph0: d70, ph1: d70},
259+
{i: 1, b: 0, p: d70feb, s: 1, pts: [2], ph0: d70feb, ph1: d70feb},
260+
{i: 2, b: 0, p: d70mar, s: 0, pts: [], ph0: d70mar, ph1: d70mar},
261+
{i: 3, b: 0, p: d70apr, s: 1, pts: [3], ph0: d70apr, ph1: d70apr}
262262
]);
263263

264264
// data on exact days: shift so each bin goes from noon to noon
@@ -272,11 +272,11 @@ describe('Test histogram', function() {
272272

273273
expect(out).toEqual([
274274
// dec 31 12:00 -> jan 31 12:00, middle is jan 16
275-
{i: 0, b: 0, p: Date.UTC(1970, 0, 16), s: 2, pts: [0, 1], p0: Date.UTC(1970, 0, 1), p1: Date.UTC(1970, 0, 31)},
275+
{i: 0, b: 0, p: Date.UTC(1970, 0, 16), s: 2, pts: [0, 1], ph0: Date.UTC(1970, 0, 1), ph1: Date.UTC(1970, 0, 31)},
276276
// jan 31 12:00 -> feb 28 12:00, middle is feb 14 12:00
277-
{i: 1, b: 0, p: Date.UTC(1970, 1, 14, 12), s: 1, pts: [2], p0: Date.UTC(1970, 1, 1), p1: Date.UTC(1970, 1, 28)},
278-
{i: 2, b: 0, p: Date.UTC(1970, 2, 16), s: 0, pts: [], p0: Date.UTC(1970, 2, 1), p1: Date.UTC(1970, 2, 31)},
279-
{i: 3, b: 0, p: Date.UTC(1970, 3, 15, 12), s: 1, pts: [3], p0: Date.UTC(1970, 3, 1), p1: Date.UTC(1970, 3, 30)}
277+
{i: 1, b: 0, p: Date.UTC(1970, 1, 14, 12), s: 1, pts: [2], ph0: Date.UTC(1970, 1, 1), ph1: Date.UTC(1970, 1, 28)},
278+
{i: 2, b: 0, p: Date.UTC(1970, 2, 16), s: 0, pts: [], ph0: Date.UTC(1970, 2, 1), ph1: Date.UTC(1970, 2, 31)},
279+
{i: 3, b: 0, p: Date.UTC(1970, 3, 15, 12), s: 1, pts: [3], ph0: Date.UTC(1970, 3, 1), ph1: Date.UTC(1970, 3, 30)}
280280
]);
281281
});
282282

@@ -292,10 +292,10 @@ describe('Test histogram', function() {
292292
x3 = x2 + oneDay;
293293

294294
expect(out).toEqual([
295-
{i: 0, b: 0, p: x0, s: 2, pts: [0, 1], p0: x0, p1: x0},
296-
{i: 1, b: 0, p: x1, s: 1, pts: [2], p0: x1, p1: x1},
297-
{i: 2, b: 0, p: x2, s: 0, pts: [], p0: x2, p1: x2},
298-
{i: 3, b: 0, p: x3, s: 1, pts: [3], p0: x3, p1: x3}
295+
{i: 0, b: 0, p: x0, s: 2, pts: [0, 1], ph0: x0, ph1: x0},
296+
{i: 1, b: 0, p: x1, s: 1, pts: [2], ph0: x1, ph1: x1},
297+
{i: 2, b: 0, p: x2, s: 0, pts: [], ph0: x2, ph1: x2},
298+
{i: 3, b: 0, p: x3, s: 1, pts: [3], ph0: x3, ph1: x3}
299299
]);
300300
});
301301

@@ -319,7 +319,7 @@ describe('Test histogram', function() {
319319
});
320320

321321
expect(out).toEqual([
322-
{i: 0, b: 0, p: 3, s: 3, width1: 2, pts: [0, 1, 2], p0: 2, p1: 3.9}
322+
{i: 0, b: 0, p: 3, s: 3, width1: 2, pts: [0, 1, 2], ph0: 2, ph1: 3.9}
323323
]);
324324
});
325325

@@ -332,7 +332,7 @@ describe('Test histogram', function() {
332332
});
333333

334334
expect(out).toEqual([
335-
{i: 0, b: 0, p: 1.1, s: 3, width1: 0.5, pts: [0, 1, 2], p0: 1.1, p1: 1.1}
335+
{i: 0, b: 0, p: 1.1, s: 3, width1: 0.5, pts: [0, 1, 2], ph0: 1.1, ph1: 1.1}
336336
]);
337337
});
338338

@@ -345,7 +345,7 @@ describe('Test histogram', function() {
345345
});
346346

347347
expect(out).toEqual([
348-
{i: 0, b: 0, p: 17, s: 2, width1: 2, pts: [2, 4], p0: 17, p1: 17}
348+
{i: 0, b: 0, p: 17, s: 2, width1: 2, pts: [2, 4], ph0: 17, ph1: 17}
349349
]);
350350
});
351351

@@ -358,7 +358,7 @@ describe('Test histogram', function() {
358358
});
359359

360360
expect(out).toEqual([
361-
{i: 0, b: 0, p: 13, s: 2, width1: 8, pts: [1, 3], p0: 13, p1: 13}
361+
{i: 0, b: 0, p: 13, s: 2, width1: 8, pts: [1, 3], ph0: 13, ph1: 13}
362362
]);
363363
});
364364

@@ -372,7 +372,7 @@ describe('Test histogram', function() {
372372

373373
var p = 1296691200000;
374374
expect(out).toEqual([
375-
{i: 0, b: 0, p: p, s: 2, width1: 2 * 24 * 3600 * 1000, pts: [1, 3], p0: p, p1: p}
375+
{i: 0, b: 0, p: p, s: 2, width1: 2 * 24 * 3600 * 1000, pts: [1, 3], ph0: p, ph1: p}
376376
]);
377377
});
378378

@@ -385,15 +385,15 @@ describe('Test histogram', function() {
385385
});
386386

387387
expect(out).toEqual([
388-
{i: 0, b: 0, p: 97, s: 2, width1: 1, pts: [1, 3], p0: 97, p1: 97}
388+
{i: 0, b: 0, p: 97, s: 2, width1: 1, pts: [1, 3], ph0: 97, ph1: 97}
389389
]);
390390
});
391391

392392
it('can tell the difference between single-bin and single-value histograms', function() {
393393
var out = _calc({x: [1, 4]}, [], {barmode: 'overlay'});
394394

395395
expect(out).toEqual([
396-
{i: 0, b: 0, p: 2, s: 2, width1: 5, pts: [0, 1], p0: 0, p1: 4}
396+
{i: 0, b: 0, p: 2, s: 2, width1: 5, pts: [0, 1], ph0: 0, ph1: 4}
397397
]);
398398

399399
// real single-valued trace inherits bar width from the simply single-bin trace
@@ -404,7 +404,7 @@ describe('Test histogram', function() {
404404
});
405405

406406
expect(out).toEqual([
407-
{i: 0, b: 0, p: 5, s: 1, width1: 5, pts: [0], p0: 5, p1: 5}
407+
{i: 0, b: 0, p: 5, s: 1, width1: 5, pts: [0], ph0: 5, ph1: 5}
408408
]);
409409
});
410410

@@ -496,14 +496,14 @@ describe('Test histogram', function() {
496496
it('makes the right base histogram', function() {
497497
var baseOut = _calc(base);
498498
expect(baseOut).toEqual([
499-
{i: 0, b: 0, p: 2, s: 1, pts: [0], p0: 0, p1: 0},
500-
{i: 1, b: 0, p: 7, s: 2, pts: [1, 4], p0: 5, p1: 5},
501-
{i: 2, b: 0, p: 12, s: 3, pts: [2, 5, 7], p0: 10, p1: 10},
502-
{i: 3, b: 0, p: 17, s: 4, pts: [3, 6, 8, 9], p0: 15, p1: 15},
499+
{i: 0, b: 0, p: 2, s: 1, pts: [0], ph0: 0, ph1: 0},
500+
{i: 1, b: 0, p: 7, s: 2, pts: [1, 4], ph0: 5, ph1: 5},
501+
{i: 2, b: 0, p: 12, s: 3, pts: [2, 5, 7], ph0: 10, ph1: 10},
502+
{i: 3, b: 0, p: 17, s: 4, pts: [3, 6, 8, 9], ph0: 15, ph1: 15},
503503
]);
504504
});
505505

506-
// p0, p1, and pts have been omitted from CDFs for now
506+
// ph0, ph1, and pts have been omitted from CDFs for now
507507
var CDFs = [
508508
{p: [2, 7, 12, 17], s: [1, 3, 6, 10]},
509509
{

0 commit comments

Comments
 (0)