|
| 1 | +var Plotly = require('@lib/index'); |
1 | 2 | var Plots = require('@src/plots/plots');
|
2 | 3 | var Lib = require('@src/lib');
|
3 | 4 |
|
4 | 5 | var convertColumnXYZ = require('@src/traces/heatmap/convert_column_xyz');
|
5 | 6 | var Heatmap = require('@src/traces/heatmap');
|
6 | 7 |
|
| 8 | +var d3 = require('d3'); |
| 9 | +var createGraphDiv = require('../assets/create_graph_div'); |
| 10 | +var destroyGraphDiv = require('../assets/destroy_graph_div'); |
7 | 11 | var customMatchers = require('../assets/custom_matchers');
|
8 | 12 |
|
9 | 13 |
|
@@ -299,3 +303,97 @@ describe('heatmap calc', function() {
|
299 | 303 | expect(out.z).toBeCloseTo2DArray([[1, 2, 3], [3, 1, 2]]);
|
300 | 304 | });
|
301 | 305 | });
|
| 306 | + |
| 307 | +describe('heatmap plot', function() { |
| 308 | + 'use strict'; |
| 309 | + |
| 310 | + afterEach(destroyGraphDiv); |
| 311 | + |
| 312 | + it('should not draw traces that are off-screen', function(done) { |
| 313 | + var mock = require('@mocks/heatmap_multi-trace.json'), |
| 314 | + mockCopy = Lib.extendDeep({}, mock), |
| 315 | + gd = createGraphDiv(); |
| 316 | + |
| 317 | + function assertImageCnt(cnt) { |
| 318 | + var images = d3.selectAll('.hm').select('image'); |
| 319 | + |
| 320 | + expect(images.size()).toEqual(cnt); |
| 321 | + } |
| 322 | + |
| 323 | + Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() { |
| 324 | + assertImageCnt(5); |
| 325 | + |
| 326 | + return Plotly.relayout(gd, 'xaxis.range', [2, 3]); |
| 327 | + }).then(function() { |
| 328 | + assertImageCnt(2); |
| 329 | + |
| 330 | + return Plotly.relayout(gd, 'xaxis.autorange', true); |
| 331 | + }).then(function() { |
| 332 | + assertImageCnt(5); |
| 333 | + |
| 334 | + done(); |
| 335 | + }); |
| 336 | + }); |
| 337 | +}); |
| 338 | + |
| 339 | +describe('heatmap hover', function() { |
| 340 | + 'use strict'; |
| 341 | + |
| 342 | + var gd; |
| 343 | + |
| 344 | + beforeAll(function(done) { |
| 345 | + jasmine.addMatchers(customMatchers); |
| 346 | + |
| 347 | + gd = createGraphDiv(); |
| 348 | + |
| 349 | + var mock = require('@mocks/heatmap_multi-trace.json'), |
| 350 | + mockCopy = Lib.extendDeep({}, mock); |
| 351 | + |
| 352 | + Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done); |
| 353 | + }); |
| 354 | + |
| 355 | + afterAll(destroyGraphDiv); |
| 356 | + |
| 357 | + function _hover(gd, xval, yval) { |
| 358 | + var fullLayout = gd._fullLayout, |
| 359 | + calcData = gd.calcdata, |
| 360 | + hoverData = []; |
| 361 | + |
| 362 | + for(var i = 0; i < calcData.length; i++) { |
| 363 | + var pointData = { |
| 364 | + index: false, |
| 365 | + distance: 20, |
| 366 | + cd: calcData[i], |
| 367 | + trace: calcData[i][0].trace, |
| 368 | + xa: fullLayout.xaxis, |
| 369 | + ya: fullLayout.yaxis |
| 370 | + }; |
| 371 | + |
| 372 | + var hoverPoint = Heatmap.hoverPoints(pointData, xval, yval); |
| 373 | + if(hoverPoint) hoverData.push(hoverPoint[0]); |
| 374 | + } |
| 375 | + |
| 376 | + return hoverData; |
| 377 | + } |
| 378 | + |
| 379 | + function assertLabels(hoverPoint, xLabel, yLabel, zLabel) { |
| 380 | + expect(hoverPoint.xLabelVal).toEqual(xLabel, 'have correct x label'); |
| 381 | + expect(hoverPoint.yLabelVal).toEqual(yLabel, 'have correct y label'); |
| 382 | + expect(hoverPoint.zLabelVal).toEqual(zLabel, 'have correct z label'); |
| 383 | + } |
| 384 | + |
| 385 | + it('should find closest point (case 1) and should', function() { |
| 386 | + var pt = _hover(gd, 0.5, 0.5)[0]; |
| 387 | + |
| 388 | + expect(pt.index).toEqual([1, 0], 'have correct index'); |
| 389 | + assertLabels(pt, 1, 1, 4); |
| 390 | + }); |
| 391 | + |
| 392 | + it('should find closest point (case 2) and should', function() { |
| 393 | + var pt = _hover(gd, 1.5, 0.5)[0]; |
| 394 | + |
| 395 | + expect(pt.index).toEqual([0, 0], 'have correct index'); |
| 396 | + assertLabels(pt, 2, 0.2, 6); |
| 397 | + }); |
| 398 | + |
| 399 | +}); |
0 commit comments