Add jsdoc for setAnnotations() and an example of how to use them with native-format...
[dygraphs.git] / dygraph-gviz.js
1 /**
2 * @license
3 * Copyright 2011 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
5 */
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
20 /**
21 * A wrapper around Dygraph that implements the gviz API.
22 * @param {Object} container The DOM object the visualization should live in.
23 */
24 Dygraph.GVizChart = function(container) {
25 this.container = container;
26 }
27
28 Dygraph.GVizChart.prototype.draw = function(data, options) {
29 // Clear out any existing dygraph.
30 // TODO(danvk): would it make more sense to simply redraw using the current
31 // date_graph object?
32 this.container.innerHTML = '';
33 if (typeof(this.date_graph) != 'undefined') {
34 this.date_graph.destroy();
35 }
36
37 this.date_graph = new Dygraph(this.container, data, options);
38 }
39
40 /**
41 * Google charts compatible setSelection
42 * Only row selection is supported, all points in the row will be highlighted
43 * @param {Array} array of the selected cells
44 * @public
45 */
46 Dygraph.GVizChart.prototype.setSelection = function(selection_array) {
47 var row = false;
48 if (selection_array.length) {
49 row = selection_array[0].row;
50 }
51 this.date_graph.setSelection(row);
52 }
53
54 /**
55 * Google charts compatible getSelection implementation
56 * @return {Array} array of the selected cells
57 * @public
58 */
59 Dygraph.GVizChart.prototype.getSelection = function() {
60 var selection = [];
61
62 var row = this.date_graph.getSelection();
63
64 if (row < 0) return selection;
65
66 col = 1;
67 for (var i in this.date_graph.layout_.datasets) {
68 selection.push({row: row, column: col});
69 col++;
70 }
71
72 return selection;
73 }
74