Commit | Line | Data |
---|---|---|
88e95c46 DV |
1 | /** |
2 | * @license | |
3 | * Copyright 2011 Dan Vanderkam (danvdk@gmail.com) | |
4 | * MIT-licensed (http://opensource.org/licenses/MIT) | |
5 | */ | |
74a5af31 DV |
6 | |
7 | /** | |
8 | * @fileoverview A wrapper around the Dygraph class which implements the | |
9 | * interface for a GViz (aka Google Visualization API) visualization. | |
10 | * It is designed to be a drop-in replacement for Google's AnnotatedTimeline, | |
11 | * so the documentation at | |
12 | * http://code.google.com/apis/chart/interactive/docs/gallery/annotatedtimeline.html | |
13 | * translates over directly. | |
14 | * | |
15 | * For a full demo, see: | |
16 | * - http://dygraphs.com/tests/gviz.html | |
17 | * - http://dygraphs.com/tests/annotation-gviz.html | |
18 | */ | |
19 | ||
758a629f DV |
20 | /*jshint globalstrict: true */ |
21 | /*global Dygraph:false */ | |
c0f54d4f DV |
22 | "use strict"; |
23 | ||
74a5af31 DV |
24 | /** |
25 | * A wrapper around Dygraph that implements the gviz API. | |
26 | * @param {Object} container The DOM object the visualization should live in. | |
27 | */ | |
28 | Dygraph.GVizChart = function(container) { | |
29 | this.container = container; | |
758a629f | 30 | }; |
74a5af31 DV |
31 | |
32 | Dygraph.GVizChart.prototype.draw = function(data, options) { | |
33 | // Clear out any existing dygraph. | |
34 | // TODO(danvk): would it make more sense to simply redraw using the current | |
35 | // date_graph object? | |
36 | this.container.innerHTML = ''; | |
37 | if (typeof(this.date_graph) != 'undefined') { | |
38 | this.date_graph.destroy(); | |
39 | } | |
40 | ||
41 | this.date_graph = new Dygraph(this.container, data, options); | |
758a629f | 42 | }; |
74a5af31 DV |
43 | |
44 | /** | |
45 | * Google charts compatible setSelection | |
46 | * Only row selection is supported, all points in the row will be highlighted | |
47 | * @param {Array} array of the selected cells | |
48 | * @public | |
49 | */ | |
50 | Dygraph.GVizChart.prototype.setSelection = function(selection_array) { | |
51 | var row = false; | |
52 | if (selection_array.length) { | |
53 | row = selection_array[0].row; | |
54 | } | |
55 | this.date_graph.setSelection(row); | |
758a629f | 56 | }; |
74a5af31 DV |
57 | |
58 | /** | |
59 | * Google charts compatible getSelection implementation | |
60 | * @return {Array} array of the selected cells | |
61 | * @public | |
62 | */ | |
63 | Dygraph.GVizChart.prototype.getSelection = function() { | |
64 | var selection = []; | |
65 | ||
66 | var row = this.date_graph.getSelection(); | |
67 | ||
68 | if (row < 0) return selection; | |
69 | ||
758a629f DV |
70 | var col = 1; |
71 | var datasets = this.date_graph.layout_.datasets; | |
72 | for (var k in datasets) { | |
73 | if (!datasets.hasOwnProperty(k)) continue; | |
74a5af31 DV |
74 | selection.push({row: row, column: col}); |
75 | col++; | |
76 | } | |
77 | ||
78 | return selection; | |
758a629f | 79 | }; |
74a5af31 | 80 |