| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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 class ClientChart extends Chart { | |
| 6 | |
| 7 static final List<String> _strokeColors = | |
| 8 const ["#ff0000", "#00ff00", "#0000ff", "#ffff00", "#ff00ff", "#00ffff", "#
000000"]; | |
| 9 | |
| 10 CanvasElement _canvas; | |
| 11 | |
| 12 ClientChart(this._canvas) : super() { } | |
| 13 | |
| 14 void render() { } | |
| 15 } | |
| 16 | |
| 17 class ClientLineChart extends ClientChart { | |
| 18 | |
| 19 ClientLineChart(CanvasElement canvas) : super(canvas) { | |
| 20 _width = _canvas.width; | |
| 21 _height = _canvas.height; | |
| 22 } | |
| 23 | |
| 24 void render() { | |
| 25 CanvasRenderingContext2D ctx = _canvas.getContext("2d"); | |
| 26 ctx.setFillColor("#ffffff"); | |
| 27 ctx.clearRect(0, 0, _width, _height); | |
| 28 | |
| 29 int tickStep; | |
| 30 int tickMin; | |
| 31 int tickMax; | |
| 32 | |
| 33 double range = _maxValue - _minValue; | |
| 34 if (range == 0) { | |
| 35 tickStep = 1; | |
| 36 int m = _minValue.floor().toInt(); | |
| 37 tickMin = m - 5; | |
| 38 tickMax = m + 5; | |
| 39 } else { | |
| 40 double log10Range = Math.log(range) / Math.log(10.0); | |
| 41 tickStep = Math.pow(10.0, (log10Range).floor()).toInt() ~/ 2; | |
| 42 tickMin = (_minValue / tickStep).floor().toInt() * tickStep; | |
| 43 tickMax = (_maxValue / tickStep).floor().toInt() * tickStep; | |
| 44 } | |
| 45 | |
| 46 int padding = 30; | |
| 47 | |
| 48 ctx.font = "Gill Sans 8px"; | |
| 49 ctx.lineWidth = 1.0; | |
| 50 | |
| 51 ctx.setStrokeColor("#000000"); | |
| 52 ctx.beginPath(); | |
| 53 ctx.moveTo(padding, padding); | |
| 54 ctx.lineTo(padding, height - padding); | |
| 55 ctx.lineTo(width - padding, height - padding); | |
| 56 ctx.stroke(); | |
| 57 | |
| 58 int scaleY = (height - 2 * padding) ~/ range; | |
| 59 double zeroY = (height - padding) - ((0.0 - _minValue) * scaleY); | |
| 60 if (zeroY >= padding && zeroY <= height - padding) { | |
| 61 ctx.beginPath(); | |
| 62 ctx.moveTo(padding, zeroY); | |
| 63 ctx.lineTo(width - padding, zeroY); | |
| 64 ctx.stroke(); | |
| 65 | |
| 66 double textWidth = ctx.measureText("0").width.toDouble(); | |
| 67 ctx.fillText("0", padding - textWidth, zeroY); | |
| 68 } | |
| 69 | |
| 70 int tickLen = 6; | |
| 71 for (int tick = tickMin; tick <= tickMax; tick += tickStep) { | |
| 72 double y = (height - padding) - ((tick - _minValue) * scaleY); | |
| 73 if (y >= padding && y <= height - padding) { | |
| 74 ctx.beginPath(); | |
| 75 ctx.moveTo(padding - tickLen / 2, y); | |
| 76 ctx.lineTo(padding + tickLen / 2, y); | |
| 77 ctx.stroke(); | |
| 78 | |
| 79 String label = tick.toString(); | |
| 80 double textWidth = ctx.measureText(label).width.toDouble(); | |
| 81 ctx.fillText(label, padding - textWidth, y); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 for (int tick = padding; tick <= width - padding; tick += 50) { | |
| 86 ctx.beginPath(); | |
| 87 ctx.moveTo(tick, height - padding - tickLen / 2); | |
| 88 ctx.lineTo(tick, height - padding + tickLen / 2); | |
| 89 ctx.stroke(); | |
| 90 } | |
| 91 | |
| 92 for (int col = _firstCol; col <= _lastCol; col++) { | |
| 93 List<double> data = getSeries(col); | |
| 94 int scaleX = (width - 2 * padding) ~/ data.length; | |
| 95 scaleY = (height - 2 * padding) ~/ range; | |
| 96 | |
| 97 ctx.setStrokeColor(_strokeColors[col - _firstCol]); | |
| 98 ctx.beginPath(); | |
| 99 | |
| 100 for (int row = 0; row < data.length; row++) { | |
| 101 double x = row * scaleX + padding; | |
| 102 double y = (height - padding) - ((data[row] - _minValue) * scaleY); | |
| 103 if (row == 0) { | |
| 104 ctx.moveTo(x, y); | |
| 105 } else { | |
| 106 ctx.lineTo(x, y); | |
| 107 } | |
| 108 } | |
| 109 ctx.stroke(); | |
| 110 } | |
| 111 } | |
| 112 } | |
| OLD | NEW |