OLD | NEW |
(Empty) | |
| 1 library charted.test.stackedbarchartrenderer; |
| 2 |
| 3 import 'dart:async'; |
| 4 import 'dart:html'; |
| 5 |
| 6 import 'package:charted/charts/charts.dart'; |
| 7 import 'package:charted/core/core.dart'; |
| 8 import 'package:charted/scale/scale.dart'; |
| 9 import 'package:unittest/unittest.dart'; |
| 10 |
| 11 // TODO(midoringo): Add more complex tests on 0 height bars. |
| 12 main() { |
| 13 const CHART_WIDTH = 1000; |
| 14 const CHART_HEIGHT = 400; |
| 15 |
| 16 List COLUMNS = [ |
| 17 new ChartColumnSpec(label:'Country', type:ChartColumnSpec.TYPE_STRING), |
| 18 new ChartColumnSpec(label:'Stats1'), |
| 19 new ChartColumnSpec(label:'Stats2'), |
| 20 new ChartColumnSpec(label:'Stats3') |
| 21 ]; |
| 22 |
| 23 const List ROWS = const [ |
| 24 const['USA', 950, 500, 200], |
| 25 const['Japan',150, 990, 200], |
| 26 const['Taiwan', 350, 127, 1337], |
| 27 const['France', 250, 290, 600], |
| 28 const['Germany', 1100, 90, 1000], |
| 29 const['England', 250, 100, 300], |
| 30 const['Brazil', 150, 270, 600], |
| 31 const['Argentina', 550, 370, 200], |
| 32 ]; |
| 33 |
| 34 ChartData data = new ChartData(COLUMNS, ROWS); |
| 35 ChartConfig config; |
| 36 ChartArea area; |
| 37 Element host = new DivElement()..classes.add('host'); |
| 38 |
| 39 void checkRenderResult() { |
| 40 // Check dimension of the chart element. |
| 41 var chartElement = host.querySelector('.charted-chart'); |
| 42 expect(chartElement.attributes['width'], CHART_WIDTH.toString()); |
| 43 expect(chartElement.attributes['height'], CHART_HEIGHT.toString()); |
| 44 |
| 45 // Should have as much bar groups as there are rows in data. |
| 46 var seriesGroup = host.querySelector('.series-group'); |
| 47 expect(seriesGroup.children.length, area.data.rows.length); |
| 48 |
| 49 // Check offset for the actual render area. |
| 50 var xOffset = area.layout.renderArea.x; |
| 51 var yOffset = area.layout.renderArea.y; |
| 52 expect(seriesGroup.attributes['transform'], |
| 53 'translate(${xOffset},${yOffset})'); |
| 54 |
| 55 // Test offsets of bar groups by dimension range band and the band |
| 56 // padding. |
| 57 var xScale = new OrdinalScale(); |
| 58 xScale.domain = ROWS.map((e) => e[0]).toList(); |
| 59 xScale.rangeRoundBands([0, area.layout.renderArea.width], |
| 60 area.theme.dimensionAxisTheme.axisBandInnerPadding, |
| 61 area.theme.dimensionAxisTheme.axisBandOuterPadding); |
| 62 for (var i = 0; i < seriesGroup.children.length; i++) { |
| 63 expect(seriesGroup.children[i].attributes['transform'], |
| 64 'translate(${xScale.range[i]}, 0)'); |
| 65 } |
| 66 |
| 67 // The measures in the ChartSeries. |
| 68 var measures = area.config.series.elementAt(0).measures; |
| 69 |
| 70 // Create a new Linear scale for the y attribute. |
| 71 var yScale = new LinearScale(); |
| 72 // 2190 is max sum of one row (stack) in ROWS, but it would get niced to |
| 73 // 2500. |
| 74 yScale.domain = [0, 2500]; |
| 75 yScale.range = [area.layout.renderArea.height, 0]; |
| 76 |
| 77 // Tests the width and height and offsets of each bar within the groups. |
| 78 for (var i = 0; i < seriesGroup.children.length; i++) { |
| 79 var group = seriesGroup.children[i]; |
| 80 |
| 81 // Check the number of bars in a group is the same as the measures in the |
| 82 // seires. |
| 83 expect(group.children.length, measures.length); |
| 84 |
| 85 var bars = group.children; |
| 86 var y = area.layout.renderArea.height; |
| 87 for (var m = 0; m < measures.length; m++) { |
| 88 // The bars is drawn reversed so we can have first column of input on |
| 89 // top of the stack. |
| 90 var reversedIndex = measures.length - m - 1; |
| 91 expect(bars[m].attributes['width'], (xScale.rangeBand - |
| 92 area.theme.defaultStrokeWidth).toString()); |
| 93 var height = (area.layout.renderArea.height - |
| 94 yScale.apply(ROWS[i][measures.elementAt(reversedIndex)])).round(); |
| 95 var separatorOffset = area.theme.defaultSeparatorWidth + |
| 96 area.theme.defaultStrokeWidth; |
| 97 |
| 98 y -= height; |
| 99 expect(bars[m].attributes['y'], y.toString()); |
| 100 |
| 101 // The first bar has -1 height so it doesn't draw on top of x-axis. |
| 102 if (m == 0) { |
| 103 height -= 1; |
| 104 } else { |
| 105 // The rest of the bar has offset on the border + separator between |
| 106 // the bars. |
| 107 height -= separatorOffset; |
| 108 } |
| 109 expect(bars[m].attributes['height'], height.toString()); |
| 110 } |
| 111 } |
| 112 } |
| 113 |
| 114 test('stacked bar chart renderer test', () { |
| 115 var chartAreaHost = new DivElement()..classes.add('chart-host'), |
| 116 series = new ChartSeries('stacked-bar', [1, 2, 3], |
| 117 new StackedBarChartRenderer()); |
| 118 host.children.add(chartAreaHost); |
| 119 config = new ChartConfig([series], [0]); |
| 120 config.minimumSize = new Rect.size(CHART_WIDTH, CHART_HEIGHT); |
| 121 area = new ChartArea(chartAreaHost, data, config); |
| 122 area.draw(); |
| 123 |
| 124 // The series group is not painted until the axis is painted. However |
| 125 // The ChartArea.draw current doesn't return a Future upon paint completion |
| 126 // Also there is currently a default transition that we can not remove (will |
| 127 // change in the near future). Set enough delay to ensure all the elements |
| 128 // in the chart has finished their initial animation. |
| 129 new Timer(new Duration(milliseconds:1000), expectAsync(checkRenderResult)); |
| 130 }); |
| 131 } |
OLD | NEW |