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