| 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; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 void addSample(List<int> segments) { | 32 void addSample(List<int> segments) { |
| 33 if (segments.length != _elements.length) { | 33 if (segments.length != _elements.length) { |
| 34 throw new RuntimeError('invalid sample size for graph'); | 34 throw new RuntimeError('invalid sample size for graph'); |
| 35 } | 35 } |
| 36 _model.addSample(segments); | 36 _model.addSample(segments); |
| 37 } | 37 } |
| 38 | 38 |
| 39 void drawBarGraph() { | 39 void drawBarGraph() { |
| 40 // Draw chart's outer box. | 40 // Draw chart's outer box. |
| 41 var context = _canvas.context2d; | 41 var context = _canvas.context2D; |
| 42 context.beginPath(); | 42 context.beginPath(); |
| 43 context.strokeStyle = 'black'; | 43 context.strokeStyle = 'black'; |
| 44 // 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. |
| 45 context.strokeRect( | 45 context.strokeRect( |
| 46 LEFT_MARGIN - 2, 1, _canvas.width - LEFT_MARGIN - RIGHT_MARGIN + 2, | 46 LEFT_MARGIN - 2, 1, _canvas.width - LEFT_MARGIN - RIGHT_MARGIN + 2, |
| 47 _canvas.height - 2, 1); | 47 _canvas.height - 2, 1); |
| 48 | 48 |
| 49 // Draw legend. | 49 // Draw legend. |
| 50 var x = _canvas.width - LEGEND_WIDTH; | 50 var x = _canvas.width - LEGEND_WIDTH; |
| 51 var y = LEGEND_Y; | 51 var y = LEGEND_Y; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 69 scaleHeight = graphHeight * 1.2; | 69 scaleHeight = graphHeight * 1.2; |
| 70 scaleHeight = ((scaleHeight / 100).ceil() * 100); | 70 scaleHeight = ((scaleHeight / 100).ceil() * 100); |
| 71 } | 71 } |
| 72 var scale = height / scaleHeight; | 72 var scale = height / scaleHeight; |
| 73 drawValues(scaleHeight, scale); | 73 drawValues(scaleHeight, scale); |
| 74 drawChart(scaleHeight, scale); | 74 drawChart(scaleHeight, scale); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void drawChart(int maxHeight, double scale) { | 77 void drawChart(int maxHeight, double scale) { |
| 78 var dividerHeight = maxHeight ~/ NUM_DIVIDERS; | 78 var dividerHeight = maxHeight ~/ NUM_DIVIDERS; |
| 79 var context = _canvas.context2d; | 79 var context = _canvas.context2D; |
| 80 context.beginPath(); | 80 context.beginPath(); |
| 81 var height = maxHeight.toInt(); | 81 var height = maxHeight.toInt(); |
| 82 var scaledY = dividerHeight * scale; | 82 var scaledY = dividerHeight * scale; |
| 83 | 83 |
| 84 // 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); | 85 context.clearRect(0, 0, LEFT_MARGIN - INSIDE_MARGIN, maxHeight); |
| 86 for (var i = 1; i < NUM_DIVIDERS; i++) { | 86 for (var i = 1; i < NUM_DIVIDERS; i++) { |
| 87 height -= (dividerHeight ~/ 100) * 100; | 87 height -= (dividerHeight ~/ 100) * 100; |
| 88 context.font = FONT; | 88 context.font = FONT; |
| 89 context.fillStyle = 'black'; | 89 context.fillStyle = 'black'; |
| 90 context.textAlign = 'right'; | 90 context.textAlign = 'right'; |
| 91 context.textBaseline = 'middle'; | 91 context.textBaseline = 'middle'; |
| 92 context.fillText(height.toString(), LEFT_MARGIN - 10, scaledY); | 92 context.fillText(height.toString(), LEFT_MARGIN - 10, scaledY); |
| 93 context.moveTo(LEFT_MARGIN - INSIDE_MARGIN, scaledY); | 93 context.moveTo(LEFT_MARGIN - INSIDE_MARGIN, scaledY); |
| 94 context.strokeStyle = 'grey'; | 94 context.strokeStyle = 'grey'; |
| 95 context.lineWidth = 0.5; | 95 context.lineWidth = 0.5; |
| 96 context.lineTo(_canvas.width - RIGHT_MARGIN, scaledY); | 96 context.lineTo(_canvas.width - RIGHT_MARGIN, scaledY); |
| 97 context.stroke(); | 97 context.stroke(); |
| 98 scaledY += dividerHeight * scale; | 98 scaledY += dividerHeight * scale; |
| 99 } | 99 } |
| 100 } | 100 } |
| 101 | 101 |
| 102 void drawValues(int maxHeight, num scale) { | 102 void drawValues(int maxHeight, num scale) { |
| 103 Iterator<Sample> iterator = _model.iterator; | 103 Iterator<Sample> iterator = _model.iterator; |
| 104 var x = LEFT_MARGIN + INSIDE_MARGIN; | 104 var x = LEFT_MARGIN + INSIDE_MARGIN; |
| 105 var y = INSIDE_MARGIN; | 105 var y = INSIDE_MARGIN; |
| 106 var w = _canvas.width - LEFT_MARGIN - RIGHT_MARGIN - INSIDE_MARGIN; | 106 var w = _canvas.width - LEFT_MARGIN - RIGHT_MARGIN - INSIDE_MARGIN; |
| 107 var h = (maxHeight * scale).ceil() - (2 * INSIDE_MARGIN); | 107 var h = (maxHeight * scale).ceil() - (2 * INSIDE_MARGIN); |
| 108 _canvas.context2d.clearRect(x, y, w, h); | 108 _canvas.context2D.clearRect(x, y, w, h); |
| 109 | 109 |
| 110 while (iterator.moveNext()) { | 110 while (iterator.moveNext()) { |
| 111 Sample s = iterator.current; | 111 Sample s = iterator.current; |
| 112 var y = INSIDE_MARGIN; | 112 var y = INSIDE_MARGIN; |
| 113 if (s != null) { | 113 if (s != null) { |
| 114 var blankHeight = scaleHeight - s.total(); | 114 var blankHeight = scaleHeight - s.total(); |
| 115 drawVerticalSegment(x, y, SAMPLE_WIDTH, blankHeight, 'white', scale); | 115 drawVerticalSegment(x, y, SAMPLE_WIDTH, blankHeight, 'white', scale); |
| 116 y += blankHeight; | 116 y += blankHeight; |
| 117 for (int i = s.length - 1; i >= 0; i--) { | 117 for (int i = s.length - 1; i >= 0; i--) { |
| 118 var h = s[i]; | 118 var h = s[i]; |
| 119 drawVerticalSegment(x, y, SAMPLE_WIDTH, h, _elements[i].color, scale); | 119 drawVerticalSegment(x, y, SAMPLE_WIDTH, h, _elements[i].color, scale); |
| 120 y += s[i]; | 120 y += s[i]; |
| 121 } | 121 } |
| 122 } else { | 122 } else { |
| 123 drawVerticalSegment(x, INSIDE_MARGIN, SAMPLE_WIDTH, | 123 drawVerticalSegment(x, INSIDE_MARGIN, SAMPLE_WIDTH, |
| 124 maxHeight, 'white', scale); | 124 maxHeight, 'white', scale); |
| 125 } | 125 } |
| 126 x += SAMPLE_WIDTH ; | 126 x += SAMPLE_WIDTH ; |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 | 129 |
| 130 void drawVerticalSegment(int x, int y, int w, int h, String color, | 130 void drawVerticalSegment(int x, int y, int w, int h, String color, |
| 131 num scale) { | 131 num scale) { |
| 132 var context = _canvas.context2d; | 132 var context = _canvas.context2D; |
| 133 y = (y * scale).floor(); | 133 y = (y * scale).floor(); |
| 134 h = (h * scale).ceil(); | 134 h = (h * scale).ceil(); |
| 135 context.beginPath(); | 135 context.beginPath(); |
| 136 context.lineWidth = w; | 136 context.lineWidth = w; |
| 137 context.fillStyle = color; | 137 context.fillStyle = color; |
| 138 context.strokeStyle = color; | 138 context.strokeStyle = color; |
| 139 if (x < INSIDE_MARGIN) { | 139 if (x < INSIDE_MARGIN) { |
| 140 x = INSIDE_MARGIN; | 140 x = INSIDE_MARGIN; |
| 141 } | 141 } |
| 142 if (y < INSIDE_MARGIN) { | 142 if (y < INSIDE_MARGIN) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 | 216 |
| 217 int get length => _segments.length; | 217 int get length => _segments.length; |
| 218 int operator[](int i) => _segments[i]; | 218 int operator[](int i) => _segments[i]; |
| 219 | 219 |
| 220 Iterator<int> get iterator => _segments.iterator; | 220 Iterator<int> get iterator => _segments.iterator; |
| 221 | 221 |
| 222 int total() { | 222 int total() { |
| 223 return _segments.fold(0, (int prev, int element) => prev + element); | 223 return _segments.fold(0, (int prev, int element) => prev + element); |
| 224 } | 224 } |
| 225 } | 225 } |
| OLD | NEW |