|
| 1 | +import { Vertex } from "../../instrumented/baseShape"; |
| 2 | +import { Rect } from "../../instrumented/primitives"; |
| 3 | +import { Axis, ParallelAxis } from "../../instrumented/axes"; |
| 4 | + |
| 5 | +const vector = [1, 2, 3, 4, 5]; |
| 6 | +const boundingBox = new Rect(new Vertex(0, 0), new Vertex(500, 500)); |
| 7 | +const origin = new Vertex(0, 0); |
| 8 | +const end = new Vertex(0, 100); |
| 9 | +const name = ""; |
| 10 | +const initScale = new Vertex(1, 1); |
| 11 | +const nTicks = 5; |
| 12 | + |
| 13 | +describe('Axis', function() { |
| 14 | + it('should draw the axis on the canvas', function() { |
| 15 | + const canvas = document.createElement('canvas'); |
| 16 | + const context = canvas.getContext('2d'); |
| 17 | + const axis = new Axis(vector, boundingBox, origin, end, name, initScale, nTicks); |
| 18 | + |
| 19 | + cy.spy(context, 'fill'); |
| 20 | + cy.spy(context, 'stroke'); |
| 21 | + |
| 22 | + axis.draw(context); |
| 23 | + |
| 24 | + cy.wrap(context.fill).should('have.been.calledWith', axis.drawPath); |
| 25 | + cy.wrap(context.stroke).should('have.been.calledWith', axis.drawPath); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should be well created without vector features', function() { |
| 29 | + const axis = new Axis(null, boundingBox, origin, end, name, initScale, nTicks); |
| 30 | + const ticks = [-1, 0, 1, 2] |
| 31 | + expect(axis.ticks).to.deep.equal(ticks); |
| 32 | + expect(axis.isDiscrete, `isDiscrete`).to.be.true; |
| 33 | +}); |
| 34 | + |
| 35 | + it('should be well created with empty vector features', function() { |
| 36 | + const axis = new Axis([], boundingBox, origin, end, name, initScale, nTicks); |
| 37 | + const ticks = [-1, 0, 1, 2] |
| 38 | + axis.ticks.forEach((tick, index) => expect(tick, `tick ${index}`).to.equal(ticks[index])); |
| 39 | + expect(axis.isDiscrete, `isDiscrete`).to.be.true; |
| 40 | + }); |
| 41 | + |
| 42 | + it('should be well created without only one feature', function() { |
| 43 | + const axis = new Axis([1], boundingBox, origin, end, name, initScale, nTicks); |
| 44 | + expect(axis.ticks.length, `length ticks`).to.equal(7); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should scale axis with another one", function() { |
| 48 | + const axis = new Axis(vector, boundingBox, origin, end, name, initScale, nTicks); |
| 49 | + const otherVector = [10, 11, 12, 13, 14, 32.4]; |
| 50 | + const otherAxis = new Axis(otherVector, boundingBox, origin, end, name, initScale, nTicks); |
| 51 | + axis.otherAxisScaling(otherAxis); |
| 52 | + expect(axis.minValue, "minValue").to.be.closeTo(-9.3, 0.1); |
| 53 | + expect(axis.maxValue, "maxValue").to.be.closeTo(15.3, 0.1); |
| 54 | + expect((axis.maxValue - axis.minValue) / axis.drawLength, "ratio").to.be.closeTo((otherAxis.maxValue - otherAxis.minValue) / otherAxis.drawLength, 0.01); |
| 55 | + }) |
| 56 | + |
| 57 | + it('should update the origin and end points of the axis', function() { |
| 58 | + const axis = new Axis(vector, boundingBox, origin, end, name, initScale, nTicks); |
| 59 | + const newOrigin = new Vertex(100, 100); |
| 60 | + const newEnd = new Vertex(100, 200); |
| 61 | + axis.transform(newOrigin, newEnd); |
| 62 | + |
| 63 | + expect(axis.origin.x, "origin.x").to.equal(newOrigin.x); |
| 64 | + expect(axis.end.y, "end.y").to.equal(newEnd.y); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should change axis scale', function() { |
| 68 | + const axis = new Axis(vector, boundingBox, origin, end, name, initScale, nTicks); |
| 69 | + const viewPoint = new Vertex(20, 20); |
| 70 | + const scaling = new Vertex(2, 0.5); |
| 71 | + const translation = new Vertex(); |
| 72 | + axis.updateScale(viewPoint, scaling, translation); |
| 73 | + |
| 74 | + expect(axis.minValue, "minValue").to.be.closeTo(-0.08, 0.005); |
| 75 | + expect(axis.maxValue, "maxValue").to.be.closeTo(8.7, 0.05); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should update axis with translation and style', function() { |
| 79 | + const axis = new Axis(vector, boundingBox, origin, end, name, initScale, nTicks); |
| 80 | + const viewPoint = new Vertex(0, 0); |
| 81 | + const scaling = new Vertex(1, 1); |
| 82 | + const translation = new Vertex(4 * axis.transformMatrix.a, 4 * axis.transformMatrix.a); |
| 83 | + const axisStyle = new Map<string, any>([ |
| 84 | + ["lineWidth", 3], |
| 85 | + ["strokeStyle", "hsl(12, 45%, 80%)"] |
| 86 | + ]) |
| 87 | + axis.update(axisStyle, viewPoint, scaling, translation); |
| 88 | + |
| 89 | + expect(axis.minValue, "minValue").to.be.closeTo(-3.2, 0.1); |
| 90 | + expect(axis.maxValue, "maxValue").to.be.closeTo(1.2, 0.1); |
| 91 | + expect(axis.lineWidth, "lineWidth").to.be.equal(3); |
| 92 | + expect(axis.strokeStyle, "strokeStyle").to.equal("hsl(12, 45%, 80%)") |
| 93 | + }); |
| 94 | + |
| 95 | + it('should reset scale and translation', function() { |
| 96 | + const axis = new Axis(vector, boundingBox, origin, end, name, initScale, nTicks); |
| 97 | + axis.updateScale(new Vertex(20, 20), new Vertex(2, 0.5), new Vertex()); |
| 98 | + axis.updateScale(new Vertex(), new Vertex(1, 1), new Vertex(25, 25)); |
| 99 | + axis.resetScale(); |
| 100 | + |
| 101 | + expect(axis.minValue, "minValue").to.be.equal(0.8); |
| 102 | + expect(axis.maxValue, "maxValue").to.be.equal(5.2); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should transform a string array into values array for ticks to be drawn", function() { |
| 106 | + const stringVector = ["z", "e", "z", "y", "o", "u", "p", "i", "p", "p"]; |
| 107 | + const stringAxis = new Axis(stringVector, boundingBox, origin, end, name, initScale, nTicks); |
| 108 | + const numberVector = [1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 5]; |
| 109 | + const numberAxis = new Axis(numberVector, boundingBox, origin, end, name, initScale, nTicks); |
| 110 | + const numericStringVector = stringAxis.stringsToValues(stringVector); |
| 111 | + const numericNumberVector = numberAxis.stringsToValues(numberVector); |
| 112 | + |
| 113 | + numericStringVector.forEach((value, index) => expect(stringAxis.labels[value], `string value ${index}`).to.equal(stringVector[index])); |
| 114 | + numericNumberVector.forEach((value, index) => expect(value, `number value ${index}`).to.equal(numberVector[index])); |
| 115 | + }) |
| 116 | +}); |
| 117 | + |
| 118 | +describe("RubberBand", function() { |
| 119 | + const canvas = document.createElement('canvas'); |
| 120 | + const context = canvas.getContext('2d'); |
| 121 | + const axis = new Axis(vector, boundingBox, origin, end, name, initScale, nTicks); |
| 122 | + const mouseClick = new Vertex(0, 50); |
| 123 | + axis.mouseMove(context, mouseClick); |
| 124 | + axis.mouseDown(mouseClick); |
| 125 | + axis.mouseMove(context, new Vertex(0, 75)); |
| 126 | + axis.mouseUp(false); |
| 127 | + |
| 128 | + it("should draw rubberBand", function() { |
| 129 | + expect(axis.rubberBand.minValue, "rubberBand minValue").to.be.closeTo(3, 0.001); |
| 130 | + expect(axis.rubberBand.maxValue, "rubberBand maxValue").to.be.closeTo(4.1, 0.001); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should translate rubberBand", function () { |
| 134 | + const mouseClickInRubberBand = new Vertex(0, 60); |
| 135 | + const previousMinValue = axis.rubberBand.minValue; |
| 136 | + const previousMaxValue = axis.rubberBand.maxValue; |
| 137 | + axis.mouseMove(context, mouseClickInRubberBand); |
| 138 | + axis.mouseDown(mouseClickInRubberBand); |
| 139 | + axis.mouseMove(context, new Vertex(0, 90)); |
| 140 | + axis.mouseUp(false); |
| 141 | + expect(axis.rubberBand.minValue, "translated rubberBand minValue").to.not.be.equal(previousMinValue); |
| 142 | + expect(axis.rubberBand.maxValue, "translated rubberBand maxValue").to.not.be.equal(previousMaxValue); |
| 143 | + expect(axis.rubberBand.length, "translated rubberBand maxValue").to.be.closeTo(Math.abs(previousMaxValue - previousMinValue), 0.00001); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should reset rubberBand", function () { |
| 147 | + axis.reset(); |
| 148 | + expect(axis.rubberBand.minValue, "reset rubberBand minValue").to.equal(0); |
| 149 | + expect(axis.rubberBand.maxValue, "reset rubberBand maxValue").to.equal(0); |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +describe('ParallelAxis', function() { |
| 154 | + |
| 155 | + const vector = [1, 2, 3, 4, 5]; |
| 156 | + const boundingBox = new Rect(new Vertex(-50, 0), new Vertex(50, 150)); |
| 157 | + const origin = new Vertex(0, 0); |
| 158 | + const end = new Vertex(0, 100); |
| 159 | + const name = "parallel axis"; |
| 160 | + const initScale = new Vertex(1, 1); |
| 161 | + const nTicks = 5; |
| 162 | + |
| 163 | + it('should draw a vertical axis on the canvas', function() { |
| 164 | + const canvas = document.createElement('canvas'); |
| 165 | + const context = canvas.getContext('2d'); |
| 166 | + const parallelAxis = new ParallelAxis(vector, boundingBox, origin, end, name, initScale, nTicks); |
| 167 | + |
| 168 | + parallelAxis.computeTitle(0, 1); |
| 169 | + |
| 170 | + expect(parallelAxis.titleSettings.baseline, "default baseline").to.be.null; |
| 171 | + expect(parallelAxis.titleSettings.orientation, "default orientation").to.be.null; |
| 172 | + |
| 173 | + parallelAxis.draw(context); |
| 174 | + |
| 175 | + expect(parallelAxis.titleSettings.baseline, "updated baseline").to.be.equal("top"); |
| 176 | + expect(parallelAxis.titleSettings.orientation, "updated orientation").to.be.equal(0); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should draw a horizontal axis on the canvas', function() { |
| 180 | + const canvas = document.createElement('canvas'); |
| 181 | + const context = canvas.getContext('2d'); |
| 182 | + const parallelAxis = new ParallelAxis(vector, boundingBox, new Vertex(0, 0), new Vertex(100, 0), name, initScale, nTicks); |
| 183 | + |
| 184 | + parallelAxis.computeTitle(0, 1); |
| 185 | + |
| 186 | + expect(parallelAxis.titleSettings.baseline, "default baseline").to.be.null; |
| 187 | + |
| 188 | + parallelAxis.draw(context); |
| 189 | + |
| 190 | + expect(parallelAxis.titleSettings.baseline, "updated baseline").to.be.equal("bottom"); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should move when title is moved', function() { |
| 194 | + const canvas = document.createElement('canvas'); |
| 195 | + const context = canvas.getContext('2d'); |
| 196 | + const parallelAxis = new ParallelAxis(vector, boundingBox, new Vertex(0, 0), new Vertex(100, 0), name, initScale, nTicks); |
| 197 | + const newLocation = new Vertex(25, 25); |
| 198 | + parallelAxis.computeTitle(0, 1); |
| 199 | + parallelAxis.draw(context); |
| 200 | + parallelAxis.mouseMove(context, parallelAxis.title.boundingBox.center); |
| 201 | + parallelAxis.mouseDown(parallelAxis.title.boundingBox.center); |
| 202 | + parallelAxis.mouseMove(context, newLocation); |
| 203 | + |
| 204 | + expect(parallelAxis.origin.x, "origin x").to.be.closeTo(18.15, 0.01); |
| 205 | + expect(parallelAxis.origin.y, "origin y").to.be.closeTo(55, 0.01); |
| 206 | + |
| 207 | + parallelAxis.mouseUp(false); |
| 208 | + |
| 209 | + expect(parallelAxis.isClicked, "isClicked").to.be.false; |
| 210 | + }); |
| 211 | + |
| 212 | + it('should flip when title is clicked', function() { |
| 213 | + const canvas = document.createElement('canvas'); |
| 214 | + const context = canvas.getContext('2d'); |
| 215 | + const parallelAxis = new ParallelAxis(vector, boundingBox, new Vertex(0, 0), new Vertex(100, 0), name, initScale, nTicks); |
| 216 | + let emittedAxis; |
| 217 | + parallelAxis.emitter.on("axisStateChange", (updatedAxis) => emittedAxis = updatedAxis); |
| 218 | + parallelAxis.computeTitle(0, 1); |
| 219 | + parallelAxis.draw(context); |
| 220 | + parallelAxis.mouseMove(context, parallelAxis.title.boundingBox.center); |
| 221 | + parallelAxis.mouseDown(parallelAxis.title.boundingBox.center); |
| 222 | + parallelAxis.mouseUp(false); |
| 223 | + |
| 224 | + expect(parallelAxis.isInverted, "isInverted").to.be.true; |
| 225 | + expect(emittedAxis).to.deep.equal(parallelAxis); |
| 226 | + }); |
| 227 | + |
| 228 | +}); |
0 commit comments