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 /// A behavior that draws marking lines on the chart. | 11 /// A behavior that draws marking lines on the chart. |
12 class LineMarker implements ChartBehavior { | 12 class LineMarker implements ChartBehavior { |
13 /// Position of the line markers. | 13 /// Position of the line markers. |
14 final Map<int,dynamic> positions; | 14 final Map<int, dynamic> positions; |
15 | 15 |
16 /// If true, the markers are drawn above the series | 16 /// If true, the markers are drawn above the series |
17 final bool drawAboveSeries; | 17 final bool drawAboveSeries; |
18 | 18 |
19 /// If true, animates (grows from the axis into the chart) | 19 /// If true, animates (grows from the axis into the chart) |
20 final bool animate; | 20 final bool animate; |
21 | 21 |
22 CartesianArea _area; | 22 CartesianArea _area; |
23 bool _isLeftAxisPrimary = false; | 23 bool _isLeftAxisPrimary = false; |
24 Rect _rect; | 24 Rect _rect; |
25 | 25 |
26 bool _showing; | 26 bool _showing; |
27 Selection _parent; | 27 Selection _parent; |
28 DataSelection _markers; | 28 DataSelection _markers; |
29 | 29 |
30 StreamSubscription _axesChangeSubscription; | 30 StreamSubscription _axesChangeSubscription; |
31 | 31 |
32 LineMarker(this.positions, | 32 LineMarker(this.positions, |
33 {this.drawAboveSeries: false, this.animate: false}); | 33 {this.drawAboveSeries: false, this.animate: false}); |
34 | 34 |
35 void init(ChartArea area, Selection upper, Selection lower) { | 35 void init(ChartArea area, Selection upper, Selection lower) { |
36 if (area is! CartesianArea) return; | 36 if (area is! CartesianArea) return; |
37 _area = area; | 37 _area = area; |
38 _parent = drawAboveSeries ? upper : lower; | 38 _parent = drawAboveSeries ? upper : lower; |
39 _isLeftAxisPrimary = _area.config.isLeftAxisPrimary; | 39 _isLeftAxisPrimary = _area.config.isLeftAxisPrimary; |
40 _axesChangeSubscription = | 40 _axesChangeSubscription = _area.onChartAxesUpdated.listen((_) => _update()); |
41 _area.onChartAxesUpdated.listen((_) => _update()); | |
42 _update(); | 41 _update(); |
43 } | 42 } |
44 | 43 |
45 void dispose() { | 44 void dispose() { |
46 if (_axesChangeSubscription != null) _axesChangeSubscription.cancel(); | 45 if (_axesChangeSubscription != null) _axesChangeSubscription.cancel(); |
47 if (_markers != null) _markers.remove(); | 46 if (_markers != null) _markers.remove(); |
48 } | 47 } |
49 | 48 |
50 bool _isDimension(int column) => _area.config.dimensions.contains(column); | 49 bool _isDimension(int column) => _area.config.dimensions.contains(column); |
51 | 50 |
52 String _pathForDimension(int column, bool initial) { | 51 String _pathForDimension(int column, bool initial) { |
53 assert(_isDimension(column)); | 52 assert(_isDimension(column)); |
54 | 53 |
55 int index; | 54 int index; |
56 for(index = 0; | 55 for (index = 0; |
57 _area.config.dimensions.elementAt(index) != column; ++index); | 56 _area.config.dimensions.elementAt(index) != column; |
| 57 ++index); |
58 | 58 |
59 assert(index == 0 || index == 1 && _area.useTwoDimensionAxes); | 59 assert(index == 0 || index == 1 && _area.useTwoDimensionAxes); |
60 | 60 |
61 var dimensionAtBottom = | 61 var dimensionAtBottom = |
62 index == 1 && _isLeftAxisPrimary || | 62 index == 1 && _isLeftAxisPrimary || index == 0 && !_isLeftAxisPrimary, |
63 index == 0 && !_isLeftAxisPrimary, | |
64 scale = _area.dimensionScales.elementAt(index), | 63 scale = _area.dimensionScales.elementAt(index), |
65 scaled = scale.scale(positions[column]), | 64 scaled = scale.scale(positions[column]), |
66 theme = _area.theme.getDimensionAxisTheme(), | 65 theme = _area.theme.getDimensionAxisTheme(), |
67 renderAreaRect = _area.layout.renderArea, | 66 renderAreaRect = _area.layout.renderArea, |
68 left = renderAreaRect.x, | 67 left = renderAreaRect.x, |
69 right = initial ? left : (left + renderAreaRect.width), | 68 right = initial ? left : (left + renderAreaRect.width), |
70 bottom = renderAreaRect.y + renderAreaRect.height, | 69 bottom = renderAreaRect.y + renderAreaRect.height, |
71 top = initial ? bottom : renderAreaRect.y; | 70 top = initial ? bottom : renderAreaRect.y; |
72 | 71 |
73 if (scale is OrdinalScale) { | 72 if (scale is OrdinalScale) { |
74 var band = scale.rangeBand, | 73 var band = scale.rangeBand, bandPadding = theme.axisBandInnerPadding; |
75 bandPadding = theme.axisBandInnerPadding; | |
76 scaled = scaled - band * bandPadding + _area.theme.defaultStrokeWidth; | 74 scaled = scaled - band * bandPadding + _area.theme.defaultStrokeWidth; |
77 band = band + 2 * (band * bandPadding - _area.theme.defaultStrokeWidth); | 75 band = band + 2 * (band * bandPadding - _area.theme.defaultStrokeWidth); |
78 return dimensionAtBottom | 76 return dimensionAtBottom |
79 ? 'M ${left + scaled} ${bottom} V ${top} H ${left + scaled + band} V $
{bottom} Z' | 77 ? 'M ${left + scaled} ${bottom} V ${top} H ${left + scaled + band} V $
{bottom} Z' |
80 : 'M ${left} ${scaled + band} H ${right} V ${scaled - band} H ${left}
Z'; | 78 : 'M ${left} ${scaled + band} H ${right} V ${scaled - band} H ${left}
Z'; |
81 } else { | 79 } else { |
82 return dimensionAtBottom | 80 return dimensionAtBottom |
83 ? 'M ${left + scaled} ${bottom} V ${top}' | 81 ? 'M ${left + scaled} ${bottom} V ${top}' |
84 : 'M ${left} ${scaled} H ${right}'; | 82 : 'M ${left} ${scaled} H ${right}'; |
85 } | 83 } |
86 } | 84 } |
87 | 85 |
88 String _pathForMeasure(int column, bool initial) { | 86 String _pathForMeasure(int column, bool initial) { |
89 throw new UnimplementedError('Measure axis markers'); | 87 throw new UnimplementedError('Measure axis markers'); |
90 } | 88 } |
91 | 89 |
92 String _getMarkerPath(int column, bool initial) => | 90 String _getMarkerPath(int column, bool initial) => _isDimension(column) |
93 _isDimension(column) | 91 ? _pathForDimension(column, initial) |
94 ? _pathForDimension(column, initial) | 92 : _pathForMeasure(column, initial); |
95 : _pathForMeasure(column, initial); | |
96 | 93 |
97 void _update() { | 94 void _update() { |
98 if (!_area.isReady) return; | 95 if (!_area.isReady) return; |
99 _markers = _parent.selectAll('.line-marker').data(positions.keys); | 96 _markers = _parent.selectAll('.line-marker').data(positions.keys); |
100 | 97 |
101 _markers.enter.append('path').each((d, i, e) { | 98 _markers.enter.append('path').each((d, i, e) { |
102 e.classes.add('line-marker'); | 99 e.classes.add('line-marker'); |
103 e.attributes['d'] = _getMarkerPath(d, animate); | 100 e.attributes['d'] = _getMarkerPath(d, animate); |
104 }); | 101 }); |
105 | 102 |
106 if (animate) { | 103 if (animate) { |
107 _markers.transition() | 104 _markers |
| 105 .transition() |
108 .attrWithCallback('d', (d, i, e) => _getMarkerPath(d, false)); | 106 .attrWithCallback('d', (d, i, e) => _getMarkerPath(d, false)); |
109 } | 107 } |
110 | 108 |
111 _markers.exit.remove(); | 109 _markers.exit.remove(); |
112 } | 110 } |
113 } | 111 } |
114 | |
OLD | NEW |