Add a fast-path setSelection() if the points are in the expected place.
[dygraphs.git] / dygraph-gviz.js
CommitLineData
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.
d67a4279
DV
26 * @param {!HTMLDivElement} container The DOM object the visualization should
27 * live in.
28 * @constructor
74a5af31
DV
29 */
30Dygraph.GVizChart = function(container) {
31 this.container = container;
758a629f 32};
74a5af31 33
d67a4279
DV
34/**
35 * @param {GVizDataTable} data
36 * @param {Object.<*>} options
37 */
74a5af31
DV
38Dygraph.GVizChart.prototype.draw = function(data, options) {
39 // Clear out any existing dygraph.
40 // TODO(danvk): would it make more sense to simply redraw using the current
41 // date_graph object?
42 this.container.innerHTML = '';
43 if (typeof(this.date_graph) != 'undefined') {
44 this.date_graph.destroy();
45 }
46
47 this.date_graph = new Dygraph(this.container, data, options);
758a629f 48};
74a5af31
DV
49
50/**
51 * Google charts compatible setSelection
52 * Only row selection is supported, all points in the row will be highlighted
d67a4279 53 * @param {Array.<{row:number}>} selection_array array of the selected cells
74a5af31
DV
54 * @public
55 */
56Dygraph.GVizChart.prototype.setSelection = function(selection_array) {
57 var row = false;
58 if (selection_array.length) {
59 row = selection_array[0].row;
60 }
61 this.date_graph.setSelection(row);
758a629f 62};
74a5af31
DV
63
64/**
65 * Google charts compatible getSelection implementation
d67a4279 66 * @return {Array.<{row:number,column:number}>} array of the selected cells
74a5af31
DV
67 * @public
68 */
69Dygraph.GVizChart.prototype.getSelection = function() {
70 var selection = [];
71
72 var row = this.date_graph.getSelection();
73
74 if (row < 0) return selection;
75
30a5cfc6
KW
76 var points = this.date_graph.layout_.points;
77 for (var setIdx = 0; setIdx < points.length; ++setIdx) {
82c6fe4d 78 selection.push({row: row, column: setIdx + 1});
74a5af31
DV
79 }
80
81 return selection;
758a629f 82};