|
| 1 | +var Plotly = require('../lib/index'); |
| 2 | +var d3 = require('d3'); |
| 3 | +var createGraphDiv = require('../test/jasmine/assets/create_graph_div'); |
| 4 | +var destroyGraphDiv = require('../test/jasmine/assets/destroy_graph_div'); |
| 5 | +var pixelCalc = require('../test/jasmine/assets/pixel_calc'); |
| 6 | +var getSVGElemScreenBBox = require('../test/jasmine/assets/get_svg_elem_screen_bbox'); |
| 7 | +var Lib = require('../src/lib'); |
| 8 | +var Axes = require('../src/plots/cartesian/axes'); |
| 9 | +var axisIds = require('../src/plots/cartesian/axis_ids'); |
| 10 | + |
| 11 | +// NOTE: this tolerance is in pixels |
| 12 | +var EQUALITY_TOLERANCE = 1e-2; |
| 13 | + |
| 14 | +var DEBUG = true; |
| 15 | + |
| 16 | +var it = function(s,f) { |
| 17 | + console.log('testing ' + s); |
| 18 | + f(function() { console.log(s + ' is done.'); }); |
| 19 | +} |
| 20 | + |
| 21 | +// acts on an Object representing a shape which could be a line or a rect |
| 22 | +function shapeFromShapePos(shape,axletter,axnum,shapepos) { |
| 23 | + shape[axletter+'0'] = shapepos.value[0]; |
| 24 | + shape[axletter+'1'] = shapepos.value[1]; |
| 25 | + if (shapepos.ref === 'range') { |
| 26 | + shape[axletter+'ref'] = axletter + axnum; |
| 27 | + } else if (shapepos.ref === 'domain') { |
| 28 | + shape[axletter+'ref'] = axletter + axnum + ' domain'; |
| 29 | + } else if (shapepos.ref === 'paper') { |
| 30 | + shape[axletter+'ref'] = 'paper'; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// axid is e.g., 'x', 'y2' etc. |
| 35 | +function logAxisIfAxType(layoutIn,layoutOut,axid,axtype) { |
| 36 | + if (axtype === 'log') { |
| 37 | + var axname = axisIds.id2name(axid); |
| 38 | + var axis = {...layoutIn[axname]}; |
| 39 | + axis.type = 'log'; |
| 40 | + axis.range = axis.range.map(Math.log10); |
| 41 | + layoutOut[axname] = axis; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +// axref can be xref or yref |
| 47 | +// c can be x0, x1, y0, y1 |
| 48 | +function mapShapeCoordToPixel(layout,axref,shape,c) { |
| 49 | + var reftype = Axes.getRefType(shape[axref]); |
| 50 | + var axletter = axref[0]; |
| 51 | + var ret; |
| 52 | + if (reftype === 'range') { |
| 53 | + var axis = axisIds.id2name(shape[axref]); |
| 54 | + ret = pixelCalc.mapRangeToPixel(layout, axis, shape[c]); |
| 55 | + } else if (reftype === 'domain') { |
| 56 | + var axis = axisIds.id2name(shape[axref]); |
| 57 | + ret = pixelCalc.mapDomainToPixel(layout, axis, shape[c]); |
| 58 | + } else if (reftype === 'paper') { |
| 59 | + var axis = axref[0]; |
| 60 | + ret = pixelCalc.mapPaperToPixel(layout, axis, shape[c]); |
| 61 | + } |
| 62 | + return ret; |
| 63 | +} |
| 64 | + |
| 65 | +// compute the bounding box of the shape so that it can be compared with the SVG |
| 66 | +// bounding box |
| 67 | +function shapeToBBox(layout,shape) { |
| 68 | + var bbox = {}; |
| 69 | + var x1; |
| 70 | + var y1; |
| 71 | + // map x coordinates |
| 72 | + bbox.x = mapShapeCoordToPixel(layout,'xref',shape,'x0'); |
| 73 | + x1 = mapShapeCoordToPixel(layout,'xref',shape,'x1'); |
| 74 | + // SVG bounding boxes have x,y referring to top left corner, but here we are |
| 75 | + // specifying shapes where y0 refers to the bottom left corner like |
| 76 | + // Plotly.js, so we swap y0 and y1 |
| 77 | + bbox.y = mapShapeCoordToPixel(layout,'yref',shape,'y1'); |
| 78 | + y1 = mapShapeCoordToPixel(layout,'yref',shape,'y0'); |
| 79 | + bbox.width = x1 - bbox.x; |
| 80 | + bbox.height = y1 - bbox.y; |
| 81 | + return bbox; |
| 82 | +} |
| 83 | + |
| 84 | +function coordsEq(a,b) { |
| 85 | + return Math.abs(a - b) < EQUALITY_TOLERANCE; |
| 86 | +} |
| 87 | + |
| 88 | +function compareBBoxes(a,b) { |
| 89 | + return ['x','y','width','height'].map( |
| 90 | + (k,)=>coordsEq(a[k],b[k])).reduce( |
| 91 | + (l,r)=>l&&r, |
| 92 | + true); |
| 93 | +} |
| 94 | + |
| 95 | +// gets the SVG bounding box of the shape and checks it against what mapToPixel |
| 96 | +// gives |
| 97 | +function checkShapePosition(gd,shape) { |
| 98 | + var shapePath = d3.selectAll('path').filter(function () { |
| 99 | + return this.style.stroke === shape.line.color; |
| 100 | + }).node(); |
| 101 | + var shapePathBBox = getSVGElemScreenBBox(shapePath); |
| 102 | + var shapeBBox = shapeToBBox(gd.layout,shape); |
| 103 | + var ret = compareBBoxes(shapeBBox,shapePathBBox); |
| 104 | + if (DEBUG) { |
| 105 | + console.log('SVG BBox',shapePathBBox); |
| 106 | + console.log('shape BBox',shapeBBox); |
| 107 | + } |
| 108 | + return ret; |
| 109 | +} |
| 110 | + |
| 111 | +// some made-up values for testing |
| 112 | +var shapePositionsX = [ |
| 113 | + { |
| 114 | + // shapes referring to data |
| 115 | + ref: 'range', |
| 116 | + // two values for rects |
| 117 | + value: [2,3] |
| 118 | + }, |
| 119 | + { |
| 120 | + // shapes referring to domains |
| 121 | + ref: 'domain', |
| 122 | + value: [0.2,0.75], |
| 123 | + }, |
| 124 | + { |
| 125 | + // shapes referring to paper |
| 126 | + ref: 'paper', |
| 127 | + value: [0.25, 0.8] |
| 128 | + } |
| 129 | +]; |
| 130 | +var shapePositionsY = [ |
| 131 | + { |
| 132 | + // shapes referring to data |
| 133 | + ref: 'range', |
| 134 | + // two values for rects |
| 135 | + value: [1,2] |
| 136 | + }, |
| 137 | + { |
| 138 | + // shapes referring to domains |
| 139 | + ref: 'domain', |
| 140 | + value: [0.25,0.7], |
| 141 | + }, |
| 142 | + { |
| 143 | + // shapes referring to paper |
| 144 | + ref: 'paper', |
| 145 | + value: [0.2, 0.85] |
| 146 | + } |
| 147 | +]; |
| 148 | +var axisTypes = [ 'linear', 'log' ]; |
| 149 | +// Test on 'x', 'y', 'x2', 'y2' axes |
| 150 | +// TODO the 'paper' position references are tested twice when once would |
| 151 | +// suffice. |
| 152 | +var axNum = ['','2']; |
| 153 | +// only test line and rect for now |
| 154 | +var shapeType = ['line','rect']; |
| 155 | +// this color chosen so it can easily be found with d3 |
| 156 | +var shapeColor = 'rgb(50, 100, 150)'; |
| 157 | +var testDomRefShapeCombo = function(combo) { |
| 158 | + var xAxNum = combo[0]; |
| 159 | + var xaxisType = combo[1]; |
| 160 | + var xshapePos = combo[2]; |
| 161 | + var yAxNum = combo[3]; |
| 162 | + var yaxisType = combo[4]; |
| 163 | + var yshapePos = combo[5]; |
| 164 | + var shapeType = combo[6]; |
| 165 | + it('should draw a ' + shapeType |
| 166 | + + ' for x' + xAxNum + ' of type ' |
| 167 | + + xaxisType |
| 168 | + + ' with a value referencing ' |
| 169 | + + xshapePos.ref |
| 170 | + + ' and for y' + yAxNum + ' of type ' |
| 171 | + + yaxisType |
| 172 | + + ' with a value referencing ' |
| 173 | + + yshapePos.ref, |
| 174 | + function (done) { |
| 175 | + var gd = createGraphDiv(); |
| 176 | + var mock = Lib.extendDeep({}, |
| 177 | + require('../test/image/mocks/domain_ref_base.json')); |
| 178 | + if (DEBUG) { |
| 179 | + console.log(combo); |
| 180 | + } |
| 181 | + Plotly.newPlot(gd, mock) |
| 182 | + var shape = { |
| 183 | + type: shapeType, |
| 184 | + line: { color: shapeColor } |
| 185 | + }; |
| 186 | + shapeFromShapePos(shape,'x',xAxNum,xshapePos); |
| 187 | + shapeFromShapePos(shape,'y',yAxNum,yshapePos); |
| 188 | + var layout = {shapes: [shape]}; |
| 189 | + // change to log axes if need be |
| 190 | + logAxisIfAxType(gd.layout,layout,'x'+xAxNum,xaxisType); |
| 191 | + logAxisIfAxType(gd.layout,layout,'y'+yAxNum,yaxisType); |
| 192 | + Plotly.relayout(gd,layout); |
| 193 | + console.log(checkShapePosition(gd,shape)); |
| 194 | + destroyGraphDiv(); |
| 195 | + }); |
| 196 | +} |
| 197 | + |
| 198 | +// Test correct shape positions |
| 199 | +function test_correct_shape_positions () { |
| 200 | + var iterable = require('extra-iterable'); |
| 201 | + // for both x and y axes |
| 202 | + var testCombos = [...iterable.cartesianProduct([ |
| 203 | + axNum,axisTypes,shapePositionsX,axNum,axisTypes,shapePositionsY,shapeType |
| 204 | + ])]; |
| 205 | + // map all the combinations to a shape definition and check this shape is |
| 206 | + // placed properly |
| 207 | + testCombos.forEach(testDomRefShapeCombo); |
| 208 | +} |
| 209 | + |
| 210 | +test_correct_shape_positions(); |
0 commit comments