|
| 1 | +var Plotly = require('@lib'); |
| 2 | +var Lib = require('@src/lib'); |
| 3 | +var ScatterTernary = require('@src/traces/scatterternary'); |
| 4 | + |
| 5 | +var d3 = require('d3'); |
| 6 | +var createGraphDiv = require('../assets/create_graph_div'); |
| 7 | +var destroyGraphDiv = require('../assets/destroy_graph_div'); |
| 8 | +var customMatchers = require('../assets/custom_matchers'); |
| 9 | + |
| 10 | + |
| 11 | +describe('scatterternary defaults', function() { |
| 12 | + 'use strict'; |
| 13 | + |
| 14 | + var supplyDefaults = ScatterTernary.supplyDefaults; |
| 15 | + |
| 16 | + var traceIn, traceOut; |
| 17 | + |
| 18 | + var defaultColor = '#444', |
| 19 | + layout = {}; |
| 20 | + |
| 21 | + beforeEach(function() { |
| 22 | + traceOut = {}; |
| 23 | + }); |
| 24 | + |
| 25 | + it('should allow one of \'a\', \'b\' or \'c\' to be missing (base case)', function() { |
| 26 | + traceIn = { |
| 27 | + a: [1, 2, 3], |
| 28 | + b: [1, 2, 3], |
| 29 | + c: [1, 2, 3] |
| 30 | + }; |
| 31 | + |
| 32 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 33 | + expect(traceOut.visible).not.toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should allow one of \'a\', \'b\' or \'c\' to be missing (\'c\' is missing case)', function() { |
| 37 | + traceIn = { |
| 38 | + a: [1, 2, 3], |
| 39 | + b: [1, 2, 3] |
| 40 | + }; |
| 41 | + |
| 42 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 43 | + expect(traceOut.visible).not.toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should allow one of \'a\', \'b\' or \'c\' to be missing (\'b\' is missing case)', function() { |
| 47 | + traceIn = { |
| 48 | + a: [1, 2, 3], |
| 49 | + c: [1, 2, 3] |
| 50 | + }; |
| 51 | + |
| 52 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 53 | + expect(traceOut.visible).not.toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should allow one of \'a\', \'b\' or \'c\' to be missing (\'a\' is missing case)', function() { |
| 57 | + traceIn = { |
| 58 | + b: [1, 2, 3], |
| 59 | + c: [1, 2, 3] |
| 60 | + }; |
| 61 | + |
| 62 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 63 | + expect(traceOut.visible).not.toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should allow one of \'a\', \'b\' or \'c\' to be missing (\'b\ and \'c\' are missing case)', function() { |
| 67 | + traceIn = { |
| 68 | + a: [1, 2, 3] |
| 69 | + }; |
| 70 | + |
| 71 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 72 | + expect(traceOut.visible).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should allow one of \'a\', \'b\' or \'c\' to be missing (\'a\ and \'c\' are missing case)', function() { |
| 76 | + traceIn = { |
| 77 | + b: [1, 2, 3] |
| 78 | + }; |
| 79 | + |
| 80 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 81 | + expect(traceOut.visible).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should allow one of \'a\', \'b\' or \'c\' to be missing (\'a\ and \'b\' are missing case)', function() { |
| 85 | + traceIn = { |
| 86 | + c: [1, 2, 3] |
| 87 | + }; |
| 88 | + |
| 89 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 90 | + expect(traceOut.visible).toBe(false); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should allow one of \'a\', \'b\' or \'c\' to be missing (all are missing case)', function() { |
| 94 | + traceIn = {}; |
| 95 | + |
| 96 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 97 | + expect(traceOut.visible).toBe(false); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should truncate data arrays to the same length (\'c\' is shortest case)', function() { |
| 101 | + traceIn = { |
| 102 | + a: [1, 2, 3], |
| 103 | + b: [1, 2], |
| 104 | + c: [1] |
| 105 | + }; |
| 106 | + |
| 107 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 108 | + expect(traceOut.a).toEqual([1]); |
| 109 | + expect(traceOut.b).toEqual([1]); |
| 110 | + expect(traceOut.c).toEqual([1]); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should truncate data arrays to the same length (\'a\' is shortest case)', function() { |
| 114 | + traceIn = { |
| 115 | + a: [1], |
| 116 | + b: [1, 2, 3], |
| 117 | + c: [1, 2] |
| 118 | + }; |
| 119 | + |
| 120 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 121 | + expect(traceOut.a).toEqual([1]); |
| 122 | + expect(traceOut.b).toEqual([1]); |
| 123 | + expect(traceOut.c).toEqual([1]); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should truncate data arrays to the same length (\'a\' is shortest case)', function() { |
| 127 | + traceIn = { |
| 128 | + a: [1, 2], |
| 129 | + b: [1], |
| 130 | + c: [1, 2, 3] |
| 131 | + }; |
| 132 | + |
| 133 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 134 | + expect(traceOut.a).toEqual([1]); |
| 135 | + expect(traceOut.b).toEqual([1]); |
| 136 | + expect(traceOut.c).toEqual([1]); |
| 137 | + }); |
| 138 | + it('should include \'name\' in \'hoverinfo\' default if multi trace graph', function() { |
| 139 | + traceIn = { |
| 140 | + a: [1, 2, 3], |
| 141 | + b: [1, 2, 3], |
| 142 | + c: [1, 2, 3] |
| 143 | + }; |
| 144 | + layout._dataLength = 2; |
| 145 | + |
| 146 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 147 | + expect(traceOut.hoverinfo).toBe('all'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should not include \'name\' in \'hoverinfo\' default if single trace graph', function() { |
| 151 | + traceIn = { |
| 152 | + a: [1, 2, 3], |
| 153 | + b: [1, 2, 3], |
| 154 | + c: [1, 2, 3] |
| 155 | + }; |
| 156 | + layout._dataLength = 1; |
| 157 | + |
| 158 | + supplyDefaults(traceIn, traceOut, defaultColor, layout); |
| 159 | + expect(traceOut.hoverinfo).toBe('a+b+c+text'); |
| 160 | + }); |
| 161 | +}); |
| 162 | + |
| 163 | +describe('scatterternary calc', function() { |
| 164 | + 'use strict'; |
| 165 | + |
| 166 | + var calc = ScatterTernary.calc; |
| 167 | + |
| 168 | + beforeAll(function() { |
| 169 | + jasmine.addMatchers(customMatchers); |
| 170 | + }); |
| 171 | + |
| 172 | + var gd, trace, cd; |
| 173 | + |
| 174 | + beforeEach(function() { |
| 175 | + gd = { |
| 176 | + _fullLayout: { |
| 177 | + ternary: { sum: 1 } |
| 178 | + } |
| 179 | + }; |
| 180 | + |
| 181 | + trace = { |
| 182 | + subplot: 'ternary', |
| 183 | + sum: 1 |
| 184 | + }; |
| 185 | + }); |
| 186 | + |
| 187 | + it('should fill in missing component (case \'c\')', function() { |
| 188 | + trace.a = [0.1, 0.3, 0.6]; |
| 189 | + trace.b = [0.3, 0.6, 0.1]; |
| 190 | + |
| 191 | + calc(gd, trace); |
| 192 | + expect(trace.c).toBeCloseToArray([0.6, 0.1, 0.3]); |
| 193 | + }); |
| 194 | + |
| 195 | + it('should fill in missing component (case \'b\')', function() { |
| 196 | + trace.a = [0.1, 0.3, 0.6]; |
| 197 | + trace.c = [0.1, 0.3, 0.2]; |
| 198 | + |
| 199 | + calc(gd, trace); |
| 200 | + expect(trace.b).toBeCloseToArray([0.8, 0.4, 0.2]); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should fill in missing component (case \'a\')', function() { |
| 204 | + trace.b = [0.1, 0.3, 0.6]; |
| 205 | + trace.c = [0.8, 0.4, 0.1]; |
| 206 | + |
| 207 | + calc(gd, trace); |
| 208 | + expect(trace.a).toBeCloseToArray([0.1, 0.3, 0.3]); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should skip over non-numeric values', function() { |
| 212 | + trace.a = [0.1, 'a', 0.6]; |
| 213 | + trace.b = [0.1, 0.3, null]; |
| 214 | + trace.c = [8, 0.4, 0.1]; |
| 215 | + |
| 216 | + cd = calc(gd, trace); |
| 217 | + |
| 218 | + expect(objectToArray(cd[0])).toBeCloseToArray([ |
| 219 | + 0.963414634, 0.012195121, 0.012195121, 0.012195121, 0.975609756 |
| 220 | + ]); |
| 221 | + expect(cd[1]).toEqual({ x: false, y: false }); |
| 222 | + expect(cd[2]).toEqual({ x: false, y: false }); |
| 223 | + }); |
| 224 | + |
| 225 | + function objectToArray(obj) { |
| 226 | + return Object.keys(obj).map(function(k) { |
| 227 | + return obj[k]; |
| 228 | + }); |
| 229 | + } |
| 230 | + |
| 231 | +}); |
| 232 | + |
| 233 | +describe('scatterternary plot and hover', function() { |
| 234 | + 'use strict'; |
| 235 | + |
| 236 | + var mock = require('@mocks/ternary_simple.json'); |
| 237 | + |
| 238 | + afterAll(destroyGraphDiv); |
| 239 | + |
| 240 | + beforeAll(function(done) { |
| 241 | + var gd = createGraphDiv(); |
| 242 | + var mockCopy = Lib.extendDeep({}, mock); |
| 243 | + |
| 244 | + Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done); |
| 245 | + }); |
| 246 | + |
| 247 | + it('should put scatterternary trace in \'frontplot\' node', function() { |
| 248 | + var nodes = d3.select('.frontplot').selectAll('.scatter'); |
| 249 | + |
| 250 | + expect(nodes.size()).toEqual(1); |
| 251 | + }); |
| 252 | + |
| 253 | + it('should generate one line path per trace', function() { |
| 254 | + var nodes = d3.selectAll('path.js-line'); |
| 255 | + |
| 256 | + expect(nodes.size()).toEqual(mock.data.length); |
| 257 | + }); |
| 258 | + |
| 259 | + it('should generate as many points as there are data points', function() { |
| 260 | + var nodes = d3.selectAll('path.point'); |
| 261 | + |
| 262 | + expect(nodes.size()).toEqual(mock.data[0].a.length); |
| 263 | + }); |
| 264 | +}); |
| 265 | + |
| 266 | +fdescribe('scatterternary hover', function() { |
| 267 | + 'use strict'; |
| 268 | + |
| 269 | + var hoverPoints = ScatterTernary.hoverPoints; |
| 270 | + |
| 271 | + var gd, pointData; |
| 272 | + |
| 273 | + beforeAll(function(done) { |
| 274 | + gd = createGraphDiv(); |
| 275 | + |
| 276 | + var data = [{ |
| 277 | + type: 'scatterternary', |
| 278 | + a: [0.1, 0.2, 0.3], |
| 279 | + b: [0.3, 0.2, 0.1], |
| 280 | + c: [0.1, 0.4, 0.5] |
| 281 | + }]; |
| 282 | + |
| 283 | + Plotly.plot(gd, data).then(done); |
| 284 | + }); |
| 285 | + |
| 286 | + beforeEach(function() { |
| 287 | + var cd = gd.calcdata, |
| 288 | + ternary = gd._fullLayout.ternary._ternary; |
| 289 | + |
| 290 | + pointData = { |
| 291 | + index: false, |
| 292 | + distance: 20, |
| 293 | + cd: cd[0], |
| 294 | + trace: cd[0][0].trace, |
| 295 | + xa: ternary.xaxis, |
| 296 | + ya: ternary.yaxis |
| 297 | + }; |
| 298 | + |
| 299 | + }); |
| 300 | + |
| 301 | + afterAll(destroyGraphDiv); |
| 302 | + |
| 303 | + it('should generate extra text field on hover', function() { |
| 304 | + var xval = 0.42, |
| 305 | + yval = 0.37, |
| 306 | + hovermode = 'closest'; |
| 307 | + |
| 308 | + var scatterPointData = hoverPoints(pointData, xval, yval, hovermode); |
| 309 | + |
| 310 | + expect(scatterPointData[0].extraText).toEqual( |
| 311 | + 'Component A: 0.3333333<br>Component B: 0.1111111<br>Component C: 0.5555556' |
| 312 | + ); |
| 313 | + |
| 314 | + expect(scatterPointData[0].xLabelVal).toBeUndefined(); |
| 315 | + expect(scatterPointData[0].yLabelVal).toBeUndefined(); |
| 316 | + }); |
| 317 | + |
| 318 | +}); |
0 commit comments