Skip to content

Commit d3b3dfb

Browse files
committed
add new errorbar plot (now draws in the trace layers)
1 parent af99cc3 commit d3b3dfb

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

src/components/errorbars/plot.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var d3 = require('d3');
13+
var isNumeric = require('fast-isnumeric');
14+
15+
var Lib = require('../../lib');
16+
var subTypes = require('../../traces/scatter/subtypes');
17+
18+
19+
module.exports = function plot(traces, plotinfo) {
20+
var xa = plotinfo.x(),
21+
ya = plotinfo.y();
22+
23+
traces.each(function(d) {
24+
var trace = d[0].trace,
25+
xObj = trace.error_x,
26+
yObj = trace.error_y;
27+
28+
var sparse = (
29+
subTypes.hasMarkers(trace) &&
30+
trace.marker.maxdisplayed > 0
31+
);
32+
33+
if(!yObj.visible && !xObj.visible) return;
34+
35+
var errorbars = d3.select(this).selectAll('g.errorbar')
36+
.data(Lib.identity);
37+
38+
errorbars.enter().append('g')
39+
.classed('errorbar', true);
40+
41+
errorbars.each(function(d) {
42+
var errorbar = d3.select(this);
43+
var coords = errorCoords(d, xa, ya);
44+
45+
if(sparse && !d.vis) return;
46+
47+
var path;
48+
49+
if(yObj.visible && isNumeric(coords.x) &&
50+
isNumeric(coords.yh) &&
51+
isNumeric(coords.ys)) {
52+
var yw = yObj.width;
53+
54+
path = 'M' + (coords.x - yw) + ',' +
55+
coords.yh + 'h' + (2 * yw) + // hat
56+
'm-' + yw + ',0V' + coords.ys; // bar
57+
58+
if(!coords.noYS) path += 'm-' + yw +',0h' + (2 * yw); // shoe
59+
60+
errorbar.append('path')
61+
.classed('yerror', true)
62+
.attr('d', path);
63+
}
64+
65+
if(xObj.visible && isNumeric(coords.y) &&
66+
isNumeric(coords.xh) &&
67+
isNumeric(coords.xs)) {
68+
var xw = (xObj.copy_ystyle ? yObj : xObj).width;
69+
70+
path = 'M' + coords.xh + ',' +
71+
(coords.y - xw) + 'v' + (2 * xw) + // hat
72+
'm0,-' + xw + 'H' + coords.xs; // bar
73+
74+
if(!coords.noXS) path += 'm0,-' + xw + 'v' + (2 * xw); // shoe
75+
76+
errorbar.append('path')
77+
.classed('xerror', true)
78+
.attr('d', path);
79+
}
80+
});
81+
});
82+
};
83+
84+
// compute the coordinates of the error-bar objects
85+
function errorCoords(d, xa, ya) {
86+
var out = {
87+
x: xa.c2p(d.x),
88+
y: ya.c2p(d.y)
89+
};
90+
91+
// calculate the error bar size and hat and shoe locations
92+
if(d.yh !== undefined) {
93+
out.yh = ya.c2p(d.yh);
94+
out.ys = ya.c2p(d.ys);
95+
96+
// if the shoes go off-scale (ie log scale, error bars past zero)
97+
// clip the bar and hide the shoes
98+
if(!isNumeric(out.ys)) {
99+
out.noYS = true;
100+
out.ys = ya.c2p(d.ys, true);
101+
}
102+
}
103+
104+
if(d.xh !== undefined) {
105+
out.xh = xa.c2p(d.xh);
106+
out.xs = xa.c2p(d.xs);
107+
108+
if(!isNumeric(out.xs)) {
109+
out.noXS = true;
110+
out.xs = xa.c2p(d.xs, true);
111+
}
112+
}
113+
114+
return out;
115+
}

0 commit comments

Comments
 (0)