| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 app; | |
| 6 | |
| 7 class GoogleChart { | |
| 8 static var _api; | |
| 9 | |
| 10 /// Get access to the JsObject containing the Google Chart API: | |
| 11 /// https://developers.google.com/chart/interactive/docs/reference | |
| 12 static get api { | |
| 13 return _api; | |
| 14 } | |
| 15 | |
| 16 static Completer _completer = new Completer(); | |
| 17 | |
| 18 static Future get onReady => _completer.future; | |
| 19 | |
| 20 static bool get ready => _completer.isCompleted; | |
| 21 | |
| 22 /// Load the Google Chart API. Returns a [Future] which completes | |
| 23 /// when the API is loaded. | |
| 24 static Future initOnce() { | |
| 25 Logger.root.info('Loading Google Charts API'); | |
| 26 context['google'].callMethod('load', | |
| 27 ['visualization', '1', new JsObject.jsify({ | |
| 28 'packages': ['corechart', 'table'], | |
| 29 'callback': new JsFunction.withThis(_completer.complete) | |
| 30 })]); | |
| 31 return _completer.future.then(_initOnceOnComplete); | |
| 32 } | |
| 33 | |
| 34 static _initOnceOnComplete(_) { | |
| 35 Logger.root.info('Google Charts API loaded'); | |
| 36 _api = context['google']['visualization']; | |
| 37 assert(_api != null); | |
| 38 return _api; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 class DataTable { | |
| 43 final _table = new JsObject(GoogleChart.api['DataTable']); | |
| 44 /// Construct a Google Chart DataTable. | |
| 45 DataTable(); | |
| 46 | |
| 47 /// Number of columns. | |
| 48 int get columns => _table.callMethod('getNumberOfColumns'); | |
| 49 /// Number of rows. | |
| 50 int get rows => _table.callMethod('getNumberOfRows'); | |
| 51 | |
| 52 /// Add a new column with [type] and [label]. | |
| 53 /// type must be: 'string', 'number', or 'boolean'. | |
| 54 void addColumn(String type, String label) { | |
| 55 _table.callMethod('addColumn', [type, label]); | |
| 56 } | |
| 57 | |
| 58 /// Add a new column with [type], [label] and [role]. | |
| 59 /// Roles are used for metadata such as 'interval', 'annotation', or 'domain'. | |
| 60 /// type must be: 'string', 'number', or 'boolean'. | |
| 61 void addRoleColumn(String type, String label, String role) { | |
| 62 _table.callMethod('addColumn', [new JsObject.jsify({ | |
| 63 'type': type, | |
| 64 'label': label, | |
| 65 'role': role, | |
| 66 })]); | |
| 67 } | |
| 68 | |
| 69 /// Remove [count] columns starting with [start]. | |
| 70 void removeColumns(int start, int count) { | |
| 71 _table.callMethod('removeColumns', [start, count]); | |
| 72 } | |
| 73 | |
| 74 /// Remove all columns in the table. | |
| 75 void clearColumns() { | |
| 76 removeColumns(0, columns); | |
| 77 } | |
| 78 | |
| 79 /// Remove [count] rows starting with [start]. | |
| 80 void removeRows(int start, int count) { | |
| 81 _table.callMethod('removeRows', [start, count]); | |
| 82 } | |
| 83 | |
| 84 /// Remove all rows in the table. | |
| 85 void clearRows() { | |
| 86 removeRows(0, rows); | |
| 87 } | |
| 88 | |
| 89 /// Adds a new row to the table. [row] must have an entry for each | |
| 90 /// column in the table. | |
| 91 void addRow(List row) { | |
| 92 _table.callMethod('addRow', [new JsArray.from(row)]); | |
| 93 } | |
| 94 | |
| 95 void addTimeOfDayValue(DateTime dt, value) { | |
| 96 var array = new JsArray.from([dt.hour, dt.minute, dt.second]); | |
| 97 addRow([array, value]); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 class Chart { | |
| 102 var _chart; | |
| 103 final Map options = new Map(); | |
| 104 | |
| 105 /// Create a Google Chart of [chartType]. e.g. 'Table', 'AreaChart', | |
| 106 /// 'BarChart', the chart is rendered inside [element]. | |
| 107 Chart(String chartType, Element element) { | |
| 108 _chart = new JsObject(GoogleChart.api[chartType], [element]); | |
| 109 } | |
| 110 | |
| 111 /// When the user interacts with the table by clicking on columns, | |
| 112 /// you must call this function before [draw] so that we draw | |
| 113 /// with the current sort settings. | |
| 114 void refreshOptionsSortInfo() { | |
| 115 var props = _chart.callMethod('getSortInfo'); | |
| 116 if ((props != null) && (props['column'] != -1)) { | |
| 117 // Preserve current sort settings. | |
| 118 options['sortColumn'] = props['column']; | |
| 119 options['sortAscending'] = props['ascending']; | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 /// Draw this chart using [table] and the current [options]. | |
| 124 void draw(DataTable table) { | |
| 125 var jsOptions = new JsObject.jsify(options); | |
| 126 _chart.callMethod('draw', [table._table, jsOptions]); | |
| 127 } | |
| 128 } | |
| OLD | NEW |