OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 Google Inc. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style |
| 5 * license that can be found in the LICENSE file or at |
| 6 * https://developers.google.com/open-source/licenses/bsd |
| 7 */ |
| 8 |
| 9 part of charted.test.svg; |
| 10 |
| 11 testSvgAxis() { |
| 12 String markup = '<div class="charts-axis-container">' |
| 13 '<div class="charts-axis"></div>' |
| 14 '<div class="charts-axis-async1"></div>' |
| 15 '<div class="charts-axis-async2"></div>' |
| 16 '<div class="charts-axis-async3"></div>' |
| 17 '<div class="charts-axis-async4"></div>' |
| 18 '</div>'; |
| 19 |
| 20 Element root; |
| 21 SelectionScope scope; |
| 22 Selection axis, ticks, text; |
| 23 |
| 24 setup() { |
| 25 root = new Element.html(markup); |
| 26 document.documentElement.append(root); |
| 27 scope = new SelectionScope.selector('.charts-axis-container'); |
| 28 axis = scope.select('.charts-axis'); |
| 29 } |
| 30 |
| 31 teardown() { |
| 32 root.remove(); |
| 33 } |
| 34 |
| 35 group('SvgAxis.axis', () { |
| 36 setUp(setup); |
| 37 tearDown(teardown); |
| 38 SvgAxis svgAxis = new SvgAxis(); |
| 39 svgAxis.scale.domain = [1, 10]; |
| 40 |
| 41 test('generates an axis with default tickFormat and scale', () { |
| 42 svgAxis.axis(axis); |
| 43 ticks = axis.selectAll('.tick'); |
| 44 text = ticks.select('text'); |
| 45 text.each((d, i, Element e) { |
| 46 //Exactly 10 ticks steped 1 from 1 to 10 according to domain |
| 47 expect(e.text, equals((i + 1).toString())); |
| 48 }); |
| 49 }); |
| 50 |
| 51 test('generates an axis with manaully set tickFormat', () { |
| 52 svgAxis.tickFormat = (x) => x.toString() + 'px'; |
| 53 svgAxis.axis(axis); |
| 54 ticks = axis.selectAll('.tick'); |
| 55 text = ticks.select('text'); |
| 56 text.each((d, i, Element e) { |
| 57 //Exactly 10 ticks steped 1 from 1 to 10 according to domain |
| 58 expect(e.text, equals((i + 1).toString() + '.0px')); |
| 59 }); |
| 60 svgAxis.tickFormat = null; |
| 61 }); |
| 62 |
| 63 test('generates an axis with manaully set tickValues', () { |
| 64 svgAxis.tickValues = [2, 4, 6, 8]; |
| 65 svgAxis.axis(axis); |
| 66 ticks = axis.selectAll('.tick'); |
| 67 text = ticks.select('text'); |
| 68 text.each((d, i, Element e) { |
| 69 //Exactly 10 ticks steped 1 from 1 to 10 according to domain |
| 70 expect(e.text, equals((i * 2 + 2).toString())); |
| 71 }); |
| 72 }); |
| 73 |
| 74 test('transforms linear scale axis ticks to right positions', () { |
| 75 Selection axis = scope.select('.charts-axis-async1'); |
| 76 SvgAxis svgAxis = new SvgAxis(); |
| 77 svgAxis.scale.domain = [1, 10]; |
| 78 svgAxis.scale.range = [10, 100]; |
| 79 svgAxis.axis(axis); |
| 80 Selection ticks = axis.selectAll('.tick'); |
| 81 new Timer(new Duration(milliseconds:200), expectAsync(() { |
| 82 ticks.each((d, i, Element e) { |
| 83 int pos = e.attributes['transform'].indexOf(','); |
| 84 expect(num.parse(e.attributes['transform'].substring(10, pos)), |
| 85 closeTo((i + 1) * 10, EPSILON)); |
| 86 }); |
| 87 })); |
| 88 Selection axis2 = scope.select('.charts-axis-async2'); |
| 89 SvgAxis svgAxis2 = new SvgAxis(); |
| 90 svgAxis2.orientation = ORIENTATION_LEFT; |
| 91 svgAxis2.scale.domain = [1, 10]; |
| 92 svgAxis2.scale.range = [10, 100]; |
| 93 svgAxis2.axis(axis2); |
| 94 Selection ticks2 = axis2.selectAll('.tick'); |
| 95 new Timer(new Duration(milliseconds:200), expectAsync(() { |
| 96 ticks2.each((d, i, Element e) { |
| 97 int pos1 = e.attributes['transform'].indexOf(','); |
| 98 int pos2 = e.attributes['transform'].indexOf(')'); |
| 99 expect(num.parse(e.attributes['transform'].substring(pos1 + 1, pos2)), |
| 100 closeTo((i + 1) * 10, EPSILON)); |
| 101 }); |
| 102 })); |
| 103 }); |
| 104 |
| 105 test('generates an axis with ordinal scale defined tickValues ' |
| 106 'and transforms ordinal scale axis ticks to right positions', () { |
| 107 Selection axis = scope.select('.charts-axis-async3'); |
| 108 SvgAxis svgAxis = new SvgAxis(); |
| 109 svgAxis.orientation = ORIENTATION_TOP; |
| 110 svgAxis.scale = new OrdinalScale(); |
| 111 svgAxis.scale.domain = ['Jan', 'Feb', 'Mar']; |
| 112 svgAxis.scale.range = [0, 45, 90]; |
| 113 svgAxis.scale.rangeBand = 10; |
| 114 svgAxis.axis(axis); |
| 115 Selection ticks = axis.selectAll('.tick'); |
| 116 text = ticks.select('text'); |
| 117 text.each((d, i, Element e) { |
| 118 expect(e.text, equals(svgAxis.scale.domain[i])); |
| 119 }); |
| 120 new Timer(new Duration(milliseconds:200), expectAsync(() { |
| 121 ticks.each((d, i, Element e) { |
| 122 int pos = e.attributes['transform'].indexOf(','); |
| 123 expect(num.parse(e.attributes['transform'].substring(10, pos)), |
| 124 closeTo(i * 45 + 5, EPSILON)); |
| 125 }); |
| 126 })); |
| 127 Selection axis2 = scope.select('.charts-axis-async4'); |
| 128 SvgAxis svgAxis2 = new SvgAxis(); |
| 129 svgAxis2.orientation = ORIENTATION_RIGHT; |
| 130 svgAxis2.scale = new OrdinalScale(); |
| 131 svgAxis2.scale.domain = ['Jan', 'Feb', 'Mar']; |
| 132 svgAxis2.scale.range = [0, 45, 90]; |
| 133 svgAxis2.scale.rangeBand = 10; |
| 134 svgAxis2.axis(axis2); |
| 135 text = ticks.select('text'); |
| 136 text.each((d, i, Element e) { |
| 137 expect(e.text, equals(svgAxis2.scale.domain[i])); |
| 138 }); |
| 139 Selection ticks2 = axis2.selectAll('.tick'); |
| 140 new Timer(new Duration(milliseconds:200), expectAsync(() { |
| 141 ticks2.each((d, i, Element e) { |
| 142 int pos1 = e.attributes['transform'].indexOf(','); |
| 143 int pos2 = e.attributes['transform'].indexOf(')'); |
| 144 expect(num.parse(e.attributes['transform'].substring(pos1 + 1, pos2)), |
| 145 closeTo(i * 45 + 5, EPSILON)); |
| 146 }); |
| 147 })); |
| 148 }); |
| 149 }); |
| 150 } |
OLD | NEW |