| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.vmstats; | 5 part of dart.vmstats; |
| 6 | 6 |
| 7 class BarGraph { | 7 class BarGraph { |
| 8 CanvasElement _canvas; | 8 CanvasElement _canvas; |
| 9 GraphModel _model; | 9 GraphModel _model; |
| 10 List<Element> _elements; | 10 List<Element> _elements; |
| 11 double scaleHeight = 0; | 11 double scaleHeight = 0; |
| 12 | 12 |
| 13 static const int SAMPLE_WIDTH = 5; | 13 static const int SAMPLE_WIDTH = 5; |
| 14 static const int LEFT_MARGIN = 50; | 14 static const int LEFT_MARGIN = 50; |
| 15 static const int RIGHT_MARGIN = 150; | 15 static const int RIGHT_MARGIN = 150; |
| 16 static const int LEGEND_WIDTH = 130; | 16 static const int LEGEND_WIDTH = 130; |
| 17 static const int LEGEND_Y = 20; | 17 static const int LEGEND_Y = 20; |
| 18 static const int INSIDE_MARGIN = 2; | 18 static const int INSIDE_MARGIN = 2; |
| 19 static const int LINE_WIDTH = 2; |
| 19 | 20 |
| 20 static const int NUM_DIVIDERS = 5; | 21 static const int NUM_DIVIDERS = 5; |
| 21 static const String FONT = "14px sans-serif"; | 22 static const String FONT = "14px sans-serif"; |
| 22 | 23 |
| 23 BarGraph(this._canvas, this._elements) { | 24 BarGraph(this._canvas, this._elements) { |
| 24 var maxElements = | 25 var maxElements = |
| 25 (_canvas.width - LEFT_MARGIN - RIGHT_MARGIN) ~/ SAMPLE_WIDTH; | 26 (_canvas.width - LEFT_MARGIN - RIGHT_MARGIN) ~/ SAMPLE_WIDTH; |
| 26 _model = new GraphModel(maxElements); | 27 _model = new GraphModel(maxElements); |
| 27 _model.addListener(drawGraph, null); | 28 _model.addListener(drawGraph, null); |
| 28 drawBarGraph(); | 29 drawBarGraph(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 42 context.strokeStyle = 'black'; | 43 context.strokeStyle = 'black'; |
| 43 // The '2's are the width of the line, even though 1 is specified. | 44 // The '2's are the width of the line, even though 1 is specified. |
| 44 context.strokeRect( | 45 context.strokeRect( |
| 45 LEFT_MARGIN - 2, 1, _canvas.width - LEFT_MARGIN - RIGHT_MARGIN + 2, | 46 LEFT_MARGIN - 2, 1, _canvas.width - LEFT_MARGIN - RIGHT_MARGIN + 2, |
| 46 _canvas.height - 2, 1); | 47 _canvas.height - 2, 1); |
| 47 | 48 |
| 48 // Draw legend. | 49 // Draw legend. |
| 49 var x = _canvas.width - LEGEND_WIDTH; | 50 var x = _canvas.width - LEGEND_WIDTH; |
| 50 var y = LEGEND_Y; | 51 var y = LEGEND_Y; |
| 51 context.font = FONT; | 52 context.font = FONT; |
| 52 for (var i = 0; i < _elements.length; i++) { | 53 for (var i = _elements.length - 1; i >= 0; i--) { |
| 53 context.fillStyle = _elements[i].color; | 54 context.fillStyle = _elements[i].color; |
| 54 context.fillRect(x, y, 20, 20); | 55 context.fillRect(x, y, 20, 20); |
| 55 context.fillStyle = 'black'; | 56 context.fillStyle = 'black'; |
| 56 context.fillText(_elements[i].name, x + 30, y + 15); | 57 context.fillText(_elements[i].name, x + 30, y + 15); |
| 57 y += 30; | 58 y += 30; |
| 58 } | 59 } |
| 59 } | 60 } |
| 60 | 61 |
| 61 void drawGraph(GraphModel model) { | 62 void drawGraph(GraphModel model) { |
| 62 var graphHeight = model.maxTotal; | 63 var graphHeight = model.maxTotal; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 74 } | 75 } |
| 75 | 76 |
| 76 void drawChart(int maxHeight, double scale) { | 77 void drawChart(int maxHeight, double scale) { |
| 77 var dividerHeight = maxHeight ~/ NUM_DIVIDERS; | 78 var dividerHeight = maxHeight ~/ NUM_DIVIDERS; |
| 78 var context = _canvas.context2d; | 79 var context = _canvas.context2d; |
| 79 context.beginPath(); | 80 context.beginPath(); |
| 80 var height = maxHeight.toInt(); | 81 var height = maxHeight.toInt(); |
| 81 var scaledY = dividerHeight * scale; | 82 var scaledY = dividerHeight * scale; |
| 82 | 83 |
| 83 // Draw the vertical axis values and lines. | 84 // Draw the vertical axis values and lines. |
| 85 context.clearRect(0, 0, LEFT_MARGIN - INSIDE_MARGIN, maxHeight); |
| 84 for (var i = 1; i < NUM_DIVIDERS; i++) { | 86 for (var i = 1; i < NUM_DIVIDERS; i++) { |
| 85 height -= (dividerHeight ~/ 100) * 100; | 87 height -= (dividerHeight ~/ 100) * 100; |
| 86 context.font = FONT; | 88 context.font = FONT; |
| 87 context.fillStyle = 'black'; | 89 context.fillStyle = 'black'; |
| 88 context.textAlign = 'right'; | 90 context.textAlign = 'right'; |
| 89 context.textBaseline = 'middle'; | 91 context.textBaseline = 'middle'; |
| 90 context.fillText(height.toString(), LEFT_MARGIN - 10, scaledY); | 92 context.fillText(height.toString(), LEFT_MARGIN - 10, scaledY); |
| 91 context.moveTo(LEFT_MARGIN - 2, scaledY); | 93 context.moveTo(LEFT_MARGIN - INSIDE_MARGIN, scaledY); |
| 92 context.strokeStyle = 'grey'; | 94 context.strokeStyle = 'grey'; |
| 93 context.lineWidth = 0.5; | 95 context.lineWidth = 0.5; |
| 94 context.lineTo(_canvas.width - RIGHT_MARGIN, scaledY); | 96 context.lineTo(_canvas.width - RIGHT_MARGIN, scaledY); |
| 95 context.stroke(); | 97 context.stroke(); |
| 96 scaledY += dividerHeight * scale; | 98 scaledY += dividerHeight * scale; |
| 97 } | 99 } |
| 98 } | 100 } |
| 99 | 101 |
| 100 void drawValues(int maxHeight, num scale) { | 102 void drawValues(int maxHeight, num scale) { |
| 101 Iterator<Sample> iterator = _model.iterator; | 103 Iterator<Sample> iterator = _model.iterator; |
| 102 var x = LEFT_MARGIN + INSIDE_MARGIN; | 104 var x = LEFT_MARGIN + INSIDE_MARGIN; |
| 105 var y = INSIDE_MARGIN; |
| 106 var w = _canvas.width - LEFT_MARGIN - RIGHT_MARGIN - INSIDE_MARGIN; |
| 107 var h = (maxHeight * scale).ceil() - (2 * INSIDE_MARGIN); |
| 108 _canvas.context2d.clearRect(x, y, w, h); |
| 103 | 109 |
| 104 while (iterator.moveNext()) { | 110 while (iterator.moveNext()) { |
| 105 Sample s = iterator.current; | 111 Sample s = iterator.current; |
| 106 var y = INSIDE_MARGIN; | 112 var y = INSIDE_MARGIN; |
| 107 if (s != null) { | 113 if (s != null) { |
| 108 var blankHeight = scaleHeight - s.total(); | 114 var blankHeight = scaleHeight - s.total(); |
| 109 drawVerticalSegment(x, y, SAMPLE_WIDTH, blankHeight, 'white', scale); | 115 drawVerticalSegment(x, y, SAMPLE_WIDTH, blankHeight, 'white', scale); |
| 110 y += blankHeight; | 116 y += blankHeight; |
| 111 for (int i = s.length - 1; i >= 0; i--) { | 117 for (int i = s.length - 1; i >= 0; i--) { |
| 112 var h = s[i]; | 118 var h = s[i]; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 129 context.beginPath(); | 135 context.beginPath(); |
| 130 context.lineWidth = w; | 136 context.lineWidth = w; |
| 131 context.fillStyle = color; | 137 context.fillStyle = color; |
| 132 context.strokeStyle = color; | 138 context.strokeStyle = color; |
| 133 if (x < INSIDE_MARGIN) { | 139 if (x < INSIDE_MARGIN) { |
| 134 x = INSIDE_MARGIN; | 140 x = INSIDE_MARGIN; |
| 135 } | 141 } |
| 136 if (y < INSIDE_MARGIN) { | 142 if (y < INSIDE_MARGIN) { |
| 137 y = INSIDE_MARGIN; | 143 y = INSIDE_MARGIN; |
| 138 } | 144 } |
| 139 var max = _canvas.width - INSIDE_MARGIN; | 145 var max = _canvas.height - INSIDE_MARGIN; |
| 140 if ((x + w) > max) { | |
| 141 w = max - x; | |
| 142 } | |
| 143 max = _canvas.height - INSIDE_MARGIN; | |
| 144 if ((y + h) > max) { | 146 if ((y + h) > max) { |
| 145 h = max - y; | 147 h = max - y; |
| 146 } | 148 } |
| 147 context.moveTo(x, y); | 149 context.moveTo(x, y); |
| 148 context.lineTo(x, y + h); | 150 context.lineTo(x, y + h); |
| 149 context.stroke(); | 151 context.stroke(); |
| 150 } | 152 } |
| 151 } | 153 } |
| 152 | 154 |
| 153 class GraphModel extends ObservableModel { | 155 class GraphModel extends ObservableModel { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 216 |
| 215 int get length => _segments.length; | 217 int get length => _segments.length; |
| 216 int operator[](int i) => _segments[i]; | 218 int operator[](int i) => _segments[i]; |
| 217 | 219 |
| 218 Iterator<int> get iterator => _segments.iterator; | 220 Iterator<int> get iterator => _segments.iterator; |
| 219 | 221 |
| 220 int total() { | 222 int total() { |
| 221 return _segments.reduce(0, (int prev, int element) => prev + element); | 223 return _segments.reduce(0, (int prev, int element) => prev + element); |
| 222 } | 224 } |
| 223 } | 225 } |
| OLD | NEW |