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