OLD | NEW |
(Empty) | |
| 1 library charted.test.linechartrenderer; |
| 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', 950, 500, 200], |
| 24 const['Japan',150, 990, 200], |
| 25 const['Taiwan', 350, 127, 1337], |
| 26 const['France', 250, 290, 600], |
| 27 const['Germany', 1100, 90, 1000], |
| 28 const['England', 250, 100, 300], |
| 29 const['Brazil', 150, 270, 600], |
| 30 const['Argentina', 550, 370, 200], |
| 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 lines as there is measures in the series. |
| 45 var seriesGroup = host.querySelector('.series-group'); |
| 46 var lines = seriesGroup.children.where((e) => e.classes.contains('line')); |
| 47 expect(lines.length, area.config.series.elementAt(0).measures.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], 1.0, |
| 60 area.theme.dimensionAxisTheme.axisOuterPadding); |
| 61 |
| 62 // Create a new Linear scale for the y attribute. |
| 63 var yScale = new LinearScale(); |
| 64 |
| 65 // 1337 is max value in ROWS, but it would get niced to 1400. |
| 66 yScale.domain = [0, 1400]; |
| 67 yScale.range = [area.layout.renderArea.height, 0]; |
| 68 |
| 69 var xValues = xScale.domain; |
| 70 var xAccessor = (d, i) => xScale.apply(xValues[i]) + xScale.rangeBand / 2; |
| 71 var yAccessor = (d, i) => yScale.apply(d); |
| 72 var lineData = area.config.series.elementAt(0).measures.map((column) { |
| 73 return area.data.rows.map((values) => values[column]).toList(); |
| 74 }).toList(); |
| 75 |
| 76 // Tests the path of the svg lines created against the data. |
| 77 // The behavior for the circles on hover may change soon. Currently too |
| 78 // many circle elements are being added and is a performance issue for large |
| 79 // data set. Add test for that after the behavior change. |
| 80 var currentPoint; |
| 81 for (var i = 0; i < lines.length; i++) { |
| 82 var data = lineData[i]; |
| 83 var path = lines.elementAt(i).attributes['d']; |
| 84 for (var j = 0; j < data.length; j++) { |
| 85 currentPoint = (j == 0 ? 'M' : 'L') + |
| 86 xAccessor(data[j], j).toString() + ',' + |
| 87 yAccessor(data[j], j).toString(); |
| 88 expect(path.startsWith(currentPoint), isTrue); |
| 89 path = path.substring(currentPoint.length); |
| 90 } |
| 91 } |
| 92 } |
| 93 |
| 94 test('line chart renderer test', () { |
| 95 var chartAreaHost = new DivElement()..classes.add('chart-host'), |
| 96 series = new ChartSeries('line', [1, 2, 3], |
| 97 new LineChartRenderer()); |
| 98 host.children.add(chartAreaHost); |
| 99 config = new ChartConfig([series], [0]); |
| 100 config.minimumSize = new Rect.size(CHART_WIDTH, CHART_HEIGHT); |
| 101 area = new ChartArea(chartAreaHost, data, config); |
| 102 area.draw(); |
| 103 |
| 104 // The series group is not painted until the axis is painted. However |
| 105 // The ChartArea.draw current doesn't return a Future upon paint completion |
| 106 // Also there is currently a default transition that we can not remove (will |
| 107 // change in the near future). Set enough delay to ensure all the elements |
| 108 // in the chart has finished their initial animation. |
| 109 new Timer(new Duration(milliseconds:1000), expectAsync(checkRenderResult)); |
| 110 }); |
| 111 } |
OLD | NEW |