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