OLD | NEW |
1 // | 1 // |
2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
3 // | 3 // |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file or at | 5 // license that can be found in the LICENSE file or at |
6 // https://developers.google.com/open-source/licenses/bsd | 6 // https://developers.google.com/open-source/licenses/bsd |
7 // | 7 // |
8 | 8 |
9 part of charted.charts; | 9 part of charted.charts; |
10 | 10 |
11 class StackedBarChartRenderer extends CartesianRendererBase { | 11 class StackedBarChartRenderer extends CartesianRendererBase { |
12 static const RADIUS = 2; | 12 static const RADIUS = 2; |
13 | 13 |
14 final Iterable<int> dimensionsUsingBand = const [0]; | 14 final Iterable<int> dimensionsUsingBand = const [0]; |
15 final bool alwaysAnimate; | 15 final bool alwaysAnimate; |
16 | 16 |
17 @override | 17 @override |
18 final String name = "stack-rdr"; | 18 final String name = "stack-rdr"; |
19 | 19 |
20 /// Used to capture the last measure with data in a data row. This is used | 20 /// Used to capture the last measure with data in a data row. This is used |
21 /// to decided whether to round the cornor of the bar or not. | 21 /// to decided whether to round the cornor of the bar or not. |
22 List<int> _lastMeasureWithData = []; | 22 List<int> _lastMeasureWithData = []; |
23 | 23 |
24 StackedBarChartRenderer({this.alwaysAnimate: false}); | 24 StackedBarChartRenderer({this.alwaysAnimate: false}); |
25 | 25 |
26 /// Returns false if the number of dimension axes on the area is 0. | 26 /// Returns false if the number of dimension axes on the area is 0. |
27 /// Otherwise, the first dimension scale is used to render the chart. | 27 /// Otherwise, the first dimension scale is used to render the chart. |
28 @override | 28 @override |
29 bool prepare(CartesianArea area, ChartSeries series) { | 29 bool prepare(ChartArea area, ChartSeries series) { |
| 30 if (area is! CartesianArea) { |
| 31 throw new ArgumentError.value(area, 'area', |
| 32 "ChartArea for StackedBarChartRenderer must be a CartesianArea"); |
| 33 } |
30 _ensureAreaAndSeries(area, series); | 34 _ensureAreaAndSeries(area, series); |
31 return true; | 35 return true; |
32 } | 36 } |
33 | 37 |
34 @override | 38 @override |
35 void draw(Element element, {Future schedulePostRender}) { | 39 void draw(Element element, {Future schedulePostRender}) { |
36 _ensureReadyToDraw(element); | 40 _ensureReadyToDraw(element); |
37 var verticalBars = !area.config.isLeftAxisPrimary; | 41 var verticalBars = !area.config.isLeftAxisPrimary; |
38 | 42 |
39 var measuresCount = series.measures.length, | 43 var measuresCount = series.measures.length, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 var offset = e.dataset['offset'], | 86 var offset = e.dataset['offset'], |
83 offsetVal = offset != null ? int.parse(offset) : 0; | 87 offsetVal = offset != null ? int.parse(offset) : 0; |
84 if (i == 0) { | 88 if (i == 0) { |
85 prevOffsetVal.add(offsetVal); | 89 prevOffsetVal.add(offsetVal); |
86 } else { | 90 } else { |
87 prevOffsetVal[prevOffsetVal.length - 1] = offsetVal; | 91 prevOffsetVal[prevOffsetVal.length - 1] = offsetVal; |
88 } | 92 } |
89 }); | 93 }); |
90 } | 94 } |
91 | 95 |
92 var barWidth = dimensionScale.rangeBand - theme.defaultStrokeWidth; | 96 var barWidth = |
| 97 (dimensionScale as OrdinalScale).rangeBand - theme.defaultStrokeWidth; |
93 | 98 |
94 // Calculate height of each segment in the bar. | 99 // Calculate height of each segment in the bar. |
95 // Uses prevAllZeroHeight and prevOffset to track previous segments | 100 // Uses prevAllZeroHeight and prevOffset to track previous segments |
96 var prevAllZeroHeight = true, prevOffset = 0; | 101 var prevAllZeroHeight = true, prevOffset = 0; |
97 var getBarLength = (d, i) { | 102 var getBarLength = (d, i) { |
98 if (!verticalBars) return measureScale.scale(d).round(); | 103 if (!verticalBars) return measureScale.scale(d).round(); |
99 var retval = rect.height - measureScale.scale(d).round(); | 104 var retval = rect.height - measureScale.scale(d).round(); |
100 if (i != 0) { | 105 if (i != 0) { |
101 // If previous bars has 0 height, don't offset for spacing | 106 // If previous bars has 0 height, don't offset for spacing |
102 // If any of the previous bar has non 0 height, do the offset. | 107 // If any of the previous bar has non 0 height, do the offset. |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 var row = rowStr != null ? int.parse(rowStr) : null; | 323 var row = rowStr != null ? int.parse(rowStr) : null; |
319 controller.add(new DefaultChartEventImpl(scope.event, area, series, row, | 324 controller.add(new DefaultChartEventImpl(scope.event, area, series, row, |
320 series.measures.elementAt(_reverseIdx(index)), data)); | 325 series.measures.elementAt(_reverseIdx(index)), data)); |
321 } | 326 } |
322 | 327 |
323 // Stacked bar chart renders items from bottom to top (first measure is at | 328 // Stacked bar chart renders items from bottom to top (first measure is at |
324 // the bottom of the stack). We use [_reversedIdx] instead of index to | 329 // the bottom of the stack). We use [_reversedIdx] instead of index to |
325 // match the color and order of what is displayed in the legend. | 330 // match the color and order of what is displayed in the legend. |
326 int _reverseIdx(int index) => series.measures.length - 1 - index; | 331 int _reverseIdx(int index) => series.measures.length - 1 - index; |
327 } | 332 } |
OLD | NEW |