OLD | NEW |
(Empty) | |
| 1 library charted.test.barchartrenderer; |
| 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 main() { |
| 12 const CHART_WIDTH = 1000; |
| 13 const CHART_HEIGHT = 400; |
| 14 |
| 15 List COLUMNS = [ |
| 16 new ChartColumnSpec(label:'Country', type:ChartColumnSpec.TYPE_STRING), |
| 17 new ChartColumnSpec(label:'Stats1'), |
| 18 new ChartColumnSpec(label:'Stats2'), |
| 19 new ChartColumnSpec(label:'Stats3') |
| 20 ]; |
| 21 |
| 22 const List ROWS = const [ |
| 23 const['USA', 9.50, 50, 2000], |
| 24 const['Japan',1.50, 99, 2000], |
| 25 const['Taiwan', 3.50, 127, 1337], |
| 26 const['France', 2.50, 29, 6000], |
| 27 const['Germany', 10.99, 999, 10000], |
| 28 const['England', 2.50, 10, 3000], |
| 29 const['Brazil', 1.50, 27, 6000], |
| 30 const['Argentina', 5.50, 37, 2000], |
| 31 ]; |
| 32 |
| 33 ChartData data = new ChartData(COLUMNS, ROWS); |
| 34 ChartConfig config; |
| 35 ChartArea area; |
| 36 Element host = new DivElement()..classes.add('host'); |
| 37 |
| 38 void checkRenderResult() { |
| 39 // Check dimension of the chart element. |
| 40 var chartElement = host.querySelector('.charted-chart'); |
| 41 expect(chartElement.attributes['width'], CHART_WIDTH.toString()); |
| 42 expect(chartElement.attributes['height'], CHART_HEIGHT.toString()); |
| 43 |
| 44 // Should have as much bar groups as there are rows in data. |
| 45 var seriesGroup = host.querySelector('.series-group'); |
| 46 expect(seriesGroup.children.length, area.data.rows.length); |
| 47 |
| 48 // Check offset for the actual render area. |
| 49 var xOffset = area.layout.renderArea.x; |
| 50 var yOffset = area.layout.renderArea.y; |
| 51 expect(seriesGroup.attributes['transform'], |
| 52 'translate(${xOffset},${yOffset})'); |
| 53 |
| 54 // Test offsets of bar groups by dimension range band and the band |
| 55 // padding. |
| 56 var xScale = new OrdinalScale(); |
| 57 xScale.domain = ROWS.map((e) => e[0]).toList(); |
| 58 xScale.rangeRoundBands([0, area.layout.renderArea.width], |
| 59 area.theme.dimensionAxisTheme.axisBandInnerPadding, |
| 60 area.theme.dimensionAxisTheme.axisBandOuterPadding); |
| 61 for (var i = 0; i < seriesGroup.children.length; i++) { |
| 62 expect(seriesGroup.children[i].attributes['transform'], |
| 63 'translate(${xScale.range[i]}, 0)'); |
| 64 } |
| 65 |
| 66 // The measures in the ChartSeries. |
| 67 var measures = area.config.series.elementAt(0).measures; |
| 68 var bar = new OrdinalScale() |
| 69 ..domain = measures |
| 70 ..rangeRoundBands([0, area.dimensionScales.first.rangeBand]); |
| 71 var barWidth = bar.rangeBand - area.theme.defaultSeparatorWidth - |
| 72 area.theme.defaultStrokeWidth; |
| 73 |
| 74 // Create a new Linear scale for the y attribute. |
| 75 var yScale = new LinearScale(); |
| 76 yScale.domain = [0, 10000]; // 10000 is max value of the input data. |
| 77 yScale.range = [area.layout.renderArea.height, 0]; |
| 78 |
| 79 // Tests the width and height and offsets of each bar within the groups. |
| 80 for (var i = 0; i < seriesGroup.children.length; i++) { |
| 81 var group = seriesGroup.children[i]; |
| 82 |
| 83 // Check the number of bars in a group is the same as the measures in the |
| 84 // seires. |
| 85 expect(group.children.length, measures.length); |
| 86 |
| 87 // Offsets of bars within the group and the width of the bars are the |
| 88 // same base on the theme and the number of measures and available |
| 89 // space in the render area. |
| 90 var bars = group.children; |
| 91 for (var m = 0; m < measures.length; m++) { |
| 92 expect(bars[m].attributes['x'], ((1 + m) * |
| 93 (area.theme.defaultSeparatorWidth + area.theme.defaultStrokeWidth) + |
| 94 (m * barWidth)).toDouble().toString()); |
| 95 expect(bars[m].attributes['width'], barWidth.toString()); |
| 96 expect(bars[m].attributes['y'], |
| 97 (yScale.apply(ROWS[i][measures.elementAt(m)])).round().toString()); |
| 98 } |
| 99 } |
| 100 } |
| 101 |
| 102 test('bar chart renderer test', () { |
| 103 var chartAreaHost = new DivElement()..classes.add('chart-host'), |
| 104 bar_series = new ChartSeries('bar', [2, 3], new BarChartRenderer()); |
| 105 host.children.add(chartAreaHost); |
| 106 config = new ChartConfig([bar_series], [0]); |
| 107 config.minimumSize = new Rect.size(CHART_WIDTH, CHART_HEIGHT); |
| 108 area = new ChartArea(chartAreaHost, data, config); |
| 109 area.draw(); |
| 110 |
| 111 // The series group is not painted until the axis is painted. However |
| 112 // The ChartArea.draw current doesn't return a Future upon paint completion |
| 113 // Also there is currently a default transition that we can not remove (will |
| 114 // change in the near future). Set enough delay to ensure all the elements |
| 115 // in the chart has finished their initial animation. |
| 116 new Timer(new Duration(milliseconds:1000), expectAsync(checkRenderResult)); |
| 117 }); |
| 118 } |
OLD | NEW |