|
| 1 | +var Plots = require('@src/plots/plots'); |
| 2 | +var Lib = require('@src/lib'); |
| 3 | + |
1 | 4 | var Bar = require('@src/traces/bar');
|
2 | 5 |
|
| 6 | +var customMatchers = require('../assets/custom_matchers'); |
| 7 | + |
3 | 8 | describe('bar supplyDefaults', function() {
|
4 | 9 | 'use strict';
|
5 | 10 |
|
@@ -56,17 +61,126 @@ describe('bar supplyDefaults', function() {
|
56 | 61 | });
|
57 | 62 | });
|
58 | 63 |
|
| 64 | +describe('heatmap calc / setPositions', function() { |
59 | 65 | 'use strict';
|
60 | 66 |
|
| 67 | + beforeAll(function() { |
| 68 | + jasmine.addMatchers(customMatchers); |
| 69 | + }); |
| 70 | + |
| 71 | + function _calc(dataOpts, layout) { |
| 72 | + var baseData = { type: 'bar' }; |
| 73 | + |
| 74 | + var data = dataOpts.map(function(traceOpts) { |
| 75 | + return Lib.extendFlat({}, baseData, traceOpts); |
| 76 | + }); |
| 77 | + |
| 78 | + var gd = { |
| 79 | + data: data, |
| 80 | + layout: layout, |
| 81 | + calcdata: [] |
| 82 | + }; |
| 83 | + |
| 84 | + Plots.supplyDefaults(gd); |
| 85 | + |
| 86 | + gd._fullData.forEach(function(fullTrace) { |
| 87 | + var cd = Bar.calc(gd, fullTrace); |
| 88 | + |
| 89 | + cd[0].t = {}; |
| 90 | + cd[0].trace = fullTrace; |
| 91 | + |
| 92 | + gd.calcdata.push(cd); |
| 93 | + }); |
| 94 | + |
| 95 | + var plotinfo = { |
| 96 | + x: function() { return gd._fullLayout.xaxis; }, |
| 97 | + y: function() { return gd._fullLayout.yaxis; } |
| 98 | + }; |
| 99 | + |
| 100 | + Bar.setPositions(gd, plotinfo); |
| 101 | + |
| 102 | + return gd.calcdata; |
| 103 | + } |
61 | 104 |
|
| 105 | + function assertPtField(calcData, prop, expectation) { |
| 106 | + var values = []; |
| 107 | + |
| 108 | + calcData.forEach(function(calcTrace) { |
| 109 | + var vals = calcTrace.map(function(pt) { |
| 110 | + return Lib.nestedProperty(pt, prop).get(); |
| 111 | + }); |
| 112 | + |
| 113 | + values.push(vals); |
| 114 | + }); |
62 | 115 |
|
| 116 | + expect(values).toBeCloseTo2DArray(expectation, undefined, '- field ' + prop); |
| 117 | + } |
63 | 118 |
|
| 119 | + function assertTraceField(calcData, prop, expectation) { |
| 120 | + var values = calcData.map(function(calcTrace) { |
| 121 | + return Lib.nestedProperty(calcTrace[0], prop).get(); |
64 | 122 | });
|
65 | 123 |
|
| 124 | + expect(values).toBeCloseToArray(expectation, undefined, '- field ' + prop); |
| 125 | + } |
| 126 | + |
| 127 | + it('should fill in calc pt fields (stack case)', function() { |
| 128 | + var out = _calc([{ |
| 129 | + y: [2, 1, 2] |
| 130 | + }, { |
| 131 | + y: [3, 1, 2] |
| 132 | + }, { |
| 133 | + y: [null, null, 2] |
| 134 | + }], { |
| 135 | + barmode: 'stack' |
66 | 136 | });
|
67 | 137 |
|
| 138 | + assertPtField(out, 'x', [[0, 1, 2], [0, 1, 2], [0, 1, 2]]); |
| 139 | + assertPtField(out, 'y', [[2, 1, 2], [5, 2, 4], [undefined, undefined, 6]]); |
| 140 | + assertPtField(out, 'b', [[0, 0, 0], [2, 1, 2], [0, 0, 4]]); |
| 141 | + assertPtField(out, 's', [[2, 1, 2], [3, 1, 2], [undefined, undefined, 2]]); |
| 142 | + assertPtField(out, 'p', [[0, 1, 2], [0, 1, 2], [0, 1, 2]]); |
| 143 | + assertTraceField(out, 't.barwidth', [0.8, 0.8, 0.8]); |
| 144 | + assertTraceField(out, 't.poffset', [-0.4, -0.4, -0.4]); |
| 145 | + assertTraceField(out, 't.dbar', [1, 1, 1]); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should fill in calc pt fields (overlay case)', function() { |
| 149 | + var out = _calc([{ |
| 150 | + y: [2, 1, 2] |
| 151 | + }, { |
| 152 | + y: [3, 1, 2] |
| 153 | + }], { |
| 154 | + barmode: 'overlay' |
| 155 | + }); |
| 156 | + |
| 157 | + assertPtField(out, 'x', [[0, 1, 2], [0, 1, 2]]); |
| 158 | + assertPtField(out, 'y', [[2, 1, 2], [3, 1, 2]]); |
| 159 | + assertPtField(out, 'b', [[0, 0, 0], [0, 0, 0]]); |
| 160 | + assertPtField(out, 's', [[2, 1, 2], [3, 1, 2]]); |
| 161 | + assertPtField(out, 'p', [[0, 1, 2], [0, 1, 2]]); |
| 162 | + assertTraceField(out, 't.barwidth', [0.8, 0.8]); |
| 163 | + assertTraceField(out, 't.poffset', [-0.4, -0.4]); |
| 164 | + assertTraceField(out, 't.dbar', [1, 1]); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should fill in calc pt fields (group case)', function() { |
| 168 | + var out = _calc([{ |
| 169 | + y: [2, 1, 2] |
| 170 | + }, { |
| 171 | + y: [3, 1, 2] |
| 172 | + }], { |
| 173 | + barmode: 'group' |
68 | 174 | });
|
69 | 175 |
|
| 176 | + assertPtField(out, 'x', [[-0.2, 0.8, 1.8], [0.2, 1.2, 2.2]]); |
| 177 | + assertPtField(out, 'y', [[2, 1, 2], [3, 1, 2]]); |
| 178 | + assertPtField(out, 'b', [[0, 0, 0], [0, 0, 0]]); |
| 179 | + assertPtField(out, 's', [[2, 1, 2], [3, 1, 2]]); |
| 180 | + assertPtField(out, 'p', [[0, 1, 2], [0, 1, 2]]); |
| 181 | + assertTraceField(out, 't.barwidth', [0.4, 0.4]); |
| 182 | + assertTraceField(out, 't.poffset', [-0.4, 0]); |
| 183 | + assertTraceField(out, 't.dbar', [1, 1]); |
70 | 184 | });
|
71 | 185 |
|
72 | 186 | });
|
0 commit comments