OLD | NEW |
(Empty) | |
| 1 library charted.test.piechartrenderer; |
| 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/layout/layout.dart'; |
| 9 import 'package:charted/svg/svg.dart'; |
| 10 import 'package:unittest/unittest.dart'; |
| 11 |
| 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:'statsList1'), |
| 19 new ChartColumnSpec(label:'statsList2'), |
| 20 new ChartColumnSpec(label:'statsList3') |
| 21 ]; |
| 22 |
| 23 const List ROWS = const [ |
| 24 const['USA', 950, 500, 200], |
| 25 const['Japan',150, 990, 200], |
| 26 ]; |
| 27 |
| 28 ChartData data = new ChartData(COLUMNS, ROWS); |
| 29 ChartConfig config; |
| 30 ChartArea area; |
| 31 Element host = new DivElement()..classes.add('host'); |
| 32 |
| 33 // Helper for constructing the SvgArcData of each slice of the pie. |
| 34 List<SvgArcData> produceArcData(List rows) { |
| 35 var arcDataList = []; |
| 36 var layout = new PieLayout(); |
| 37 var innerRadius = 0; |
| 38 // height is shorter than width. |
| 39 var pieRadius = area.layout.renderArea.height / 2; |
| 40 var outerRadius = pieRadius - 10; |
| 41 var sliceRadius = (pieRadius - 10 - innerRadius) / rows.length; |
| 42 |
| 43 for (var i = 0; i < rows.length; i++) { |
| 44 var arcData = layout.layout(rows[i]); |
| 45 arcData.forEach((e) { |
| 46 e.innerRadius = outerRadius - sliceRadius * (i + 1) + 0.5; |
| 47 e.outerRadius = outerRadius - sliceRadius * i; |
| 48 }); |
| 49 |
| 50 arcDataList.addAll(arcData); |
| 51 } |
| 52 |
| 53 return arcDataList; |
| 54 } |
| 55 |
| 56 // Extracts the data used to compute each pie in the input. |
| 57 List extractPieData() { |
| 58 return new List()..addAll(area.data.rows.map((e) { |
| 59 var row = []; |
| 60 for (var measure in config.series.elementAt(0).measures) { |
| 61 row.add(e[measure]); |
| 62 } |
| 63 return row; |
| 64 })); |
| 65 } |
| 66 |
| 67 void checkRenderResult() { |
| 68 // Check dimension of the chart element. |
| 69 var chartElement = host.querySelector('.charted-chart'); |
| 70 expect(chartElement.attributes['width'], CHART_WIDTH.toString()); |
| 71 expect(chartElement.attributes['height'], CHART_HEIGHT.toString()); |
| 72 |
| 73 // Should have as much slicesList and statsList text as there are measures. |
| 74 var rowGroups = host.querySelectorAll('.row-group'); |
| 75 var slicesList = []; |
| 76 var statsList = []; |
| 77 for (var rowGroup in rowGroups) { |
| 78 slicesList.add(rowGroup.children.where((e) => |
| 79 e.classes.contains('pie-path'))); |
| 80 statsList.add(rowGroup.children.where((e) => |
| 81 e.classes.contains('statistic'))); |
| 82 expect(slicesList.last.length, |
| 83 area.config.series.elementAt(0).measures.length); |
| 84 expect(statsList.last.length, |
| 85 area.config.series.elementAt(0).measures.length); |
| 86 } |
| 87 |
| 88 // Check offset for the actual render area. |
| 89 var xOffset = area.layout.renderArea.width / 2; |
| 90 var yOffset = area.layout.renderArea.height / 2; |
| 91 expect(rowGroups.elementAt(0).attributes['transform'], |
| 92 'translate(${xOffset}, ${yOffset})'); |
| 93 |
| 94 // Tests all the text elements that show the percentage stats of each slice. |
| 95 var pieRows = extractPieData(); |
| 96 for (var i = 0; i < statsList.length; i++) { |
| 97 var pieData = pieRows.elementAt(i); |
| 98 var sum = pieData.reduce((a, b) => a + b); |
| 99 var textElements = statsList[i]; |
| 100 for (var j = 0; j < textElements.length; j++) { |
| 101 var stat = textElements.elementAt(j); |
| 102 num percentage = pieData.elementAt(j) / sum; |
| 103 percentage.round(); |
| 104 expect((percentage * 100).round().toString() + '%', stat.text); |
| 105 } |
| 106 } |
| 107 |
| 108 // Tests the path of each slices in each pie. Utilizing the PieLayout and |
| 109 // SvgArc which is individually tested. |
| 110 var arc = new SvgArc(); |
| 111 var arcDataList = produceArcData(pieRows); |
| 112 for (var i = 0; i < slicesList.length; i++) { |
| 113 var slices = slicesList[i]; // Slices of one pie. |
| 114 for (var j = 0; j < slices.length; j++) { |
| 115 expect(slices.elementAt(j).attributes['d'], |
| 116 arc.path(arcDataList[i * slices.length + j], 0, host)); |
| 117 } |
| 118 } |
| 119 } |
| 120 |
| 121 test('pie chart renderer test', () { |
| 122 var chartAreaHost = new DivElement()..classes.add('chart-host'), |
| 123 series = new ChartSeries('pie', [1, 2, 3], new PieChartRenderer()); |
| 124 host.children.add(chartAreaHost); |
| 125 config = new ChartConfig([series], [0]); |
| 126 config.minimumSize = new Rect.size(CHART_WIDTH, CHART_HEIGHT); |
| 127 area = new ChartArea(chartAreaHost, data, config, dimensionAxesCount: 0); |
| 128 area.draw(); |
| 129 |
| 130 // The series group is not painted until the axis is painted. However |
| 131 // The ChartArea.draw current doesn't return a Future upon paint completion |
| 132 // Also there is currently a default transition that we can not remove (will |
| 133 // change in the near future). Set enough delay to ensure all the elements |
| 134 // in the chart has finished their initial animation. |
| 135 new Timer(new Duration(milliseconds:1000), expectAsync(checkRenderResult)); |
| 136 }); |
| 137 } |
OLD | NEW |