Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: runtime/bin/vmstats/bargraph.dart

Issue 12377099: Added support for "bin-ified" vmstats web app source files (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Final code review changes Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/resources.h ('k') | runtime/bin/vmstats/isolate_list.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of dart.vmstats;
6
7 class BarGraph {
8 CanvasElement _canvas;
9 GraphModel _model;
10 List<Element> _elements;
11 double scaleHeight = 0;
12
13 static const int SAMPLE_WIDTH = 5;
14 static const int LEFT_MARGIN = 50;
15 static const int RIGHT_MARGIN = 150;
16 static const int LEGEND_WIDTH = 130;
17 static const int LEGEND_Y = 20;
18 static const int INSIDE_MARGIN = 2;
19
20 static const int NUM_DIVIDERS = 5;
21 static const String FONT = "14px sans-serif";
22
23 BarGraph(this._canvas, this._elements) {
24 var maxElements =
25 (_canvas.width - LEFT_MARGIN - RIGHT_MARGIN) ~/ SAMPLE_WIDTH;
26 _model = new GraphModel(maxElements);
27 _model.addListener(drawGraph, null);
28 drawBarGraph();
29 }
30
31 void addSample(List<int> segments) {
32 if (segments.length != _elements.length) {
33 throw new RuntimeError('invalid sample size for graph');
34 }
35 _model.addSample(segments);
36 }
37
38 void drawBarGraph() {
39 // Draw chart's outer box.
40 var context = _canvas.context2d;
41 context.beginPath();
42 context.strokeStyle = 'black';
43 // The '2's are the width of the line, even though 1 is specified.
44 context.strokeRect(
45 LEFT_MARGIN - 2, 1, _canvas.width - LEFT_MARGIN - RIGHT_MARGIN + 2,
46 _canvas.height - 2, 1);
47
48 // Draw legend.
49 var x = _canvas.width - LEGEND_WIDTH;
50 var y = LEGEND_Y;
51 context.font = FONT;
52 for (var i = 0; i < _elements.length; i++) {
53 context.fillStyle = _elements[i].color;
54 context.fillRect(x, y, 20, 20);
55 context.fillStyle = 'black';
56 context.fillText(_elements[i].name, x + 30, y + 15);
57 y += 30;
58 }
59 }
60
61 void drawGraph(GraphModel model) {
62 var graphHeight = model.maxTotal;
63 var width = _canvas.clientWidth;
64 var height = _canvas.clientHeight;
65 if (graphHeight >= scaleHeight) {
66 // Make scale height a bit higher to allow for growth, and
67 // round to nearest 100.
68 scaleHeight = graphHeight * 1.2;
69 scaleHeight = ((scaleHeight / 100).ceil() * 100);
70 }
71 var scale = height / scaleHeight;
72 drawValues(scaleHeight, scale);
73 drawChart(scaleHeight, scale);
74 }
75
76 void drawChart(int maxHeight, double scale) {
77 var dividerHeight = maxHeight ~/ NUM_DIVIDERS;
78 var context = _canvas.context2d;
79 context.beginPath();
80 var height = maxHeight.toInt();
81 var scaledY = dividerHeight * scale;
82
83 // Draw the vertical axis values and lines.
84 for (var i = 1; i < NUM_DIVIDERS; i++) {
85 height -= (dividerHeight ~/ 100) * 100;
86 context.font = FONT;
87 context.fillStyle = 'black';
88 context.textAlign = 'right';
89 context.textBaseline = 'middle';
90 context.fillText(height.toString(), LEFT_MARGIN - 10, scaledY);
91 context.moveTo(LEFT_MARGIN - 2, scaledY);
92 context.strokeStyle = 'grey';
93 context.lineWidth = 0.5;
94 context.lineTo(_canvas.width - RIGHT_MARGIN, scaledY);
95 context.stroke();
96 scaledY += dividerHeight * scale;
97 }
98 }
99
100 void drawValues(int maxHeight, num scale) {
101 Iterator<Sample> iterator = _model.iterator;
102 var x = LEFT_MARGIN + INSIDE_MARGIN;
103
104 while (iterator.moveNext()) {
105 Sample s = iterator.current;
106 var y = INSIDE_MARGIN;
107 if (s != null) {
108 var blankHeight = scaleHeight - s.total();
109 drawVerticalSegment(x, y, SAMPLE_WIDTH, blankHeight, 'white', scale);
110 y += blankHeight;
111 for (int i = s.length - 1; i >= 0; i--) {
112 var h = s[i];
113 drawVerticalSegment(x, y, SAMPLE_WIDTH, h, _elements[i].color, scale);
114 y += s[i];
115 }
116 } else {
117 drawVerticalSegment(x, INSIDE_MARGIN, SAMPLE_WIDTH,
118 maxHeight, 'white', scale);
119 }
120 x += SAMPLE_WIDTH ;
121 }
122 }
123
124 void drawVerticalSegment(int x, int y, int w, int h, String color,
125 num scale) {
126 var context = _canvas.context2d;
127 y = (y * scale).floor();
128 h = (h * scale).ceil();
129 context.beginPath();
130 context.lineWidth = w;
131 context.fillStyle = color;
132 context.strokeStyle = color;
133 if (x < INSIDE_MARGIN) {
134 x = INSIDE_MARGIN;
135 }
136 if (y < INSIDE_MARGIN) {
137 y = INSIDE_MARGIN;
138 }
139 var max = _canvas.width - INSIDE_MARGIN;
140 if ((x + w) > max) {
141 w = max - x;
142 }
143 max = _canvas.height - INSIDE_MARGIN;
144 if ((y + h) > max) {
145 h = max - y;
146 }
147 context.moveTo(x, y);
148 context.lineTo(x, y + h);
149 context.stroke();
150 }
151 }
152
153 class GraphModel extends ObservableModel {
154 List<Sample> _samples = new List<Sample>();
155 int _maxSize;
156
157 static const int _LARGE_LENGTH = 999999999;
158
159 GraphModel(this._maxSize) {}
160
161 void addSample(List<int> segments) {
162 var len = _samples.length;
163 if (_samples.length >= _maxSize) {
164 _samples.remove(_samples.first);
165 }
166 _samples.add(new Sample(segments));
167 notifySuccess();
168 }
169
170 int get maxSize => _maxSize;
171
172 Iterator<Sample> get iterator => _samples.iterator;
173
174 Sample operator[](int i) => _samples[i];
175
176 /**
177 * Returns the minimum total from all the samples.
178 */
179 int get minTotal {
180 var min = _LARGE_LENGTH;
181 _samples.forEach((Sample s) => min = (s.total() < min ? s.total() : min));
182 return min;
183 }
184
185 /**
186 * Returns the maximum total from all the samples.
187 */
188 int get maxTotal {
189 var max = 0;
190 _samples.forEach((Sample s) => max = (s.total() > max ? s.total() : max));
191 return max;
192 }
193 }
194
195 /**
196 * An element is a data type that gets charted. Each element has a name for
197 * the legend, and a color for the bar graph. The number of elements in a
198 * graph should match the number of segments in each sample.
199 */
200 class Element {
201 final String name;
202 final String color; // Any description the DOM will accept, like "red".
203
204 Element(this.name, this.color) {}
205 }
206
207 /**
208 * A sample is a list of segment lengths.
209 */
210 class Sample {
211 List<int> _segments;
212
213 Sample(this._segments) {}
214
215 int get length => _segments.length;
216 int operator[](int i) => _segments[i];
217
218 Iterator<int> get iterator => _segments.iterator;
219
220 int total() {
221 return _segments.reduce(0, (int prev, int element) => prev + element);
222 }
223 }
OLDNEW
« no previous file with comments | « runtime/bin/resources.h ('k') | runtime/bin/vmstats/isolate_list.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698