|
| 1 | +var Plotly = require('@lib/index'); |
| 2 | + |
| 3 | +var createGraphDiv = require('../assets/create_graph_div'); |
| 4 | +var destroyGraphDiv = require('../assets/destroy_graph_div'); |
| 5 | +var customMatchers = require('../assets/custom_matchers'); |
| 6 | + |
| 7 | +describe('aggregate', function() { |
| 8 | + var gd; |
| 9 | + |
| 10 | + beforeAll(function() { jasmine.addMatchers(customMatchers);}); |
| 11 | + |
| 12 | + beforeEach(function() { gd = createGraphDiv(); }); |
| 13 | + |
| 14 | + afterEach(destroyGraphDiv); |
| 15 | + |
| 16 | + it('handles all funcs for numeric data', function() { |
| 17 | + // throw in some non-numbers, they should get discarded except first/last |
| 18 | + Plotly.newPlot(gd, [{ |
| 19 | + x: [1, 2, 3, 4, 'fail'], |
| 20 | + y: [1.1, 2.2, 3.3, 'nope', 5.5], |
| 21 | + marker: { |
| 22 | + size: ['2001-01-01', 0.2, 0.1, 0.4, 0.5], |
| 23 | + color: [2, 4, '', 10, 8], |
| 24 | + opacity: [0.6, 'boo', 0.2, 0.8, 1.0], |
| 25 | + line: { |
| 26 | + color: [2.2, 3.3, 4.4, 5.5, 'the end'] |
| 27 | + } |
| 28 | + }, |
| 29 | + transforms: [{ |
| 30 | + type: 'aggregate', |
| 31 | + groups: ['a', 'b', 'a', 'a', 'a'], |
| 32 | + aggregations: [ |
| 33 | + // missing array - the entry is ignored |
| 34 | + {target: '', func: 'avg'}, |
| 35 | + // disabled explicitly |
| 36 | + {target: 'x', func: 'avg', enabled: false}, |
| 37 | + {target: 'x', func: 'sum'}, |
| 38 | + // non-numerics will not count toward numerator or denominator for avg |
| 39 | + {target: 'y', func: 'avg'}, |
| 40 | + {target: 'marker.size', func: 'min'}, |
| 41 | + {target: 'marker.color', func: 'max'}, |
| 42 | + // marker.opacity doesn't have an entry, but it will default to first |
| 43 | + // as if it were {target: 'marker.opacity', func: 'first'}, |
| 44 | + {target: 'marker.line.color', func: 'last'}, |
| 45 | + // not present in data, but that's OK for count |
| 46 | + {target: 'marker.line.width', func: 'count'}, |
| 47 | + // duplicate entry - discarded |
| 48 | + {target: 'x', func: 'min'} |
| 49 | + ] |
| 50 | + }] |
| 51 | + }], { |
| 52 | + // log axis doesn't change how sum (or avg but not tested) works |
| 53 | + xaxis: {type: 'log'} |
| 54 | + }); |
| 55 | + |
| 56 | + var traceOut = gd._fullData[0]; |
| 57 | + |
| 58 | + expect(traceOut.x).toEqual([8, 2]); |
| 59 | + expect(traceOut.y).toBeCloseToArray([3.3, 2.2], 5); |
| 60 | + expect(traceOut.marker.size).toEqual([0.1, 0.2]); |
| 61 | + expect(traceOut.marker.color).toEqual([10, 4]); |
| 62 | + expect(traceOut.marker.opacity).toEqual([0.6, 'boo']); |
| 63 | + expect(traceOut.marker.line.color).toEqual(['the end', 3.3]); |
| 64 | + expect(traceOut.marker.line.width).toEqual([4, 1]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('handles all funcs except sum for date data', function() { |
| 68 | + // weird cases handled in another test |
| 69 | + Plotly.newPlot(gd, [{ |
| 70 | + x: ['2001-01-01', '', '2001-01-03', '2001-01-05', '2001-01-07'], |
| 71 | + y: ['1995-01-15', '2005-03-15', '1990-12-23', '2001-01-01', 'not a date'], |
| 72 | + text: ['2001-01-01 12:34', '2001-01-01 12:35', '2001-01-01 12:36', '2001-01-01 12:37', ''], |
| 73 | + hovertext: ['a', '2001-01-02', '2001-01-03', '2001-01-04', '2001-01-05'], |
| 74 | + customdata: ['2001-01', 'b', '2001-03', '2001-04', '2001-05'], |
| 75 | + transforms: [{ |
| 76 | + type: 'aggregate', |
| 77 | + // groups can be any type, but until we implement binning they |
| 78 | + // will always compare as strings = so 1 === '1' === 1.0 !== '1.0' |
| 79 | + groups: [1, 2, '1', 1.0, 1], |
| 80 | + aggregations: [ |
| 81 | + {target: 'x', func: 'avg'}, |
| 82 | + {target: 'y', func: 'min'}, |
| 83 | + {target: 'text', func: 'max'}, |
| 84 | + // hovertext doesn't have a func, default to first |
| 85 | + {target: 'hovertext'}, |
| 86 | + {target: 'customdata', func: 'last'}, |
| 87 | + // not present in data, but that's OK for count |
| 88 | + {target: 'marker.line.width', func: 'count'}, |
| 89 | + // duplicate entry - discarded |
| 90 | + {target: 'x', func: 'min'} |
| 91 | + ] |
| 92 | + }] |
| 93 | + }]); |
| 94 | + |
| 95 | + var traceOut = gd._fullData[0]; |
| 96 | + |
| 97 | + expect(traceOut.x).toEqual(['2001-01-04', undefined]); |
| 98 | + expect(traceOut.y).toEqual(['1990-12-23', '2005-03-15']); |
| 99 | + expect(traceOut.text).toEqual(['2001-01-01 12:37', '2001-01-01 12:35']); |
| 100 | + expect(traceOut.hovertext).toEqual(['a', '2001-01-02']); |
| 101 | + expect(traceOut.customdata).toEqual(['2001-05', 'b']); |
| 102 | + expect(traceOut.marker.line.width).toEqual([4, 1]); |
| 103 | + }); |
| 104 | + |
| 105 | + it('handles all funcs except sum and avg for category data', function() { |
| 106 | + // weird cases handled in another test |
| 107 | + Plotly.newPlot(gd, [{ |
| 108 | + x: ['a', 'b', 'c', 'aa', 'd'], |
| 109 | + y: ['q', 'w', 'e', 'r', 't'], |
| 110 | + text: ['b', 'b', 'a', 'b', 'a'], |
| 111 | + hovertext: ['c', 'b', 'a', 'b', 'a'], |
| 112 | + transforms: [{ |
| 113 | + type: 'aggregate', |
| 114 | + groups: [1, 2, 1, 1, 1], |
| 115 | + aggregations: [ |
| 116 | + {target: 'x', func: 'min'}, |
| 117 | + {target: 'y', func: 'max'}, |
| 118 | + {target: 'text', func: 'last'}, |
| 119 | + // hovertext doesn't have an entry, but it will default to first |
| 120 | + // not present in data, but that's OK for count |
| 121 | + {target: 'marker.line.width', func: 'count'}, |
| 122 | + // duplicate entry - discarded |
| 123 | + {target: 'x', func: 'max'} |
| 124 | + ] |
| 125 | + }] |
| 126 | + }], { |
| 127 | + xaxis: {categoryarray: ['aaa', 'aa', 'a', 'b', 'c']} |
| 128 | + }); |
| 129 | + |
| 130 | + var traceOut = gd._fullData[0]; |
| 131 | + |
| 132 | + // explicit order (only possible for axis data) |
| 133 | + expect(traceOut.x).toEqual(['aa', 'b']); |
| 134 | + // implied order from data |
| 135 | + expect(traceOut.y).toEqual(['t', 'w']); |
| 136 | + expect(traceOut.text).toEqual(['a', 'b']); |
| 137 | + expect(traceOut.hovertext).toEqual(['c', 'b']); |
| 138 | + expect(traceOut.marker.line.width).toEqual([4, 1]); |
| 139 | + }); |
| 140 | + |
| 141 | + it('allows date and category sums, and category avg, with weird output', function() { |
| 142 | + // this test is more of an FYI than anything else - it doesn't break but |
| 143 | + // these results are usually meaningless. |
| 144 | + |
| 145 | + Plotly.newPlot(gd, [{ |
| 146 | + x: ['2001-01-01', '2001-01-02', '2001-01-03', '2001-01-04'], |
| 147 | + y: ['a', 'b', 'b', 'c'], |
| 148 | + text: ['a', 'b', 'a', 'c'], |
| 149 | + transforms: [{ |
| 150 | + type: 'aggregate', |
| 151 | + groups: [1, 1, 2, 2], |
| 152 | + aggregations: [ |
| 153 | + {target: 'x', func: 'sum'}, |
| 154 | + {target: 'y', func: 'sum'}, |
| 155 | + {target: 'text', func: 'avg'} |
| 156 | + ] |
| 157 | + }] |
| 158 | + }]); |
| 159 | + |
| 160 | + var traceOut = gd._fullData[0]; |
| 161 | + |
| 162 | + // date sums: 1970-01-01 is "zero", there are shifts due to # of leap years |
| 163 | + // without that shift these would be 2032-01-02 and 2032-01-06 |
| 164 | + expect(traceOut.x).toEqual(['2032-01-03', '2032-01-07']); |
| 165 | + // category sums: can go off the end of the category array -> gives undefined |
| 166 | + expect(traceOut.y).toEqual(['b', undefined]); |
| 167 | + // category average: can result in fractional categories -> rounds (0.5 rounds to 1) |
| 168 | + expect(traceOut.text).toEqual(['b', 'b']); |
| 169 | + }); |
| 170 | + |
| 171 | + it('can aggregate on an existing data array', function() { |
| 172 | + Plotly.newPlot(gd, [{ |
| 173 | + x: [1, 2, 3, 4, 5], |
| 174 | + y: [2, 4, 6, 8, 10], |
| 175 | + marker: {size: [10, 10, 20, 20, 10]}, |
| 176 | + transforms: [{ |
| 177 | + type: 'aggregate', |
| 178 | + groups: 'marker.size', |
| 179 | + aggregations: [ |
| 180 | + {target: 'x', func: 'sum'}, |
| 181 | + {target: 'y', func: 'avg'} |
| 182 | + ] |
| 183 | + }] |
| 184 | + }]); |
| 185 | + |
| 186 | + var traceOut = gd._fullData[0]; |
| 187 | + |
| 188 | + expect(traceOut.x).toEqual([8, 7]); |
| 189 | + expect(traceOut.y).toBeCloseToArray([16 / 3, 7], 5); |
| 190 | + expect(traceOut.marker.size).toEqual([10, 20]); |
| 191 | + }); |
| 192 | + |
| 193 | + it('handles median, mode, rms, & stddev for numeric data', function() { |
| 194 | + // again, nothing is going to barf with non-numeric data, but sometimes it |
| 195 | + // won't make much sense. |
| 196 | + |
| 197 | + Plotly.newPlot(gd, [{ |
| 198 | + x: [1, 1, 2, 2, 1], |
| 199 | + y: [1, 2, 3, 4, 5], |
| 200 | + marker: { |
| 201 | + size: [1, 2, 3, 4, 5], |
| 202 | + line: {width: [1, 1, 2, 2, 1]}, |
| 203 | + color: [1, 1, 2, 2, 1] |
| 204 | + }, |
| 205 | + transforms: [{ |
| 206 | + type: 'aggregate', |
| 207 | + groups: [1, 2, 1, 1, 1], |
| 208 | + aggregations: [ |
| 209 | + {target: 'x', func: 'mode'}, |
| 210 | + {target: 'y', func: 'median'}, |
| 211 | + {target: 'marker.size', func: 'rms'}, |
| 212 | + {target: 'marker.line.width', func: 'stddev', funcmode: 'population'}, |
| 213 | + {target: 'marker.color', func: 'stddev'} |
| 214 | + ] |
| 215 | + }] |
| 216 | + }]); |
| 217 | + |
| 218 | + var traceOut = gd._fullData[0]; |
| 219 | + |
| 220 | + // 1 and 2 both have count of 2 in the first group, |
| 221 | + // but 2 gets to that count first |
| 222 | + expect(traceOut.x).toEqual([2, 1]); |
| 223 | + expect(traceOut.y).toBeCloseToArray([3.5, 2], 5); |
| 224 | + expect(traceOut.marker.size).toBeCloseToArray([Math.sqrt(51 / 4), 2], 5); |
| 225 | + expect(traceOut.marker.line.width).toBeCloseToArray([0.5, 0], 5); |
| 226 | + expect(traceOut.marker.color).toBeCloseToArray([Math.sqrt(1 / 3), 0], 5); |
| 227 | + }); |
| 228 | +}); |
0 commit comments